Run commands on remote server using plink

As an engineer, you might come across some task that need to run commands on remote machine. This post will show you how to use plink to run command on remote machine.

PuTTY Link (Plink), a command-line utility for secure remote access. You can find the software here. If you have installed full version of the famous tool Putty, you probably already have it installed. Just open a command prompt and type “plink” to check if plink is already in the system.

Syntax is quite simple as shown below.
The batch flag is used to avoid any interactive prompts that may accrue when logging to remote system.

plink -batch -ssh TargetIP -l Username -pw Password Command

You can also run multiple commands, you need to put all your commands in a text file. The format will be one command per line. Then use the syntax shown below to run multiple commands.

plink -batch -ssh TargetIP -l Username -pw Password -m Commands.txt


Plink is a command line utility so typically data flow redirection works perfectly.
However, this cause another issue, you do not see any console output. You won’t know if there is something wrong or if the script is stuck on any time. Luckily, we can use tee to have both console output and log file. Syntax is shown as below.
Tee only works in powershell.

plink -batch -ssh TargetIP -l Username -pw Password Command | tee LogName.log

Leave a comment

Your email address will not be published. Required fields are marked *