내 코드에 문제가 있습니다. 그것은 출력이 작을 때 잘 작동하지만 출력이 클 때는 깨집니다. 데이터가 작지만 데이터가 큰 경우 실패 할 때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.
조금 단순화하기 위해 ['subprocess.check_output()'] (https://docs.python.org/2/library/subprocess.html#subprocess.check_output)을 사용할 수 있습니까? 임시 파일에 쓸 필요는 없습니다. –
@Wyatt, 이것은 내가 처음에 이미하고있는 일이지만, 실패하고있었습니다. 나는 파일로 저장하는 것이 효과가 있을지도 모른다고 생각한 두 번째 파일을 만들었지 만 어느 쪽도하지 못했습니다. 작은 데이터에서는 작동하지만 데이터가 크면 실패합니다. – Neo
첫 번째 버전을 건너 뛰었고 실제로 두 번째 버전 만 보았습니다. 'plink' 명령이 더 큰 데이터에서 시간 초과되는 것처럼 보입니다. 명령 줄에서 직접 실행하면 성공적으로 완료됩니까? –