Remote execute script on Linux - 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.

Related

Running history command remotely in linux

My requirement is to save history of the commands into a file called history_yymmdd.txt by running the following command on a remote server.
history > history_20170523.txt
I tried with the following command, but it was creating a blank file on remote server(10.12.13.14).
ssh 10.12.13.14 "history > history_20170523.txt"
When I log in to the remote server and run the history command, then the file was created successfully. But I need to run the command on 20 servers so creating a script to run it remotely on each server is my objective here.
Thanks in advance.
ssh user#machine_name "cat ~/.bash_history > history_20170523.txt"
The 'history' command dumps the contents of .bash_history, so this may be useful to you.
A more elegant solution might be:
scp user#machine_name:~/.bash_history history_20170523.txt
you are doing it in a wrong way, also there is no user for the remote machine. Correct way to do is
ssh -q -tt user#10.12.13.14 'history > history_20170523.txt'

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

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

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.

Executing a command on remote via ssh doesn't work

I am trying to execute a command on remote server using ssh. The command is as
ssh machine -l user "ls"
This command get stuck in between and finally we have to suspend it.
But, executing the command ssh machine -l user works fine and this command makes us connect to remote machine.
Can someone please help in getting the root cause of why the ls on remote server doesn't work by ssh.
EDIT 1 : Here is the ouput after using -v switch with SSH
debug1: Authentication succeeded (publickey).
debug1: channel 0: new [client-session]
debug1: Entering interactive session.
debug1: Sending environment.
debug1: Sending env LANG = en_US.UTF-8
debug1: Sending command: ls
After printing Sending command: ls the terminal stucks.
I suspect one of two things is happening. First of all, the ssh server may be set to start a particular command for the user, regardless of what command you asked to run. You'd see this behavior if the user was restricted to running SFTP in the usual manner, for example. There are two ways this may be set up:
A ForceCommand directive in the remote server's sshd configuration file.
A directive in the remote user's authorized_keys file for the key being used.
The simplest way to check this would be to log in to the remote server and examine the two files. Alternately, you could start one of these ssh sessions, let it hang, and then run "ps" on the remote server to see what actual processes are running for the user in question.
The other possibility is that the remote user has a line in his .bashrc or other shell startup script which is introducing a wait or else waiting for you to type something. Again, you should start one of these ssh sessions, let it hang, and then run "ps" on the remote server to see what actual processes are running for the user.
Questions:
Does the problem occur on the commandline or within a script?
Are you prompted for your passowrd?
Is there any output? If yes: post it here.
And try
ssh -v user#host "ls"
or
ssh -v -l user host "ls"
and you will get additional output. You can use -v option upto 3 times for higher verbosity.
ssh -vvvl user host "ls"
EDIT:
If I had to debug this, I'd do the following:
go to the target machine, the one you want to 'ssh' to.
log in with the same user you tried with ssh
enter the "ls" command"
It is an unusal thing, but 'ls' is not necessarily what you expect it to be. At the commandline on the target-machine, try
which ls
and then use the output with the fully qualified name for your ssh call, e.g.:
ssh machine -l user "/bin/ls"
Remember, that when excuting a command via ssh you do not automatically have the same path as with a regular login.
Finally, examine your log-files on the target-machine. They usually reside under /var/log (at least under debian).
EDIT2:
On linux machines, I've sometimes experienced a problem with the 'ls' command hanging without any output. This happend to me when there were filesystems in the directory which were in some way 'invalid'. For example if there was an invalid mount of an android mtpfs, the ls command couldn't deal with that and hung.
So try to 'ls' a different directory, e.g.
ssh host -l user "ls /tmp"
If this works, then check from the commandline whether there is a directory or a file whith some invalid state which causes the ls command to fail.

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

Resources