scripting an ssh session?

I know the root login/password for a machines, and I want to automate some commands like this from each:

ssh root@remoteHost1 "tail /var/log/messages"
ssh root@remoteHost2 "tail /var/log/messages"
ssh root@remoteHost3 "tail /var/log/messages"
ssh root@remoteHost4 "tail /var/log/messages"
ssh root@remoteHost5 "tail /var/log/messages"

My only problem is that I cant guarantee that the keys and such exist every time, cause there is often re-imaging done without my knowledge... and I dont want to enter "yes" and my password 6 times each time I wanna run my script... so I need to setup something like passwordless ssh but using a script to set it up, and then another script to run my commands... or both in one script, doesn't matter to me...

Is this doable? or is it possible to constantly check STDIN, and when it matches /password\:confused: then I pipe it my password, and if it matches /\(yes\/no\)/ then I know to pipe it "yes"... but then how would I mimic the enter key, cause Im pretty sure "\n" is not enter, but just a new line

any help here?

  1. Get your public key into the image.
  • OR *
  1. Get the people who do the re-imaging to re-install your key when they re-image.
  • OR *
  1. ssh to your own machine taking an input file but use the option to allocate a pseudo-terminal, then in that ssh shell do the ssh to the actual target.

Basically to get the password entered you should have a terminal allocated.

Thanks for the reply

I think I found a CPAN module that will allow me to use both a username and password

Net::SSH

Seems like it is a fully loaded SSH session within Perl, so will do what I need it to.