2016-06-24 4 views
2

paramiko을 사용하여 원격 호스트에 SSH Channel을 생성합니다. 그러나 ssh_object.exec_command을 사용하여 명령을 실행하려고하면 명령이 실행되지 않습니다.Python2.7 : ssh.exec_command가 명령을 실행하지 않음

이 기능은 내 ssh 핸들러 작성

def ssh_connect(ip,user,pwd): 
    ''' 
    This function will make an ssh connection to the ip using the credentials passed and return the handler 
    Args: 
     ip: IP Address of the box into which ssh has to be done 
     user: User name of the box to which ssh has to be done 
     pass: password of the box to which ssh has to be done 
    Returns: 
     An ssh handler 
    ''' 
    ssh = paramiko.SSHClient() 
    ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy()) 
    ssh.connect(ip, username=user, password=pwd) 
    return ssh 

을 내가 처리기를 사용하고있는 곳은 다음과 같습니다

ssh_obj = ssh_connect(ip, username, password) 
folder = "/var/xyz/images/" + build_number 
command = "mkdir " + folder 
ssh_stdin, ssh_stdout, ssh_stderr = ssh_obj.exec_command(command) 

내가 원격 시스템으로 이동, 폴더가 생성 없어했다 . 마찬가지로, ls 명령의 출력도 읽으려고 시도했습니다. ssh_stdout.read() 할 때 응답이 없습니다.

어디로 잘못 가고 있습니까?

+0

ssh_stderr에 로그가 있는지 확인할 수 있습니까? –

+0

@AlekhyaVemavarapu, 어떻게 확인하나요? –

+0

try command = "mkdir -p"+ – skynyrd

답변

1

paramiko 2.0.2를 사용하는 CentOS 7 서버에서 같은 문제가 발생했습니다. paramiko의 GitHub의 홈페이지에서 예는 처음에 나를 위해 작동하지 않았다 : 나는 위의 예제는 작동하기 시작 원격 시스템을 업데이트 https://github.com/paramiko/paramiko#demo

import paramiko 
client = paramiko.SSHClient() 
client.set_missing_host_key_policy(paramiko.AutoAddPolicy()) 
client.connect(hostname=self.server['host'], username=self.server['username'], password=self.server['password']) 
stdin, stdout, stderr = client.exec_command('ls') 
for line in stdout: 
    print '... ' + line.strip('\n') 
client.close() 

후. 하지만 내가 쓴 코드는 (OP의 코드와 비슷합니다.) 이것은 실행 직후에 stdout 버퍼를 읽을 필요가 있다는 생각을 둡니다. 그래서 코드를 수정하여 작동 시켰습니다. OP 코드를 기반으로 보일 수 있습니다.

ssh_obj = ssh_connect(ip, username, password) 
folder = "/var/xyz/images/" + build_number 
command = "mkdir " + folder 
ssh_stdin, ssh_stdout, ssh_stderr = ssh_obj.exec_command(command) 
# Read the buffer right after the execution: 
ssh_stdout.read() 

흥미로운 점은 나중에 (클라이언트를 닫은 후) 버퍼를 읽음으로써 아무 것도 얻을 수 없다는 것입니다.