Skip to content

Instantly share code, notes, and snippets.

@jakelevi1996
Last active February 10, 2023 12:21
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save jakelevi1996/7b29df35ed9ba328c82f9a3941b8fea5 to your computer and use it in GitHub Desktop.
Save jakelevi1996/7b29df35ed9ba328c82f9a3941b8fea5 to your computer and use it in GitHub Desktop.
Automating SSH with Putty (Windows)

Automating SSH with Putty (Windows)

Putty is a tool which can be used for various purposes, including communicating with a Linux computer using SSH (Secure SHell) from a Windows computer. Given the username, hostname, and password of a Linux machine which is to be SSHed into, Putty can be automated from the command line as follows (assuming Putty is on the Windows path):

putty -ssh username@hostname -pw password

If the same user on the same Linux machine is to be accessed repeatedly, this command can be automated in Windows by creating a shortcut as follows:

  • Right click on the desktop/explorer
  • Select New > Shortcut
  • Enter the above putty command into the field labeled "Type the location of the item:"
  • Enter a name for the shortcut (EG "SSH into my-device") into the field labelled "Type a name for this shortcut"
  • This shortcut can now be used to start up this SSH session automatically, without having to open PuTTY and enter the hostname, username and password every time
  • This shortcut can also be added to the Start menu for even easier access, by right-clicking on the shortcut and selecting "Pin to Start"

Automating file transfer with PSCP

PSCP is a tool which can be used to copy files between Linux and Windows computers (similar to scp which transfers files between Linux computers), and is installed by default along with Putty. A file with absolute path /path/to/file can be copied from a Linux machine to a Windows machine in the current directory with the command:

pscp -pw password username@hostname:/path/to/file .

NB the bash command realpath can be used to get the full path to the file on the Linux computer (EG from an SSH terminal in the directory of the file).

Files can be transferred in the opposite direction from a Windows machine to a Linux machine with the syntax:

pscp -pw password file1 file2 file3 username@hostname:target

To recursively copy a directory, use the -r flag:

pscp -r -pw password dir_name/ username@hostname:path/to/parent/dir/dir_name/

Note that if the target directory does not already exist on the remote machine, it can be created using the following command from the local machine (the -p flag is used to create all necessary parent directories) (see below for more information about plink):

plink -ssh -batch -pw password username@hostname mkdir -p path/to/parent/dir/dir_name/

Scripting SSH commands

Instead of opening an SSH terminal, sometimes it is desirable to simply execute a single command on a remote Linux computer from a Windows computer (EG from a script on the Window computer which performs other commands). This can be done using the plink command (which is also installed by default along with Putty):

plink -batch -pw password username@hostname bash_command

The -batch flag is used to automate the command such that it doesn't require user input.

SSHing into an Android phone

By using the app SSHelper on an Android phone it is possible to connect to the phone using SSH via Putty on Windows, as described in this tutorial. In the configuration tab of SSHelper, find the server address (looks like an IP address), and the SSH server port number (EG 2222), and then connect using the following command:

putty -ssh admin@<server address> -P 2222 -pw admin

NB there are alternatives to SSHelper such as SSHDroid, described here and here.

For some reason, the files on the Android (as might be seen by viewing the Android filesystem on Windows through a USB MTP connection) are stored in a folder in the home directory called "SDCard", as shown in the output from the Putty session below:

$ ls
SDCard
$ ls -a SDCard
.                    .photoShare          Audiobooks           Huawei               Notifications        Sounds               com.facebook.orca
..                   ANRSnap              DCIM                 HuaweiSystem         Pictures             WhatsApp             tencent
.PiMusicPlayer       Alarms               Download             Movies               Podcasts             backup
.backups             Android              EditedOnlinePhotos   Music                Ringtones            com.facebook.katana

Once connected via SSH, it is possible to change up a few directories to something which resembles a root directory, as shown in the Putty session below:

$ pwd && ls
/data/data/com.arachnoid.sshelper/files/home
SDCard
$ cd .. && pwd && ls
/data/data/com.arachnoid.sshelper/files
SSHelper.obj  home          usr
$ cd .. && pwd && ls
/data/data/com.arachnoid.sshelper
bin      dev      etc      files    home     lib      libexec  tmp      var

To give another example, the filenames contained in the Music folder can be written to a text file called Phone music.txt on the PC (in whichever folder is open in the terminal window) using the following command:

plink -ssh -P 2222 -pw admin -batch root@<server address> ls SDCard/Music > "Phone music.txt"

After running this command using my phone, the first 15 lines of Phone music.txt are as follows:

1940 - Woody Guthrie - Dust Bowl Ballads
1956 - Ravi Shankar - Three Ragas
1958 - John Coltrane - Blue Train
1959 - Charles Mingus - Mingus Ah Um
1959 - Miles Davis - Kind of Blue
1959 - The Dave Brubeck Quartet - Time Out
1961 - John Coltrane - My favourite things
1963 - Bob Dylan - The Freewheelin' Bob Dylan
1963 - Charles Mingus - The Black Saint and the Sinner Lady
1965 - Bob Dylan - Highway 61 Revisited
1965 - John Coltrane - A Love Supreme
1967 - Leonard Cohen - Songs of Leonard Cohen
1967 - Love - Forever Changes
1967 - The Beatles - Sgt. Pepper's Lonely Hearts Club Band
1967 - The Doors - The Doors

Automating serial connections

A different set of Putty command line options can be used to automatically start serial connections from the command line, EG with the following command:

putty -serial COM4 -sercfg 115200

Explanation:

  • putty specifies using putty
  • -serial specifies opening a serial connection
  • COM4 specifies opening the serial connection on port COM4
  • -sercfg 115200 specifies using a baud rate of 115200

As mentioned above with SSH commands, this command can be automated in a Windows shortcut.

@balajignanasundaram
Copy link

Hi @jakelevi1996, this is super usefull. I'm looking to send a text snippet which is requested in my environment to login. i tried using -m option but it didn't help me. Would you be able to asisst!

@jakelevi1996
Copy link
Author

@balajignanasundaram What is the command you're using, and what is the error message?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment