我正在编写一个脚本,该脚本需要能够打开一个单独的控制台窗口并对其进行读写(在Windows上)。我想在新控制台中使用telnet和ssh,并将主控制台用作管理器
现在我正在尝试一些事情:
主文件
import subprocess as sp
from getpass import getpass
import telnetlib
import multiprocessing as mp
import os
def run(command):
cmd = sp.Popen(command.split(), sp.PIPE)
com, err = cmd.communicate()
print(com,err)
if __name__ == "__main__":
login = input("Podaj login: ")
password = getpass()
ip = "10.74.119.252"
command = f"python LogInConsol.py {ip} {login} {password}"
process = mp.Process(target=run, args= (command,))
process.start()
LogInConsol文件
import telnetlib
import subprocess as sb
import sys
argv = sys.argv
ip = argv[1]
login = argv[2]
pw = argv[3]
# tn = telnetlib.Telnet(host=ip, port= 23)
# tn.read_until(b"login: ")
command = f"cmd.exe start /k telnet {ip}"
cmd = sb.run(command.split(),sb.PIPE)
com, err = cmd.communicate()
但是这种行为使得一切都发生在主控制台窗口(我从中启动程序)
UPDATE
这开始按我的意愿工作。
Main.py
from subprocess import Popen, PIPE, CREATE_NEW_CONSOLE
from getpass import getpass
import telnetlib
import multiprocessing as mp
import threading as th
def run(command):
cmd = Popen(command, PIPE, creationflags=CREATE_NEW_CONSOLE)
com, err = cmd.communicate()
print(com,err)
if __name__ == "__main__":
mp.freeze_support()
login = input("Podaj login: ")
password = getpass()
ip = "10.10.10.10"
command = f"python LogInConsol.py {ip} {login} {password}"
process = mp.Process(target=run, args= (command,))
process.start()
input("Wait ")
LogInConsol.py
import telnetlib
import subprocess as sp
import sys
argv = sys.argv
ip = argv[1]
login = argv[2]
pw = argv[3]
print(argv)
tn = telnetlib.Telnet(host=ip,port=23)
red = tn.read_until(b"username: ")
print(red)
答案 0 :(得分:1)
可以尝试wshshell:
1. pip install pywin32-221-cp36-cp36m-win_amd64.whl
2. python.exe pywin32_postinstall.py -install (DOS command line)
在脚本中
import win32com.client
WshShell = win32com.client.Dispatch("WScript.Shell")
WshShell.run("cmd")
答案 1 :(得分:1)
这可能是XY problem的情况吗?您需要通过telnet或ssh进行连接,但是您真的需要介于两者之间的该控制台吗?