2017-01-22 5 views
0

192.168.119.128から192.168.119.129に自動ログインしていくつかのコマンドを実行したいので、私はexpectスクリプトを書いています。対話指令なしのLinux expectコマンド

[email protected]:~/Work$ ./a.sh 
spawn ssh [email protected] 
[email protected]'s password: 
Last login: Sun Jan 22 17:36:21 2017 from 192.168.119.128 
[[email protected] ~]# [email protected]:~/Work$ 

私はsuccessfulyログインが、touch /tmp/a.txtコマンドが実行されていないようだ。

a.sh

#!/usr/bin/expect -f 

set timeout 5 
spawn ssh [email protected] 
expect "password" {send "123456\r"} 
expect "]#" {send "touch /tmp/a.txt\r"} 
#interact 

出力されます。

#interacta.shのコメントを外すと正常に動作し、ファイルa.txtが作成されます。ここで

#!/usr/bin/expect -f 

set timeout 5 
spawn ssh [email protected] 
expect "password" {send "123456\r"} 
expect "]#" {send "touch /tmp/a.txt\r"} 
interact 

が出力されます。

[email protected]:~/Work$ ./a.sh 
spawn ssh [email protected] 
[email protected]'s password: 
Last login: Sun Jan 22 17:41:23 2017 from 192.168.119.128 
[[email protected] ~]# touch /tmp/a.txt 
[[email protected] ~]# 

なぜinteractディレクティブスクリプトの仕事のない間違いましたか?ありがとう。

答えて

1

interactを指定しないと、最後のコマンドexpect "]#"の後にExpectスクリプトが終了し、生成されたプロセスが終了します。これは、シェルがまだ実行中のときに、(PuTTYのような)SSHクライアントアプリケーションを閉じるのと同じです。

interactは、実行されたプロセスが終了するのを待つ長時間実行コマンドです。

+0

はい、最後に "#interact"を "expect"に変更しました。# "{close}"が動作します。 –

関連する問題