使用python打开cmd并自动输入密码 [英] Using python to open cmd and automatically enter a password

查看:841
本文介绍了使用python打开cmd并自动输入密码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我设法通过python打开了cmd.但是,在执行cmd.exe之前,使用runas管理员附带密码检查.

I've managed to get the cmd being opened by python. However, using runas administrator comes with a password check before cmd.exe is executed.

我正在使用它来打开cmd ...

I'm using this to open cmd...

import subprocess

subprocess.call(["runas", "/user:Administrator", "cmd.exe"])

我正在寻找一种自动在运行代码时打开的runas.exe提示符中输入密码的方法.假设我要创建var = "test"并将其添加到import subprocess之后,我将如何创建它,以便将此变量传递给runas.exe并视为输入?

I'm looking for a way to automatically enter the password into the runas.exe prompt which opens when i run the code. Say if i were to create var = "test" and add it after import subprocess how would i make it so that this variable is passed to and seen as an input to the runas.exe?

该解决方案仅需要3.4版或更高版本的python模块.

The solution would require only python modules which are in version 3.4 or higher.

我发现一些似乎可以直接输入到runas.exe的代码.但是,当在代码中输入应该为test时,明显的输入为\x00\r\n我可以肯定的是,如果我可以将输入设为test,则代码将成功.

I have found some code which appears to input straight into runas.exe. However, the apparent input is \x00\r\n when in the code the input is supposed to be test I am fairly certain that if i can get the input to be test then the code will be successful.

代码如下:

import subprocess

args = ['runas', '/user:Administrator', 'cmd.exe']

proc = subprocess.Popen(args, 
                        stdin=subprocess.PIPE, 
                        stdout=subprocess.PIPE, 
                        stderr=subprocess.PIPE)

proc.stdin.write(b'test\n')
proc.stdin.flush()

stdout, stderr = proc.communicate()
print (stdout)
print (stderr)

推荐答案

虽然不能回答您的问题,但这可以解决您的问题.使用 psexec 代替runas.您可以这样运行它:

Although not an answer to your question, this can be a solution to your problem. Use psexec instead of runas. You can run it like this:

psexec -u user -p password cmd

(或使用subprocess.Popen或其他方式从Python运行)

(or run it from Python using subprocess.Popen or something else)

这篇关于使用python打开cmd并自动输入密码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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