Search code examples
pythonsubprocessputty

Conversion of .bat file to open and load putty session into Python


I have a .bat file which takes some arguments and connect to putty using python. Below is code for reference.

pushd c:
start /min Putty.exe -load SessionName -l UserName -pw Password

I am calling putty1.bat file in python using os.system as mentioned below:

os.system('putty1.bat')

I have seen some reference related to subprocess but it's not helping me out how to pass above mentioned parameters.

Thanks in advance.


Solution

  • You can use Plink which is a command line application. 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())
    

    OR try with 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)