X

Automate remote linux commands from Windows terminal using plink.


Unlike putty which creates interactive ssh session to Linux or UNIX server, plink takes non-interactive ssh session. Using Plink we can automate remote linux commands from windows terminal.


How to automate remote linux commands from windows terminal.

1.Download plink from the web

In order to automate remote linux commands from windows terminal , you need to first download the plink utility from below URL as per your architecture (32 bit or 64 Bit).

plink.exe (a command-line interface to the PuTTY back ends)

 

2. Set the PATH variable

Once you have done with downloaded plink you cannot just go execute the plink like we do for putty. You need to set PATH variable on Windows terminal so that it can called from any location. To do so follow below procedure.

  • From the desktop, right click the Computer icon.
  • Choose Properties from the context menu.
  • Click the Advanced system settings link.
  • Click Environment Variables.
  • In the Edit System Variable (or New System Variable) window, Check for PATH environment variable and append the location where you have downloaded the plink. In my case I have downloaded at location “c:\automation\”. Hence I have appended “;c:\automation\” string at the last for PATH variable as below:

3. Basic syntax for plink command utility.

plink [options] connection [command]

In the above syntax:

  • options – various options to plink.
  • connection – This will have the connection information of the Linux server that you want to connect to.
  • command – This is the command that should be executed on the remote Linux server. This is optional.

4. Final step to automate remote linux commands from windows terminal

For the demo purpose we have taken automation task of setting up an password for user “mann” on two servers(you can take it to any number of servers as per your requirement).

Server details IP:

192.168.216.134
192.168.216.135

Command to be used for setting an password for user mann

echo linuxpassword | passwd --stdin mann

The above command will set "linuxpassword" as a password for user mann.

Now create one txt file “server_list.txt”under c:\automation folder which contain servers ip so that servers can be connected as below:

Now change directory to c:\automation and execute below command from windows command prompt:

FOR /F "tokens=1,2* delims=," %G IN (C:\automation\server_list.txt) DO plink -ssh root@%G -pw root123 ("echo linuxpassword | passwd --stdin mann")

In above command we have used user root by passing -ssh parameter & root123 is an password for root user indicated by -pw parameter. In the above example kindly enclose your Linux command within double quotes in bracket.

Execution logs:

c:\automation>FOR /F "tokens=1,2* delims=," %G IN (C:\automation\server_list.txt
) DO plink -ssh root@%G -pw root123 ("echo linuxpassword | passwd --stdin mann")


c:\automation>plink -ssh root@192.168.216.134 -pw root123 ("echo linuxpassword |
 passwd --stdin mann")
Changing password for user mann.
passwd: all authentication tokens updated successfully.

c:\automation>plink -ssh root@192.168.216.135 -pw root123 ("echo linuxpassword |
 passwd --stdin mann")
Changing password for user mann.
passwd: all authentication tokens updated successfully.

c:\automation>

This is how we can automate remote linux commands from windows terminal.

View Comments (1)

Related Post