2017-10-26 6 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 

편집 : 가용 스루풋 교체 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

첫 번째 버전을 건너 뛰었고 실제로 두 번째 버전 만 보았습니다. 'plink' 명령이 더 큰 데이터에서 시간 초과되는 것처럼 보입니다. 명령 줄에서 직접 실행하면 성공적으로 완료됩니까? –

답변

1

plink ssh not working with multiple commands passed in a file. - 65059 - The Cisco Learning Network에 따르면,이 시스코 라우터에 문제가있어 파이썬 관련이없는 여기에 코드입니다.

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

    는 TCP 규칙에 따라, 그것은 단지 입력 소켓을 닫아야에도 불구하고 즉시 시스코는 입력에 EOF를 참조로, 그것은 연결 모두 측면을 삭제 있다고 말한다. 해결 방법은 모든 출력이 다운로드 될 때까지 EOF를 지연시키는 것입니다. 빠른 & 더러운 sleep을 사용하지만 스크립팅에는 신뢰할 수 없습니다.

  • Putty Dies with Large Output : networking - Reddit은 MTU 문제라고 말합니다. 증상은 정보를 더 한 약 화면을 얻을 수있는되지 않습니다

    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가 아니며, 단지 명령을 포함하는 추가 데이터입니다. 첫 번째 링크의 또 다른 해결 방법은 입력 명령 사이에 몇 줄의 뉴 라인을 삽입하는 것입니다. 이런 식으로 보면, 깨진 전송 로직이 삽입되어 그 위에 숨어있는 패딩 대신 사용할 수 있습니다.