2013-06-16 7 views
1

의 표준 출력을 읽었으므로 일부 기본 SSH 테스트를 위해 paramiko와 작업 중이었고 표준 출력으로 출력되지 않았습니다. 내 코드를 heres. 내가 이것을 실행할 때마다 (I가 CP처럼 뭔가를 할 경우, 파일이 복사에 의해 볼 때) Paramiko : 원격으로 실행 된 명령

import paramiko 
client=paramiko.SSHClient() 
client.set_missing_host_key_policy(paramiko.AutoAddPolicy()) 
com="ls ~/desktop" 
client.connect('MyIPAddress',MyPortNumber, username='username', password='password') 
output="" 
stdin, stdout, stderr = client.exec_command(com) 

print "ssh succuessful. Closing connection" 
client.close() 
print "Connection closed" 
stdout=stdout.readlines() 
print stdout 
print com 
for line in stdout: 
    output=output+line 
if output!="": 
    print output 
else: 
    print "There was no output for this command" 

그래서, 명령이 실행됩니다,하지만 난 항상 "이 명령에 대한 출력이 없었다"얻는다. stdout = stdout.readlines()를 출력하면 []가 출력됩니다. 또한 print 문을 for 루프에 추가하면 결코 실행되지 않습니다. 누군가 나를 도와 줄 수 있니? 감사!

답변

5

당신은 읽기 라인 전에 연결을 닫았 :

import paramiko 
client=paramiko.SSHClient() 
client.set_missing_host_key_policy(paramiko.AutoAddPolicy()) 
com="ls ~/desktop" 
client.connect('MyIPAddress',MyPortNumber, username='username', password='password') 
output="" 
stdin, stdout, stderr = client.exec_command(com) 

print "ssh succuessful. Closing connection" 
stdout=stdout.readlines() 
client.close() 
print "Connection closed" 

print stdout 
print com 
for line in stdout: 
    output=output+line 
if output!="": 
    print output 
else: 
    print "There was no output for this command" 
+0

허, 그렇게 생각하지 않았습니다. 감사! – ollien

+0

@jabaldonedo이 코드를 프로젝트에서 사용해 보았습니다. 문제는 SSH가 있고 stdout.read() 행이 중단 될 때입니다. 위의 코드를 사용하는 것과 마찬가지로'print 'ssh 성공을 얻습니다. 연결을 닫고 "중지합니다. 나는 CTRL + C조차 빠져 나올 수 없다. 이 문제를 해결할 수없는 것 같습니다. – tjoenz

+0

나는 그 코드에 아무런 문제가 없으며, 당신이 무엇을하려고하는지, 그리고 당신의 실행 구성을 모른 채 그 오류를 일으키는 원인을 말하기는 쉽지 않다. 어쩌면 문제를 설명하는 새로운 질문과 지금까지 시도한 내용을 게시해야합니다. – jabaldonedo

0

* 대화 형 예 : ==== 1 부, 이것은 서버에서 쉬 출력을 보여 끝에 ">" 입니다 계속 또는 종료에 몇 가지 입력을 필요 ======

selilsosx045 : uecontrol-CXC_173_6456-R32A01 lteue $ ./uecontrol.sh -host localhost를 UE 제어 : 시작 UE 제어 사용 : UE 제어 : 자바 - Dlogdir = -Duecontrol.c onfigdir =./etc -jar ./server/server-R32A01.jar -host localhost 파일에서 등록 정보로드 /Users/lteue/Downloads/uecontrol-CXC_173_6456-R32A01/etc/uecontrol.properties 호스트 로컬 호스트에 대한 원격 CLI 시작 CLI를 종료하려면 Q 명령을 입력하고 사용 가능한 명령에 대한 정보를 얻으려면 HELP 을 입력하십시오. CLI가 입력 준비가되었습니다. peramiko ============ *

UEC>

=========== Pyhton 코드는 방법을 아래에보십시오 :하지 표준 출력 동안 .channel.exit_status_ready() :

def shCommand(server_list): 
server_IP = server_list[0] 
username = server_list[1] 
password = server_list[2] 

ssh = paramiko.SSHClient() 
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy()) 
ssh.connect(server_IP,22,username, password)strong text 

commandList = ['list \n'] 
alldata = getUeInfo(ssh,commandList) 
ssh.close() 

def getUeInfo(ssh,commandList): 
data_buffer = "" 
num_of_input = 0 
stdin, stdout, stderr = ssh.exec_command('cmd') 
while not stdout.channel.exit_status_ready(): 
    solo_line = ""   

    if stdout.channel.recv_ready(): 

     solo_line = stdout.channel.recv(1024) # Retrieve the first 1024 bytes 
     data_buffer += solo_line    


    if(cmp(solo_line,'uec> ') ==0): #len of solo should be 5 , 
    if num_of_input == 0 : 
     data_buffer = ""  
     for cmd in commandList : 
     #print cmd 
     stdin.channel.send(cmd) 
     num_of_input += 1 
    if num_of_input == 1 : 
     stdin.channel.send('q \n') 
     num_of_input += 1 

return data_buffer