# Create a batch file or shortcut to PuTTY (ssh) that opens a session and runs a command

I often find myself opening an SSH session to run the same single command. I have everything setup to login without entering a password (Via SSH Key-Based Auth), so I wondered if there was a way to create a shortcut or a batch file in Windows that would load PuTTY or a similar program, then fire off that command (and likely exit if result is good).

Use the commandline PuTTY version plink.exe to initate a SSH connection to a host of choice. Use the -ssh switch to connect with SSH. With the -m switch you can include a command file:

plink.exe -ssh host1 -m C:\path\to\commands.txt


You can download plink.exe from here.

Last step would be to create a shortcut including plink.exe with the desired parameters.

Check out the Plink documentation for other various parameters: Plink.exe documentation

• No need to use -m switch with Plink, as it allow to specify command directly on its command-line, see my answer. – Martin Prikryl Dec 19 '17 at 15:15
• I am choosing this answer as I prefer using -ssh host1 which relates to putty Profile vs user@host – FreeSoftwareServers Dec 21 '17 at 12:39
• @FreeSoftwareServers There's no difference to PuTTY in this respect. You can do plink site command the same way as with PuTTY (except for command). Plink and PuTTY has basically the same set of command-line options. – Martin Prikryl Dec 28 '17 at 8:02

To automate a command execution, use Plink (from PuTTY package), not PuTTY itself.

Plink accepts a command on its command line:

plink.exe user@host command


If you want to keep using PuTTY, you can use -m switch to specify a command file (Plink supports the -m switch too).

• When I said I had no password, I should have specified I meant with SSH Key-based Auth, but I figured it would be assumed. I don't think this would work as it is, it would need parameters added on like -i key or -pw password – FreeSoftwareServers Dec 21 '17 at 12:38
• @FreeSoftwareServers There's no difference to PuTTY in this respect. You can do plink site command the same way as with PuTTY (except for command). Plink and PuTTY has basically the same set of command-line options. – Martin Prikryl Dec 21 '17 at 12:52
• your answer did not include the options to use SSH Key Based Auth aka PWDless login, while the correct flags exist, i found them in the man pages for plink which was included in MrPowerUsers answer, I understand both could work but MrPower users answer fit my question better and linked the man pages as well – FreeSoftwareServers Dec 28 '17 at 8:06

You can use putty configurations to achieve this.

Enter the remote command that you'd like to run here:

Then, before clicking "Open", go back to the "Session" tab (at the top), and save your configuration.

Now, create a shortcut to putty.exe, adding the -load flag, for example:

%PATH_TO_PUTTY%\putty.exe -load my_config


Now, you can just click on the shortcut and it will load your session, executing your command.

• Looks awesome, ill test tomorrow, i suppose i will just execute a script and it can end the session based on results of script – FreeSoftwareServers Dec 19 '17 at 12:45
• Yes, the session will end when the command terminates. If you'd like PuTTY to hang around once done (e.g: to inspect the output), then set "Close window on exit" in the "Session" tab. – Attie Dec 19 '17 at 14:09

If you're using Windows 10, you might be interested to know that Microsoft reportedly has a beta of OpenSSH (client and server):

[…] go to “Manage Optional Features” then + “Add a feature”. You can then scroll down the list and find the OpenSSH Client (Beta) and OpenSSH Server (Beta) features in Windows. The idea of running another remote service on Windows can be daunting so we do not blame you if you do not want to install the server.

Once installed, you can simply fire up your command line and use the OpenSSH client by typing ssh followed by the command such as ssh ubuntu@someIP.

Here is the current (as of November 28, 2017) ssh command usage guide we are getting with the Windows OpenSSH Client:

C:\WINDOWS\system32>ssh
usage: ssh [-46AaCfGgKkMNnqsTtVvXxYy] [-b bind_address] [-c cipher_spec]
[-D [bind_address:]port] [-E log_file] [-e escape_char]
[-F configfile] [-I pkcs11] [-i identity_file]
[-J [user@]host[:port]] [-L address] [-l login_name] [-m mac_spec]
[-O ctl_cmd] [-o option] [-p port] [-Q query_option] [-R address]
[-S ctl_path] [-W host:port] [-w local_tun[:remote_tun]]
[user@]hostname [command]
C:\WINDOWS\system32>


If this works, you should be able to use ssh like you would on a *Nix:

C:\WINDOWS\system32>ssh user@host "ls -l ~"

• Thats really cool to know, but I love SuperPuTTY. I will keep an eye on Windows OpenSSH as well. – FreeSoftwareServers Dec 21 '17 at 12:18

You can also look at software such as mRemoteNG, MOBAxTerm or SecureCRT which will manage your SSH connections for you, this also links with the saved PuTTY sessions so you can apply a template to the session.

This is the final "CMD" which I can just save on my FileServer and create a shortcut on my Desktop.

::FreeSoftwareServers
::Automated Opening of SSH Tunnel & Execute CMD on Remote Host
::https://superuser.com/questions/1278434/create-a-batch-file-or-shortcut-to-putty-ssh-that-opens-a-session-and-runs-a-c

set puttydir="C:\Program Files\PuTTY"