Caputure the keystroke events of remote machine and display them on local machine - linux

I have two linux let say A and B systems connected via SSH. On machine A I have a QR scanner connected to it. I want to scan the QR code on machine A redirect it to a file and then display the decoded sting on machine B. For this purpose what I am trying to do is, I do ssh from the local B machine which runs a script to redirect the stdin to a file to capture the decoded strings. It just invoke a script on remote machine which redirect stdin timeout 5 cat > qrCode.txt.
The real problem is when invoke the scripts locally on my machine A it works fine, but when I try to do the same from B machine on A using SSH, I get the file qrCode.txt on remote machine B but it's empty. Does not contain anything.
So how can I redirect the stdin of remote machine to a file and then read from that file to display the decoded string on local machine.
I guess the terminal is missing, because the Qr scanner flush out everything to terminal but when i do ssh, there is no access to terminal so it doesn't capture the keystrokes.
Is there anyway to achieve this?
Here is the ssh command on system B
#!/bin/bash
echo "Going to invoke remotes script"
sshpass -p <password> ssh user#192.168.2.10 -q -tt "/home/user/scanQr.sh"
used -tt parameter to avoid the warning pseudo terminal will not be allocated because stdin is not a terminal
and this is the script on remote machine A
#!/bin/bash
cd /home/user
cat > code.txt

Related

SSH, run process and then ignore the output

I have a command that will SSH and run a script after SSH'ing. The script runs a binary file.
Once the script is done, I can type any key and my local terminal goes back to its normal state. However, since the process is still running in the machine I SSH'ed into, any time it logs to stdout I see it in my local terminal.
How can I ignore this output without monkey patching it on my local machine by passing it to /dev/null? I want to keep the output inside the machine I am SSH'ing to and I want to just leave the SSH altogether after the process starts. I can pass it to /dev/null in the machine, however.
This is an example of what I'm running:
cat ./sh/script.sh | ssh -i ~/.aws/example.pem ec2-user#11.111.11.111
The contents of script.sh looks something like this:
# Some stuff...
# Run binary file
./bin/binary &
Solved it with ./bin/binary &>/dev/null &
Copy the script to the remote machine and then run it remotely. Following commands are executed on your local machine.
$ scp -i /path/to/sshkey /some/script.sh user#remote_machine:/path/to/some/script.sh
# Run the script in the background on the remote machine and pipe the output to a logfile. This will also exit from the SSH session right away.
$ ssh -i /path/to/sshkey \
user#remote_machine "/path/to/some/script.sh &> /path/to/some/logfile &"
Note, logfile will be created on the remote machine.
# View the log file while the process is executing
$ ssh -i /path/to/sshkey user#remote_machine "tail -f /path/to/some/logfile"

Using local system as ssh client and server

I am using local system to learn ssh and what I am trying to do is execute a command on the remote server.
I have ssh server running on terminal1 and client on terminal2.
I used the following command on terminal2:
ssh user1#127.0.0.1 echo Display this.
but it echoes on terminal2. How would I know if the command actually worked if it's not displaying in terminal1?
Thank you.
It worked correctly. It ssh'd into the server, executed the command, and returned the stdout of that command back to you.
SSH gains access to the server, but not necessarily any TTY's active on it. You would have to jump through some hoops to send text to a specific TTY, such as your Terminal1.
A better test would be:
ssh user1#127.0.0.1 'touch ~/testfile'
Then you can check on your server (which is localhost) to see if testfile was created in your user1 home folder. If it did, then the connection and the command succeeded.

Remote execute script on Linux

Wondering if it is possible to write a shell script like this, and if possible, any reference/sample implementation I can refer to? Thanks.
Step 1, scp a local file on a local box to a remote box ABC, may need
input password or we can hard code password into script possible?
Step 2, remote execute a script on box ABC, and leverage the file
uploaded in Step 1
Step 3, the output of Step 2 (which is on
console/stdout) is redirected to local box.
I tried this:
scp ~/Downloads/data/1.dat root#host:/root/data /root/exercise/test /root/data/1.dat
I understand that you want to copy a file to a remote machine, run a command there with that file as an argument and get the output on your local machine. Apparently, you need the test program, which is on the remote machine.
Your try takes you halfway there. You could do it as follows:
scp ~/Downloads/data/1.dat root#host:/root/data
ssh root#host '/root/exercise/test /root/data/1.dat'
The first command copies your file, the second runs the command on the remote machine. Depending on the test command, you can new get some output file back to your local machine:
scp root#host:/root/results/outputfile .
Or, if the command writes to standard out, you could redirect the output to a file on the remote machine by appending > /root/results/outputfile to the ssh command and then scp it back to your local machine.
You can execute commands using ssh, for example:
$ ssh user#host ls -la
will connect to host host as user, and after successful authorization execute ls -la command, presenting the output locally. After command finishes connection will be closed.

how to write expect script to login and run command on remote box

i wanted to execute commands on remote linux box from windows and also wanted to collect result of executed command. Basically i have to pass 2 boxes to execute that command here is flow.
Login to a box
ssh to another box
run command
collect output of command locally (in file)
I tried following
F:\xyz>plink xyz#a1.b1.com -i F:\x\y\PRIVATEKEY.ppk -pw xyz
ssh -f root#166.1.8.1 yum upgrade Cyberc
but this is asking for password. I can do it by adding id_rsa.pub value in to authorized_keys but we dont have permission to do. So instead of that i wanted to write EXPECT script to pass user/pass and commands to complete my job.
Any help on EXPECT script would be much appreciated.
Unless the program on the remote linux host is interactive (i.e. it has prompts that the user must respond to), then you probably don't need to use expect - you can simply use plink to connect to the remote Linux host from your windows machine and run the command. You can specify the username and password to authenticate with the remote host in the plink command. See the following links for more info:
http://the.earth.li/~sgtatham/putty/0.58/htmldoc/Chapter7.html
http://stackoverflow.com/questions/12844944/login-syntax-for-plink-using-ip-username-and-password

how to write a shell script to make an ssh connection to a machine and continue remaining script on that machine

I am writing a script where I run the script on one server through which I create an ssh connection to another machine and want to continue the following script code on the remote machine.... Can any body tell what is the way to do it?
if you want to execute a command on remote host through ssh and get the output on local host, that what i understand from you question then use.
ssh -n <hostname/IP> 'command'
where -n will Redirects stdin from /dev/null
you can store the output to a variable or file as
var =`ssh -n <hostname/IP> 'command'`
or
ssh -n <hostname/IP> 'command' >> output.txt
also if you want to send multiple command use ; for command separator.
NOTE: ssh without password should be enable from local host to remote host.
else you need to specify password explicitly.

Resources