Cerca nel sito

HowTo enter from a Linux machine in other Linux machine via ssh, execute commands and return back

15. 03. 22
Visite: 2306

By Paolo Holzl, ver 1.0 of 24/5/2008


Licensed under a Creative Commons License.


I read a lot of document that describe the fundamental operations to do that but always incomplete or partial.
The purpose of this document is do a concrete example of the really indispensable istructions.

 



First problem is disable request for password ssh.
From client machine you have to execute

ssh-keygen -q -t rsa -N '' -f ~/.ssh/id_rsa
ssh-copy-id -i /root/.ssh/id_rsa userpc2@pc2

Now you can probe enter via ssh without password
ssh userpc2@pc2

This script for example go to postgresql, execute a dump (backup) of database opengroupware and copy it on pc1

#!/bin/bash
echo ------------------------------------------------------------------
date
echo Example of dump and copy
cd /media/sda3/pc1/dumpDatabases
rm bkOpengroupware
ssh userpc2@pc2 << EOF
su backup
cd
rm bkOpengroupware
echo Execute dump of Opengroupware
pg_dump -h localhost -Fc -x -f bkOpengroupware ogo
EOF
echo Copy working ...
scp userpc2@pc2:/var/lib/postgres/bkOpengroupware .
ls bk* -al
date
echo ------------------------------------------------------------------