如何使用python脚本通过ssh提供密码?

时间:2019-07-05 08:15:58

标签: python

sshpass -P mysshpass ssh root@127.0.0.1“ ./myscript.py”

上面是我从shell执行的命令,由于脚本“ myscript.py”要求输入密码,因此要求我输入密码。但是,当我从python执行相同的命令时,它不会提示我输入密码。

我的python代码

os.system(sshpass -P mysshpass ssh root@127.0.0.1“ ./myscript.py”)

2 个答案:

答案 0 :(得分:0)

您可以考虑使用paramiko模块,或者通过使用ssh -i指定密钥文件(rsa私钥)(如果您对ssh auth不熟悉,请参阅authorized_keys

答案 1 :(得分:0)

这是一个允许您使用pexpect获得所请求结果的功能:

import pexpect

def ssh(host, cmd, user, password, timeout=30, bg_run=False):                                                                                                 
    """SSH'es to a host using the supplied credentials and executes a command.                                                                                                 
    Throws an exception if the command doesn't return 0.                                                                                                                       
    bgrun: run command in the background"""                                                                                                                                    

    fname = tempfile.mktemp()                                                                                                                                                  
    fout = open(fname, 'w')                                                                                                                                                    

    options = '-q -oStrictHostKeyChecking=no -oUserKnownHostsFile=/dev/null -oPubkeyAuthentication=no'                                                                         
    if bg_run:                                                                                                                                                         
        options += ' -f'                                                                                                                                                       
    ssh_cmd = 'ssh %s@%s %s "%s"' % (user, host, options, cmd)                                                                                                                 
    child = pexpect.spawn(ssh_cmd, timeout=timeout)                                                                                                                            
    child.expect(['password: '])                                                                                                                                                                                                                                                                                               
    child.sendline(password)                                                                                                                                                   
    child.logfile = fout                                                                                                                                                       
    child.expect(pexpect.EOF)                                                                                                                                                  
    child.close()                                                                                                                                                              
    fout.close()                                                                                                                                                               

    fin = open(fname, 'r')                                                                                                                                                     
    stdout = fin.read()                                                                                                                                                        
    fin.close()                                                                                                                                                                

    if 0 != child.exitstatus:                                                                                                                                                  
        raise Exception(stdout)                                                                                                                                                

    return stdout