Bash SSH Shell Script – How to Write a Script to Log onto Another Machine

bashshell-scriptssh

Is it possible to write a bash script, that

  1. would be started from machine A, logs in on a different machine B by ssh (both machines A and B would be Linux-Machines),
  2. copys some files on to machine B
  3. runs a python script a given python script on these machines.
  4. transfers the results back to machine A
  5. logs off from machine B.

Is this technically doable?

Best Answer

Of course it is doable:

scp file user@host:
ssh user@host path_to_script
scp user@host:file_to_copy ./

and that's it...

But there is one problem: you will be asked for password three times. To avoid that you could generate ssh keys and authorize users by these keys.

To generate ssh keys run ssh-keygen -t rsa, answer questions and copy public key to remote host (machine B) to ~/.ssh/authorized_keys file. Private key should be saved in ~/.ssh/id_rsa on local machine (A).

Related Question