2017-10-26 4 views
1

私のコードに問題があります。出力が小さい場合はうまく動作しますが、出力が大きい場合は破損します。データは小さいですが、データが大きいとき失敗したときに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. 
+0

これを簡略化するために['subprocess.check_output()'](https://docs.python.org/2/library/subprocess.html#subprocess.check_output)を使用できますか?一時ファイルに書き込む必要はありません。 –

+0

@Wyatt、これはまさに私が最初にやっていることですが、失敗していました。私は、おそらくファイルに保存すると動作すると思っていたが、どちらもそれをしなかったと考えていた。小さなデータでも動作しますが、データが膨大な場合は失敗します。 – Neo

+0

私は最初のバージョンをスキミングして、実際には2番目のバージョンだけを見ました。 'plink'コマンドが大きなデータのタイムアウトになっているようです。コマンドラインで直接実行すると正常に完了しますか? –

答えて

1

plink ssh not working with multiple commands passed in a file. - 65059 - The Cisco Learning Networkによると、これはCiscoルータの問題ですので、Pythonの関連ではありません。

  • SSH using public key authentication to ... - Cisco Support Community

    は、TCPのルールに従って、それが唯一の入力ソケットを閉じる必要がありますにもかかわらず、すぐにシスコは、入力時にEOFを見るように、それは接続の両方脇をドロップすることを言います。これは、すべての出力がダウンロードされるまでEOFを遅らせることを提案します。それは速い&ダーティーsleepを使用しますが、それはスクリプト作成には信頼できません。

  • Putty Dies with Large Output : networking - Redditは、MTUの問題です。症状が情報のおよそ1画面より取得することができないされています。実際には

    I've come across a few MTU-related issues that manifested in terminal emulators in a similar manner. Usually it's some sort of point-to-point leased line that's carried in a VLAN where the added bytes for tagging mess things up and drop the frame in transit. When that happens, shorter output will go through fine, but longer output will just kill the session. Sometimes gracefully, other times not.

を、最後のものは正しい説明であると思われます。接続ドロップを引き起こすのはEOFではありません。コマンドを実行した直後の追加データです。第1リンクのもう一つの回避策は、入力コマンドの間にいくつかの改行を挿入することです。この方法では、壊れた送信ロジックが挿入されて詰まることの代わりにパディングとして機能します。

関連する問題