2016-06-24 9 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

コマンド= "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() 

ようになります(あなたがクライアントを閉じた後)以降のバッファの読み取りがあなたに何も与えないということです。

関連する問題