Skip to main content
 首页 » 编程设计

Python2.7 : ssh. exec_command 没有执行任何命令

2025年04月02日15luoye11

<分区>

我正在使用 paramiko 创建到远程主机的 SSH channel 。但是,当我尝试使用 ssh_object.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() 时没有响应。

我哪里错了?