LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Software (https://www.linuxquestions.org/questions/linux-software-2/)
-   -   Program that logs in on remote computer via ssh and executes commands on it (https://www.linuxquestions.org/questions/linux-software-2/program-that-logs-in-on-remote-computer-via-ssh-and-executes-commands-on-it-583117/)

harrybazeegar 09-08-2007 04:45 AM

Program that logs in on remote computer via ssh and executes commands on it
 
I am a network administrator and i want to write a program that can log in on remote computers via ssh and then execute certain commands on it.

What i want to know is:
1. how to make the program log in.
2. how to execute the commands.

Alien Bob 09-08-2007 06:42 AM

Simple.
First configure a public/private key pair for passwordless ssh-logins. This is what you need before you can write unattended scripts.
Look at these google hits, the process is well-documented: ssh private key setup

Once you have a key - assume that the key for your remote ssh server is "/home/alien/.ssh/myremoteserver" - you use a "here-document" to run the remote commands:
Code:

ssh -i "/home/alien/.ssh/myremoteserver" -l alien some.remote.net <<EOT
ls
cd public_html
ls -la
EOT

Using ssh-keys it is also possible to copy files between the two sites unattended (nice for backups):

Code:

rsync -va -e "ssh -i /home/alien/.ssh/myremoteserver -l alien" some.remote.net:mydailybackup_20070908.tar.gz .
Eric

acid_kewpie 09-08-2007 07:14 AM

not a networking question. moved to Linux - software. additionally you might like to look at a tool like dsh which allows you do do as above to multiple systems at once.


All times are GMT -5. The time now is 08:39 AM.