Search code examples
windowsbatch-filesshwindows-10putty

Windows batch file to login to Putty saved ssh session and respond to series of prompts


Everyday I have to manually login to an ssh session through Putty and select specific options in order to run a program that sends out an email (about an hour later). I want to automate this with a batch program that can be run daily using Windows Task Manager but am not very experienced with these types of scripts. Is this possible to make work?

Here is what I have tried:

I have two batch files - myscript.bat and start_input.bat. I want to automatically execute start_input.bat to run the full program and have the email sent from the remote server I'm logging into.

myscript.bat

cd C:\Program Files\PuTTY\
plink "offsite_server"

start_input.bat

(echo.&echo myusername&echo mypassword&echo c&echo f&echo o&echo c&echo y&echo  &echo q&echo q&echo q&echo q) | myscript.bat

When I do this manually I login to the session on Putty and then enter a series of inputs to complete the process: press return type "username", press enter type "password", press enter type "c" type "f" type "o" type "c" type "y" press space bar type "q" type "q" type "q" type "q"


Solution

  • Windows Task Manager alone can't handle this level of automation. Batch files are also not inherently part of the Windows Task Manager, but can be scheduled to run using it. If the primary aim is to automate a PuTTY login, plink really offers a more straightforward method. You could create a batch file like this:

    login.bat

    C:\Program Files\PuTTY\plink.exe -ssh offsite_server -l myusername -pw mypassword -m C:\path\to\commands.txt
    

    The sequence of commands you mentioned seems tied to the server's own interface rather than to PuTTY, so this may not work. A static batch script may not suffice for this. For a more dynamic solution, you could either use a Python script or VBScript to automate the keystrokes. Assuming you want to keep the built-in steadiness of Windows, VBScript, although poorer, may suffice.

    script.vbs

    Set objShell = CreateObject("WScript.Shell")
    
    ' Run Putty and connect to the server
    objShell.Run "C:\Program Files\PuTTY\putty.exe -ssh offsite_server -l myusername -pw mypassword"
    
    ' Wait for Putty to launch
    WScript.Sleep 5000
    
    ' Send your sequence of commands
    objShell.SendKeys "c{ENTER}"
    WScript.Sleep 100
    objShell.SendKeys "f{ENTER}"
    ' ... (continue for the rest of your commands)
    
    ' Close Putty
    objShell.SendKeys "%{F4}"
    

    Python, with its robust libraries, can handle SSH connections effectively, while also offering a more versatile environment for what you are asking. The paramiko package is especially useful for this:

    import paramiko
    
    # Initialize SSH client
    ssh = paramiko.SSHClient()
    ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
    
    # Connect to the server
    ssh.connect('offsite_server', username='myusername', password='mypassword')
    
    # Run commands
    stdin, stdout, stderr = ssh.exec_command('c; f; o; c; y; q; q; q; q')
    
    # Close connection
    ssh.close()
    

    You can schedule any of these scripts to run automatically using the Windows Task Scheduler. Keep in mind that these are basic examples and may require further adaptation based on your server and network setup, as the problem is entirely there.

    Hope it helps.