【问题标题】:How to provide a password over ssh using python script?如何使用 python 脚本通过 ssh 提供密码?
【发布时间】:2019-07-05 08:15:58
【问题描述】:

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")

【问题讨论】:

    标签: python


    【解决方案1】:

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

    【讨论】:

      【解决方案2】:

      这是一个允许您使用 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
      

      【讨论】:

      • 好像你正在超时。 ssh 命令是否在 BASH 中工作?
      最近更新 更多