リモートログインしてコマンドを実行するスクリプト

sshで接続する場合、expectを使う。

-------------------------------------------------------------
#!/bin/sh
hosts=`cat TEST.list`
user=************
pass=****************
for host in $hosts
do
  ./test.exp $user $pass $host
done 
---------------------------------------------------------------------

ここで「TEST.list」は接続先の機器の一覧で一行に一つ記述する。
test.expはこんな感じ。

-----------------------------------------------------------------------------------------
#!/usr/bin/expect

log_file expect.log

set User [lindex $argv 0]
set PW [lindex $argv 1]
set RemoteHost [lindex $argv 2]
set Prompt "\[#$%>\]"

set timeout 5

spawn ssh -l ${User} ${RemoteHost}
expect {
  -glob "(yes/no)?" {
    send "yes\n"
    exp_continue
  }
  -glob "User AuthenticationPassword:" {
    send "${PW}\n"
  }
}
expect {
  -glob "${Prompt}" {
    send "<コマンド>\n"
  }
}

expect {
  -glob "${Prompt}" {
    send "quit\n"
  }
}
-----------------------------------------------------------------------------

「set Prompt “[#$%>]”」は、コマンドプロンプトに使われる文字を指定している。接続先の端末毎に指定すれば良い。
「-glob “(yes/no)?” {」のところは、初めてssh接続する端末はカギを保存するか聞いてくるので「yes」と入力する、という記述。
「-glob “User AuthenticationPassword:” {」はパスワードを聞かれたらパスワードを入力するという記述。「“User AuthenticationPassword:”」の部分は接続先の端末がなんと聞いてくるかを記述する。