これをファイルに保存しますtelnet.exp
:
#!/usr/bin/expect -f
spawn telnet 10.123.9.111
expect login {send username\r}
expect password {send password\r}
interact ;# I assume you want to do something on the remote machine here
実行可能にする:chmod 700 telnet.exp
-パスワードをプレーンテキストファイルに保存しているため、制限付きのアクセス許可を使用することをお勧めします(デバイスがsshを実行できない場合を除き、キーとともにsshを使用する必要があります)。
それを実行します:./telnet.exp
スクリプトに変数を渡せるようにする場合:
#!/usr/bin/expect -f
lassign $argv ip user passwd
spawn telnet $ip
expect login {send $user\r}
expect password {send $passwd\r}
interact ;# I assume you want to do something on the remote machine here
それを実行します:./telnet.exp 10.123.9.111 username secret
ps
パスワードがリストに表示されるようになるため、これも悪いことです。