0

を使用してサーバーに接続しており、順次出力を受信するためparamikoに使用しようとしています。channel.send以下のスクリプトは、 からの出力をキャッチできませんchannel.recv。何か案は?

import paramiko
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect("xx.xx.xx.xx",username='kshk',password='xxxxxxxx',key_filename='/home/krisdigitx/.ssh/id_rsa')
channel = ssh.invoke_shell()
channel.send('df -h\n')

while channel.recv_ready():
    outp = channel.recv(1024)
print outp

与えます:

krisdigitx@krisdigitx-Dell-System-XPS-L702X:~/SCRIPTS$ python test.py 
Traceback (most recent call last):
  File "test.py", line 11, in <module>
    print outp
NameError: name 'outp' is not defined

インタープリターモードでスクリプトを実行すると機能します...

>>> import paramiko
>>> ssh = paramiko.SSHClient()
>>> ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
>>> ssh.connect("xx.xx.xx.xx",username='kshk',password='xxxxx',key_filename='/home/krisdigitx/.ssh/id_rsa')
>>> channel = ssh.invoke_shell()
>>> channel.send('df -h\n')
6
>>> while  channel.recv_ready():
...     outp = channel.recv(1024)
... 
>>> print outp
/dec/sda
                      7.2T  6.6T  622G  92% /tmp/xxx
[kshk@server ~]$ 
>>> 
4

1 に答える 1

0

with ステートメントで作成したコンテキスト マネージャーのスコープで outp を定義します。コンテキストマネージャーのスイート内で印刷してみてください。つまり

while channel.recv_ready():
    outp = channel.recv(1024)
    print outp

またはこれを行います:

outp = ""
while channel.recv_ready():
    outp = channel.recv(1024)
print outp
于 2013-04-25T19:04:15.063 に答える