使用subprocess.Popen通过SSH或SCP发送密码

问题描述 投票:9回答:6

我正在尝试使用scp运行subprocess.Popen(安全复制)命令。登录要求我发送密码:

from subprocess import Popen, PIPE

proc = Popen(['scp', "[email protected]:/foo/bar/somefile.txt", "."], stdin = PIPE)
proc.stdin.write(b'mypassword')
proc.stdin.flush()

这会立即返回错误:

[email protected]'s password:
Permission denied, please try again.

我确定密码是正确的。我通过在shell上手动调用scp轻松验证它。那么为什么这不起作用呢?

请注意,有许多类似的问题,询问subprocess.Popen并发送自动SSH或FTP登录密码:

How can I set a users password in linux from a python script? Use subprocess to send a password

这些问题的答案不起作用和/或不适用,因为我使用的是Python 3。

python linux ssh python-3.x subprocess
6个回答
6
投票

您链接的第二个答案表明您使用Pexpect(这通常是与期望输入的命令行程序交互的正确方法)。有一个fork它适用于python3,你可以使用。


10
投票

这是使用ssh密码的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

使用scp应该可以做类似的事情。


6
投票

OpenSSH scp实用程序调用ssh程序与远程主机建立SSH连接,ssh进程处理身份验证。 ssh实用程序不接受命令行或其标准输入上的密码。我相信这是OpenSSH开发人员的一个深思熟虑的决定,因为他们认为人们应该使用更安全的机制,如基于密钥的身份验证。任何调用ssh的解决方案都将遵循以下方法之一:

  1. 使用SSH key进行身份验证,而不是密码。
  2. 使用sshpassexpect或类似工具自动响应密码提示。
  3. 使用(滥用)SSH_ASKPASS功能通过调用另一个命令sshhere或某些答案here来获取here来获取密码。
  4. 让SSH服务器管理员启用host-based authentication并使用它。请注意,基于主机的身份验证仅适用于某些网络环境。请参阅附加说明herehere
  5. 使用perl,python,java或您喜欢的语言编写自己的ssh客户端。大多数现代编程语言都有ssh客户端库,您可以完全控制客户端获取密码的方式。
  6. 下载ssh source code并构建一个可以按照您想要的方式运行的ssh的修改版本。
  7. 使用不同的ssh客户端。有other ssh clients可用,包括免费和商业。其中一个可能比OpenSSH客户端更适合您的需求。

在这种特殊情况下,鉴于您已经从python脚本调用scp,似乎其中一个是最合理的方法:

  1. 使用python expect模块pexpect来调用scp并将密码提供给它。
  2. 使用python ssh实现的paramiko来执行此ssh任务,而不是调用外部程序。

4
投票

Pexpect有一个完整的库:pxssh

http://pexpect.readthedocs.org/en/stable/api/pxssh.html

import pxssh
import getpass
try:
    s = pxssh.pxssh()
    hostname = raw_input('hostname: ')
    username = raw_input('username: ')
    password = getpass.getpass('password: ')
    s.login(hostname, username, password)
    s.sendline('uptime')   # run a command
    s.prompt()             # match the prompt
    print(s.before)        # print everything before the prompt. 
    s.logout()
except pxssh.ExceptionPxssh as e:
    print("pxssh failed on login.")
    print(e)

1
投票

我猜一些应用程序使用stdin与用户交互,一些应用程序使用终端进行交互。在这种情况下,当我们使用PIPE编写密码时,我们正在写入stdin。但SCP应用程序从终端读取密码。由于子进程无法与使用终端的用户交互,但只能使用stdin进行交互,我们无法使用子进程模块,我们必须使用pexpect来使用scp复制文件。

随意更正。


0
投票

这是我的基于pexpect的scp函数。除密码外,它还可以处理通配符(即多文件传输)。要处理多个文件传输(即通配符),我们需要通过shell发出命令。请参阅pexpect FAQ

import pexpect

def scp(src,user2,host2,tgt,pwd,opts='',timeout=30):
    ''' Performs the scp command. Transfers file(s) from local host to remote host '''
    cmd = f'''/bin/bash -c "scp {opts} {src} {user2}@{host2}:{tgt}"'''
    print("Executing the following cmd:",cmd,sep='\n')

    tmpFl = '/tmp/scp.log'
    fp = open(tmpFl,'wb')
    childP = pexpect.spawn(cmd,timeout=timeout)
    try:
        childP.sendline(cmd)
        childP.expect([f"{user2}@{host2}'s password:"])
        childP.sendline(pwd)
        childP.logfile = fp
        childP.expect(pexpect.EOF)
        childP.close()
        fp.close()

        fp = open(tmpFl,'r')
        stdout = fp.read()
        fp.close()

        if childP.exitstatus != 0:
            raise Exception(stdout)
    except KeyboardInterrupt:
        childP.close()
        fp.close()
        return

    print(stdout)

它可以这样使用:

params = {
    'src': '/home/src/*.txt',
    'user2': 'userName',
    'host2': '192.168.1.300',
    'tgt': '/home/userName/',
    'pwd': myPwd(),
    'opts': '',
}

scp(**params)
© www.soinside.com 2019 - 2024. All rights reserved.