Wednesday, November 12, 2014

How to run a command on Remote server without login to server shell prompt?

http://www.nextstep4it.com/how-to-run-a-command-on-remote-server-without-login-to-server-shell-prompt

By using SSH command, You can run a command on remote server without login to server shell.
SSH Command Format:
# ssh remoteuser@remotehost remotecommand
Suppose I want to run ls command on remote server pk.testmail.com
parveen@Earth:~$ ssh root@pk.testmail.com ls
 The authenticity of host ' pk.testmail.com (x.x.x.x)' can't be established.
 RSA key fingerprint is 7d:74:91:ed:30:1e:86:0b:69:c9:77:0b:72:0e:ad:4e.
 Are you sure you want to continue connecting (yes/no)? yes
 Warning: Permanently added 'pk.testmail.com' (RSA) to the list of known hosts.
 root@pk.testmail.com's password:

anaconda-ks.cfg
downloads
install.log
install.log.syslog
Note : - When we run any command on remote server, by default ssh will not allocate a pseudoterminal. For using pseudoterminal we have to use -t option with ssh command as explained below.
parveen@Earth:~$ ssh -t root@ pk.testmail.com ls
 root@pk.testmail.com's password:
 anaconda-ks.cfg  downloads  install.log  install.log.syslog
 Connection to pk.testmail.com closed.
Now you can see, Command out put will be same as we run same command after login to remote server.
To run multiple commands on remote server, use semi-colon between commands as shown below:
parveen@Earth:~$ ssh -t root@ pk.testmail.com "ls ; df -h ; date ; cal"
 root@pk.testmail.com's password:
 anaconda-ks.cfg  downloads  install.log  install.log.syslog  server_invalid_mails_cleanup.sh
 Filesystem      Size  Used Avail Use% Mounted on
 /dev/vda         20G  6.9G   12G  37% /
 tmpfs           246M     0  246M   0% /dev/shm
 Mon Nov 10 00:52:12 EST 2014
 November 2014
 Su Mo Tu We Th Fr Sa
 1
 2  3  4  5  6  7  8
 9 10 11 12 13 14 15
 16 17 18 19 20 21 22
 23 24 25 26 27 28 29
 30
 Connection to pk.testmail.com closed.
We can also use echo command to run a remotecommand on remoteserver.
parveen@Earth:~$ echo "ls" | ssh root@pk.testmail.com
 Pseudo-terminal will not be allocated because stdin is not a terminal.
 root@pk.testmail.com's password:
 anaconda-ks.cfg
 downloads
 install.log
 install.log.syslog
Note:- We can’t use -t option with ssh command to run remotecommand using echo command because Pseudo-terminal will not be allocated because stdin is not a terminal.
Use ssh command with tar command to move a directory of files between two machines as an alternative to scp command. Example is explained below:
parveen@Earth:~$ tar -cvf - images/test1 | ssh root@ pk.testmail.com  '(cd /tmp/; tar -xf -)'
 images/test1/
 images/test1/7
 images/test1/6
 images/test1/5
 images/test1/4
 images/test1/9
 images/test1/8
 images/test1/3
 images/test1/2
 images/test1/1
 root@pk.testmail.com's password:

Now files are uploaded on Remote server. Check files on remote server as shown below.

[root@pk tmp]# ls -l images/
 total 4
 drwxrwxr-x 2 1000 1000 4096 Nov 10 01:02 test1
 [root@pk tmp]# ls -l images/test1/
 total 0
 -rw-rw-r-- 1 1000 1000 0 Nov 10 01:02 1
 -rw-rw-r-- 1 1000 1000 0 Nov 10 01:02 2
 -rw-rw-r-- 1 1000 1000 0 Nov 10 01:02 3
 -rw-rw-r-- 1 1000 1000 0 Nov 10 01:02 4
 -rw-rw-r-- 1 1000 1000 0 Nov 10 01:02 5
 -rw-rw-r-- 1 1000 1000 0 Nov 10 01:02 6
 -rw-rw-r-- 1 1000 1000 0 Nov 10 01:02 7
 -rw-rw-r-- 1 1000 1000 0 Nov 10 01:02 8
 -rw-rw-r-- 1 1000 1000 0 Nov 10 01:02 9

No comments:

Post a Comment