Automate remote Linux commands from Windows terminal using Plink.

Unlike putty, which creates an interactive SSH session on Linux or UNIX server, plink accepts a non-interactive SSH session. Using Plink, we can automate remote Linux commands from a terminal window.

How to automate remote Linux commands from a terminal window.

1.Download plink

To automate remote Linux commands from a terminal window, you must first download the Plink utility according to your server architecture (32 bit or 64 bit).

2. Set the PATH variable

Once you have downloaded Plink, you cannot simply execute Plink as we do with putty. You need to set the PATH variable in the Windows terminal so that it can be called from anywhere. To do this, follow the instructions below.

  • On the desktop, right-click the My Computer icon.
  • Select Properties from the context menu.
  • Click Advanced Communications Settings.
  • Click Environment Variables.
  • Edit System Variables (or New System Variable) Check your PATH environment variable and add the location where you downloaded Plink. In my case, we downloaded the path “C: automation “. Therefore, we added at the end of the line “; C: automation “for the PATH variable as shown below:

3. Basic syntax of the Plink command line utility.

plink [options] connection [command]

In the above syntax:

  • options – various Plink options.
  • connection – This will contain the connection information of the Linux server you want to connect to.
  • command is a command to be executed on a remote Linux server. It is optional.

4. The final step to automate remote Linux from terminal windows

For demonstration purposes, we took the task of automating the setting of a password for the user “andreyex” on two servers (you can take it to any number of servers according to your requirements).

Server details IP:
 
192.168.226.121
192.168.226.212
 
Command to be used for setting an password for user mann
 
echo linuxpassword | passwd --stdin andreyex
 
The above command will set "linuxpassword" as a password for use

Now create one text file “server_list.txt” in the C: automation directory that contains the IP addresses of the servers so that the servers can be connected.

Now change the directory to c: automation and run the following command from a command prompt window:

FOR /F "tokens=1,2* delims=," %G IN (C:automationserver_list.txt) DO plink -ssh [email protected]%G -pw root321 ("echo linuxpassword | passwd --stdin andreyex")

In the command above, we used the root user, passing in the -ssh parameter & root321 is the password for the root user specified by the -pw parameter. In the example above, kindly enclose the Linux command in double quotes.

Executing logs:

c:automation>FOR /F "tokens=1,2* delims=," %G IN (C:automationserver_list.txt
) DO plink -ssh [email protected]%G -pw root321 ("echo linuxpassword | passwd --stdin andreyex")
 
 
c:automation>plink -ssh [email protected] -pw root321 ("echo linuxpassword |
passwd --stdin andreyex")
Changing password for user andreyex.
passwd: all authentication tokens updated successfully.
 
c:automation>plink -ssh [email protected] -pw root321 ("echo linuxpassword |
passwd --stdin andreyex")
Changing password for user andreyex.
passwd: all authentication tokens updated successfully.
 
c:automation>

This is how we can automate remote Linux commands from a terminal window.

Sidebar