9

PythonインストールにExpectスクリプト「myexpect.sh」を実行するように指示しようとしています。

#!/usr/bin/expect
spawn ssh usr@myip
expect "password:"
send "mypassword\n";
send "./mycommand1\r"
send "./mycommand2\r"
interact

私はWindowsを使用しているので、Expectスクリプトの行をPythonに書き直すことはできません。助言がありますか?「./myexpect.sh」がbashシェルから実行する方法で実行できるものはありますか?


サブプロセスコマンドである程度の成功を収めました。

subprocess.call("myexpect.sh",  shell=True)

エラーが発生しました:

myexpect.shは有効なWin32アプリケーションではありません。

これを回避するにはどうすればよいですか?

4

2 に答える 2

23

pexpectライブラリを使用します。これは、Expect機能のPythonバージョンです。

例:

child = pexpect.spawn('Some command that requires password')
child.expect('Enter password:')
child.sendline('password')
child.expect(pexpect.EOF, timeout=None)
cmd_show_data = child.before
cmd_output = cmd_show_data.split('\r\n')
for data in cmd_output:
    print data

Pexpectには、学ぶべき例がたくさんあります。インタラクト()の使用については、script.py例から確認してください。

(Windowsの場合、pexpectに代わるものがあります。)

于 2012-06-22T16:41:54.860 に答える
0

.expectスクリプトなので、スクリプトの拡張名を変更する必要があると思います。

使用する代わりに

subprocess.call("myexpect.sh", shell=True)

あなたが使用する必要があります

subprocess.call("myexpect.expect", shell=True)
于 2018-02-28T20:17:19.287 に答える