**文書の過去の版を表示しています。**
expectによる入力の自動化
次のようなファイルを作っておいて、./ssh.exp 192.168.251.154 などとやるとパスワードを自動入力してssh接続が可能。
#!/usr/bin/expect set RemoteHost [lindex $argv 0] set PW "programming" #set PW [lindex $argv 1] set Prompt "\[#$%>\]" set timeout 5 spawn env LANG=C /usr/bin/ssh -o StrictHostKeyChecking=no -l training ${RemoteHost} expect { -glob "(yes/no)?" { send "yes\n" exp_continue } -glob "password:" { send -- "${PW}\n" } } expect { -glob "${Prompt}" { interact exit 0 } }
ssh先でコマンドを実行して終わる場合は
#!/usr/bin/expect set PW "programming" set RemoteHost [lindex $argv 0] #set PW [lindex $argv 1] set Prompt "\[#$%>\]" set timeout 5 spawn env LANG=C /usr/bin/ssh -o StrictHostKeyChecking=no -l training ${RemoteHost} expect { -glob "(yes/no)?" { send "yes\n" exp_continue } -glob "password:" { send -- "${PW}\n" } } expect { -glob "${Prompt}" { log_user 0 send "date\n" send "gsettings set org.gnome.desktop.session idle-delay 0\n" } } expect { -regexp "\n.*\n" { log_user 1 send "exit\n" exit 0 } }