我有一个.bat
文件,它接受一些参数并使用python连接到putty。以下是供参考的代码。
pushd c:
start /min Putty.exe -load SessionName -l UserName -pw Password
我使用putty1.bat
在python中调用os.system
文件,如下所述:
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)