转换 .bat 文件以打开 putty 会话并将其加载到 Python
Conversion of .bat file to open and load putty session into Python
我有一个 .bat
文件,它接受一些参数并使用 python 连接到 putty。以下代码供参考。
pushd c:
start /min Putty.exe -load SessionName -l UserName -pw Password
我正在使用 os.system
在 python 中调用 putty1.bat
文件,如下所述:
os.system('putty1.bat')
我看过一些与 subprocess
相关的参考资料,但它并没有帮助我了解如何传递上述参数。
提前致谢。
您可以使用 Plink
,这是一个命令行应用程序。 here more info
import subprocess
sp = subprocess.Popen(['plink', '-ssh', '-l', 'username', '-pw', 'password', 'SessionName'], \
shell = False, stdin=subprocess.PIPE, stdout=subprocess.PIPE)
sp.communicate('lmstat -a\nexit\n'.encode())
或尝试 paramiko
import paramiko
import socket
class Point:
def __init__(self,host,username,password,port):
self.host = host
self.username = username
self.password = password
self.port = port
def connect(self):
"""Login to the remote server"""
print("Establishing ssh connection")
self.client = paramiko.SSHClient()
self.client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
# Connect to the server
self.client.connect(hostname=self.host, port=self.port, username=self.username, password=self.password,
timeout=1000, allow_agent=False, look_for_keys=False)
print("Connected to the server", self.host)
我有一个 .bat
文件,它接受一些参数并使用 python 连接到 putty。以下代码供参考。
pushd c:
start /min Putty.exe -load SessionName -l UserName -pw Password
我正在使用 os.system
在 python 中调用 putty1.bat
文件,如下所述:
os.system('putty1.bat')
我看过一些与 subprocess
相关的参考资料,但它并没有帮助我了解如何传递上述参数。
提前致谢。
您可以使用 Plink
,这是一个命令行应用程序。 here more info
import subprocess
sp = subprocess.Popen(['plink', '-ssh', '-l', 'username', '-pw', 'password', 'SessionName'], \
shell = False, stdin=subprocess.PIPE, stdout=subprocess.PIPE)
sp.communicate('lmstat -a\nexit\n'.encode())
或尝试 paramiko
import paramiko
import socket
class Point:
def __init__(self,host,username,password,port):
self.host = host
self.username = username
self.password = password
self.port = port
def connect(self):
"""Login to the remote server"""
print("Establishing ssh connection")
self.client = paramiko.SSHClient()
self.client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
# Connect to the server
self.client.connect(hostname=self.host, port=self.port, username=self.username, password=self.password,
timeout=1000, allow_agent=False, look_for_keys=False)
print("Connected to the server", self.host)