Shammer's Philosophy

My private adversaria

Automate the operation started from ssh login

I would like to automate the CLI operation started from ssh login.
This may be a useful if you would like to repeat the similar command.
This can be come true with sshpass. Here is a sample.

for i in `seq 1 10`;do (echo "somecommand 1";echo "somecommand 2";echo "specific_command $i";echo "exit") | sshpass -p $pass ssh $sshuser@$ssh_target_host;done

This example is same as the following steps.

  1. ssh $sshuser@$ssh_target_host
  2. Type $pass as $sshuser password
  3. execute somecommand 1
  4. execute somecommand 2
  5. execute specific_command 1
  6. logout by exit
  7. ssh $sshuser@$ssh_target_host
  8. Type $pass as $sshuser password
  9. execute somecommand 1
  10. execute somecommand 2
  11. execute specific_command 2
  12. logout by exit
  13. repeat from ssh $sshuser@$ssh_target_host to logout by exit until repeat count will reach 10

Specific_command requires any argument which is not static value. This is useful against the case like defining multiple IP address from remote host.