次の方法
はあり
ます
か?
注:
- スクリプトは、パテ経由で Linux サーバーにアクセスする Windows マシンで実行する必要があります。
- 1 は subprocess.Popen() で簡単でした。
- 次回サーバーにログインするためのパスワードの送信に行き詰まっています。RSA ssh キーは、サーバー上で制限されています。
何か案は?Pythonスクリプトの他の代替手段はありますか?
はい、できます。を使用しpexpect
ます。
ただし、cygwin をインストールしないと Windows で pexpect を使用できないことに注意してください。Cygwin を使用せずに Windows でプログラムを直接実行する場合は、winexpect
( https://bitbucket.org/geertj/winpexpect/wiki/Home ) を使用します。
Pexpect/Winexpect の使用例:
#!/usr/bin/env python
import pexpect
ssh_newkey = 'Are you sure you want to continue connecting'
# my ssh command line
p=pexpect.spawn('ssh mysurface@192.168.1.105 uname -a')
i=p.expect([ssh_newkey,'password:',pexpect.EOF])
if i==0:
print "I say yes"
p.sendline('yes')
i=p.expect([ssh_newkey,'password:',pexpect.EOF])
if i==1:
print "I give password",
p.sendline("mypassword")
p.expect(pexpect.EOF)
elif i==2:
print "I either got key or connection timeout"
pass
print p.before # print out the result
あなたの場合、plink
代わりにssh
andwinexpect
の代わりに使用する必要がありますpexpect
。