I have a scenario where I need to run a linux shell command frequently (with different filenames) from windows. I am using PuTTY and WinSCP to do that (requires login name and password). The file is copied to a predefined folder in the linux machine through WinSCP and then the command is run from PuTTY. Is there a way by which I can automate this through a program. Ideally I would like to right click the file from windows and issue the command which would copy the file to remote machine and run the predefined command (in PuTTy) with the filename as argument.
9 Answers
Putty usually comes with the "plink" utility.
This is essentially the "ssh" command line command implemented as a windows .exe.
It pretty well documented in the putty manual under "Using the command line tool plink".
You just need to wrap a command like:
plink root@myserver /etc/backups/do-backup.sh
in a .bat script.
You can also use common shell constructs, like semicolons to execute multiple commands. e.g:
plink read@myhost ls -lrt /home/read/files;/etc/backups/do-backup.sh
-
5This is the answer. In addition you can use pageant to allow the key to be loaded and used so there is no need to enter the key's passphrase each time. Keep in mind that due to Windows's architecture there is no way to prevent the unencrypted key from being written out to a disk swap file. You can reduce this risk by removing your key from pageant if you know it will not be used for a while.– DDayCommented May 8, 2015 at 15:15
-
2plink -i file.ppk username@hostname path/to/directory/file.sh to execute a script on EC2 from windows/unix via command line Commented Sep 14, 2016 at 17:12
There could be security issues with common methods for auto-login. One of the most easiest ways is documented below:
And as for the part the executes the command In putty UI, Connection>SSH> there's a field for remote command.
4.17 The SSH panel
The SSH panel allows you to configure options that only apply to SSH sessions.
4.17.1 Executing a specific command on the server
In SSH, you don't have to run a general shell session on the server. Instead, you can choose to run a single specific command (such as a mail user agent, for example). If you want to do this, enter the command in the "Remote command" box. http://the.earth.li/~sgtatham/putty/0.53/htmldoc/Chapter4.html
in short, your answers might just as well be similar to the text below:
-
6Ok, so I can use
putty.exe -ssh -2 -l username -pw password -m c:remote.cmd hostname
. Thanks a lot. Now I need to figure out command to copy the file and give arguments to remote.cmd– NemoCommented May 27, 2011 at 3:23 -
-
The command-line options are documented in the PuTTY User Manual, which can be accessed by clicking the Help button, in 3 Using PuTTY > 3.8 The PuTTY command line > 3.8.3 Standard command-line options.– XP1Commented Oct 22, 2013 at 12:05
-
4I down voted because this answer does not answer the question asked. It's a bunch of words, then a link which only kind of answers the question. There's other answers here which are drastically shorter and provide an answer. Commented Feb 13, 2015 at 19:05
-
You can write a TCL script and establish SSH session to that Linux machine and issue commands automatically. Check http://wiki.tcl.tk/11542 for a short tutorial.
You can create a putty session, and auto load the script on the server, when starting the session:
putty -load "sessionName"
At remote command, point to the remote script.
-
What is the argument to add the remote command per commandline ? Commented Jun 19, 2020 at 13:48
You can do both tasks (the upload and the command execution) using WinSCP. Use WinSCP script like:
option batch abort
option confirm off
open your_session
put %1%
call script.sh
exit
Reference for the call
command:
https://winscp.net/eng/docs/scriptcommand_call
Reference for the %1%
syntax:
https://winscp.net/eng/docs/scripting#syntax
You can then run the script like:
winscp.exe /console /script=script_path\upload.txt /parameter file_to_upload.dat
Actually, you can put a shortcut to the above command to the Windows Explorer's Send To menu, so that you can then just right-click any file and go to the Send To > Upload using WinSCP and Execute Remote Command (=name of the shortcut).
For that, go to the folder %USERPROFILE%\SendTo
and create a shortcut with the following target:
winscp_path\winscp.exe /console /script=script_path\upload.txt /parameter %1
-
I'm so glad I saw this because I already am using winscp in my script and putty was being annoying Commented Dec 9, 2015 at 16:38
Here is a totally out of the box solution.
- Install AutoHotKey (ahk)
- Map the script to a key (e.g. F9)
In the ahk script, a) Ftp the commands (.ksh) file to the linux machine
b) Use plink like below. Plink should be installed if you have putty.
plink sessionname -l username -pw password test.ksh
or
plink -ssh example.com -l username -pw password test.ksh
All the steps will be performed in sequence whenever you press F9 in windows.
-
(...perhaps you meant "thinking outside the box", since this is a few steps more than one.) 😉 Commented Oct 27, 2019 at 14:59
Code:
using System;
using System.Diagnostics;
namespace playSound
{
class Program
{
public static void Main(string[] args)
{
Console.WriteLine(args[0]);
Process amixerMediaProcess = new Process();
amixerMediaProcess.StartInfo.CreateNoWindow = false;
amixerMediaProcess.StartInfo.UseShellExecute = false;
amixerMediaProcess.StartInfo.ErrorDialog = false;
amixerMediaProcess.StartInfo.RedirectStandardOutput = false;
amixerMediaProcess.StartInfo.RedirectStandardInput = false;
amixerMediaProcess.StartInfo.RedirectStandardError = false;
amixerMediaProcess.EnableRaisingEvents = true;
amixerMediaProcess.StartInfo.Arguments = string.Format("{0}","-ssh username@"+args[0]+" -pw password -m commands.txt");
amixerMediaProcess.StartInfo.FileName = "plink.exe";
amixerMediaProcess.Start();
Console.Write("Presskey to continue . . . ");
Console.ReadKey(true);
}
}
}
Sample commands.txt:
ps
Try MtPutty, you can automate the ssh login in it. Its a great tool especially if you need to login to multiple servers many times. Try it here
Another tool worth trying is TeraTerm. Its really easy to use for the ssh automation stuff. You can get it here. But my favorite one is always MtPutty.
-
Hi my query here is that I need to automate script execution without any human intervention, how to do that. I am using selenium. Commented Apr 17, 2015 at 6:02
In case you are using Key based authentication, using saved Putty session seems to work great, for example to run a shell script on a remote server(In my case an ec2).Saved configuration will take care of authentication.
C:\Users> plink saved_putty_session_name path_to_shell_file/filename.sh
Please remember if you save your session with name like(user@hostname), this command would not work as it will be treated as part of the remote command.
-
The correct syntax to load a saved session (that works with any site name) is
plink -load name command
-- As already shown in the answer by @lwpro2. Commented Mar 12, 2019 at 7:04