我已经成功地让 python 打开了 cmd。但是,使用 runas 管理员在执行 cmd.exe 之前会进行密码检查。
我用它来打开cmd...
import subprocess
subprocess.call(["runas", "/user:Administrator", "cmd.exe"])
我正在寻找一种方法来自动将密码输入到运行代码时打开的 runas.exe 提示符中。假设我要创建var = "test"
并添加它,import subprocess
我将如何制作它以便将该变量传递给 runas.exe 并将其视为 runas.exe 的输入?
该解决方案仅需要 3.4 或更高版本的 python 模块。
我发现一些代码似乎直接输入到 runas.exe 中。然而,明显的输入是\x00\r\n
在代码中输入应该是的时候test
,我相当确定如果我可以获得输入,那么test
代码就会成功。
代码如下:
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)
Run Code Online (Sandbox Code Playgroud) 我正在尝试编写一些代码,这些代码将覆盖其以前的输出,例如原始输出是"1"
,但"1"
被替换为"2"
. 这使得看起来好像"1"
从一开始就没有被输出过。我有一个名为 的列表列表board
,我使用以下代码将此列表转换为多行字符串:
rendered_board = ""
for y in range(board_height):
for x in range(board_width):
rendered_board += board[y][x]
rendered_board += "\n"
Run Code Online (Sandbox Code Playgroud)
我得到了我想要的字符串,但是,我希望能够用新字符串更新该字符串并用它替换旧字符串。
输出示例:
+-------+ +-------+
| | *gets replaced by* | |
| | -------------------> | * |
| | | |
+-------+ +-------+
Run Code Online (Sandbox Code Playgroud)
end="\r"
目前我一直在尝试使用例如来做到这一点
print(rendered_board, end="\r")
update_board()
print(rendered_board, end="\r")
Run Code Online (Sandbox Code Playgroud)
这导致了以下输出:
+-------+
| |
| |
| |
+-------+
+-------+
| |
| * |
| |
+-------+
Run Code Online (Sandbox Code Playgroud)