如何给子进程一个密码并同时获得标准输出 [英] how to give subprocess a password and get stdout at the same time

查看:19
本文介绍了如何给子进程一个密码并同时获得标准输出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试检查远程机器上是否存在可执行文件,然后运行该可执行文件.为此,我使用子进程运行 ssh <host>ls ,如果成功,运行 ssh ;<文件>.当然,ssh 要求输入密码,我想自动提供密码.另外,我想从 ls 获取返回码,并通过运行命令获取 stdout 和 stderr.

I'm trying to check for the existence of an executable on a remote machine, then run said executable. To do so I'm using subprocess to run ssh <host> ls <file>, and if that's successful, run ssh <host> <file>. ssh asks for a password, of course, and I'd like to provide that automatically. Also, I'd like to get the returncode from ls, and stdout and stderr from running the command.

所以我知道需要 communicate() 方法,以避免死锁,但我无法让 Popen(stdin) 识别密码.此外,我使用的是 Python 2.4.3,并坚持使用该版本.这是我目前得到的代码:

So I know the communicate() method is needed, to avoid deadlocks, but I can't get the password to be recognized by Popen(stdin). Also I'm using Python 2.4.3, and stuck on that version. Here's the code I've got so far:

import os  
import subprocess as sb  

def WallHost(args):  
    #passwd = getpass.getpass()  
    passwd = "password"  
    for host in args:   

        # ssh to the machine and verify that the script is in /usr/bin   
        sshLsResult = sb.Popen(["ssh", host, "ls", "/usr/bin/wall"], stdin=sb.PIPE, stderr=sb.PIPE, stdout=sb.PIPE)  
        (sshLsStdout, sshLsStderr) = sshLsResult.communicate(input=passwd)  
        sshResult = sshLsResult.returncode  

        if sshResult != 0:                      
            raise "wall is not installed on %s. Please check." % host  
        else:  
            sshWallResult = sb.Popen(["ssh", host, "/usr/bin/wall", "hello world"], stdin=sb.PIPE, stderr=sb.PIPE, stdout=sb.PIPE)  
            (sshWallStdout, sshWallStderr) = sshWallResult.communicate(input=passwd)  
            print "sshStdout for wall is \n%s\nsshStderr is \n\n" % (sshWallStdout, sshWallStderr)  

args = ["127.0.0.1", "192.168.0.1", "10.10.265.1"]  
WallHost(args)  

<小时>

感谢任何帮助接受该密码的过程.或者,如果您有更好的方法来检查可执行文件,然后在远程主机上运行它.;)


Any help getting the process to accept that password is appreciated. Or if you've got a better way to check for the executable and then run it on a remote host. ;)

谢谢安东尼

推荐答案

如何使用 authorized_keys.然后,您无需输入密码.

How about using authorized_keys. Then, you don't need to input password.

你也可以走硬路(只适用于 Linux):

You can also go hard way (only work in Linux):

import os
import pty

def wall(host, pw):
    pid, fd = pty.fork()
    if pid == 0: # Child
        os.execvp('ssh', ['ssh', host, 'ls', '/usr/bin/wall'])
        os._exit(1) # fail to execv

    # read '..... password:', write password
    os.read(fd, 1024)
    os.write(fd, pw + '\n')

    result = []
    while True:
        try:
            data = os.read(fd, 1024)
        except OSError:
            break
        if not data:
            break
        result.append(data)
    pid, status = os.waitpid(pid, 0)
    return status, ''.join(result)

status, output = wall('localhost', "secret")
print status
print output

http://docs.python.org/2/library/pty.html

这篇关于如何给子进程一个密码并同时获得标准输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆