私のコードに問題があります。出力が小さい場合はうまく動作しますが、出力が大きい場合は破損します。データは小さいですが、データが大きいとき失敗したときにsubprocess.CalledProcessErrorとssh接続が、sshコマンドが大量の出力を生成すると、ドロップする
def listDevices(username, pass, regex):
command = "list-dev " + regex
deviceArray = []
connectString = "plink -ssh -l " + username + " -pw " + pass + " -P " + SshPort + " " + Server + " \"" + command + "\""
rawList = subprocess.check_output(connectString, shell=True)
for line in rawList.split("\r\n"):
if "" is not line:
deviceArray.append(line)
print deviceArray
return deviceArray
Server = 10.10.10.1
SshPort = 22
username = "test"
pass - "password"
regex = "rt*mdr*"
mdrList = listDevices(username, pass, regex)
print mdrList
これは正常に動作します:
は、ここに私のコードです。ここで
は誤りです:subprocess.CalledProcessError: Command 'plink -ssh -l test -pw password -P 4000 10.10.10.1 "list-dev *"' returned non-zero exit status 1
編集:私はPLINK交換しparamikoを書きましたが、それでもすべてのデータを取得していない
。ここでのコードは次のとおりです。
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect(ip,username=username, password=password, port = 9000)
list =["list-devices rt*"]
command = '\n'.join(list)
print command
stdin,stdout,stderr = ssh.exec_command(command)
print stdout.read()
それはエラーの下に私を与える:
Traceback (most recent call last):
File "C:/Users/xx/Scripts/Test2.py", line 31, in <module>
stdin,stdout,stderr = ssh.exec_command(command)
File "C:\Python27\paramiko\client.py", line 404, in exec_command
chan.exec_command(command)
File "C:\Python27\paramiko\channel.py", line 60, in _check
return func(self, *args, **kwds)
File "C:\Python27\paramiko\channel.py", line 229, in exec_command
self._wait_for_event()
File "C:\Python27\paramiko\channel.py", line 1086, in _wait_for_event
raise e
paramiko.ssh_exception.SSHException: Channel closed.
これを簡略化するために['subprocess.check_output()'](https://docs.python.org/2/library/subprocess.html#subprocess.check_output)を使用できますか?一時ファイルに書き込む必要はありません。 –
@Wyatt、これはまさに私が最初にやっていることですが、失敗していました。私は、おそらくファイルに保存すると動作すると思っていたが、どちらもそれをしなかったと考えていた。小さなデータでも動作しますが、データが膨大な場合は失敗します。 – Neo
私は最初のバージョンをスキミングして、実際には2番目のバージョンだけを見ました。 'plink'コマンドが大きなデータのタイムアウトになっているようです。コマンドラインで直接実行すると正常に完了しますか? –