If you use both 'Nix and Windows systems, you may be interested in running scripts on the server without having to manually ssh all the time from your workstation. Previously I have looked at remotely booting a server using Wake-on-LAN and remote server shutdown. This article covers running scripts on a remote server, using Windows batch files.
In the above illustration a number of batch files have been added as Start menu shortcuts, to perform various functions on a remote FreeBSD Unix server. These batch files use a third-party utility called Plink (PuTTY-link) which harnesses the power of ssh without needing to log in and run scripts manually. Plink was developed by the same team who created the awesome PuTTY client and can be downloaded, together with other useful utilities from Simon Tatham's website.
Each function requires a pair of files on the Windows box: a batch file and a text file, plus the plink.exe executable. Firstly the batch file invokes plink to perform the ssh log-in, which in turn runs the command(s) in the text file on the remote server. Here's an example..
The batch file, gothwrite.bat:
f:\progs\plink -ssh andym@goth -pw mypassword -m f:\progs\gothwrite.txt
The text file, gothwrite.txt invokes a shell script which is sitting on the server:
To keep things simple I use the same name for the batch and text file pair (in this case, gothwrite) and they all live together in the \progs directory, which is in the system path on my Windows box.
The writesomething.sh sitting on the remote server has been designed to perform some function and has been tested on the server before running it remotely. Depending on server permissions, the script may also need sudo to work properly.
The full set of plink command switches can be seen by simply typing plink in a command prompt window or going to the Plink command options page.
AndyM | Updated July 2019