将.bat文件转换为打开并将putty会话加载到Python中

问题描述 投票:1回答:1

我有一个.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相关的参考文献,但它并没有帮助我如何传递上述参数。

提前致谢。

python subprocess putty
1个回答
1
投票

您可以使用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)
© www.soinside.com 2019 - 2024. All rights reserved.