使用 PuTTY 从 Windows 在 Linux 上自动运行命令
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6147203/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Automating running command on Linux from Windows using PuTTY
提问by Nemo
I have a scenario where I need to run alinux 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.
我有一种情况,我需要运行一个从窗户经常Linux shell命令(用不同的文件名)。我正在使用 PuTTY 和 WinSCP 来做到这一点(需要登录名和密码)。文件通过 WinSCP 复制到 linux 机器中的预定义文件夹,然后从 PuTTY 运行命令。有没有办法可以通过程序自动执行此操作。理想情况下,我想从 Windows 中右键单击该文件并发出将文件复制到远程机器的命令,并以文件名作为参数运行预定义的命令(在 PuTTy 中)。
采纳答案by Gary Tsui
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.
至于执行命令的部分,在 putty UI 中,Connection>SSH> 有一个用于远程命令的字段。
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
4.17 SSH 面板
SSH 面板允许您配置仅适用于 SSH 会话的选项。
4.17.1 在服务器上执行特定命令
在 SSH 中,您不必在服务器上运行常规 shell 会话。相反,您可以选择运行单个特定命令(例如邮件用户代理)。如果要执行此操作,请在“远程命令”框中输入命令。 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:
简而言之,您的答案可能与以下文本类似:
回答by Himanshu
You can write a TCL script and establish SSH session to that Linux machine and issue commands automatically. Check http://wiki.tcl.tk/11542for a short tutorial.
您可以编写 TCL 脚本并与该 Linux 机器建立 SSH 会话并自动发出命令。查看http://wiki.tcl.tk/11542以获取简短教程。
回答by James Anderson
Putty usuallycomes 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".
Putty通常带有“plink”实用程序。
这本质上是作为 Windows .exe 实现的“ssh”命令行命令。
它在“使用命令行工具 plink”下的腻子手册中得到了很好的记录。
You just need to wrap a command like:
你只需要包装一个命令,如:
plink root@myserver /etc/backups/do-backup.sh
in a .bat script.
在 .bat 脚本中。
You can also use common shell constructs, like semicolons to execute multiple commands. e.g:
您还可以使用常见的 shell 结构,例如分号来执行多个命令。例如:
plink read@myhost ls -lrt /home/read/files;/etc/backups/do-backup.sh
回答by lwpro2
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.
在远程命令中,指向远程脚本。
回答by Martin Prikryl
You can do both tasks (the upload and the command execution) using WinSCP. Use WinSCP script like:
您可以使用 WinSCP 执行这两项任务(上传和命令执行)。使用 WinSCP 脚本,例如:
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
call
命令参考:https:
//winscp.net/eng/docs/scriptcommand_call
Reference for the %1%
syntax:
https://winscp.net/eng/docs/scripting#syntax
%1%
语法参考: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 Tomenu, 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).
实际上,您可以将上述命令的快捷方式添加到 Windows 资源管理器的“发送到”菜单中,这样您就可以右键单击任何文件并转到“发送到”>“使用 WinSCP 上传”并执行远程命令(=快捷方式的名称) )。
For that, go to the folder %USERPROFILE%\SendTo
and create a shortcut with the following target:
为此,请转到该文件夹%USERPROFILE%\SendTo
并使用以下目标创建快捷方式:
winscp_path\winscp.exe /console /script=script_path\upload.txt /parameter %1
回答by Sudheesh.M.S
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
尝试MtPutty,您可以在其中自动进行 ssh 登录。它是一个很棒的工具,尤其是当您需要多次登录多个服务器时。在这里试试
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.
另一个值得尝试的工具是TeraTerm。它真的很容易用于 ssh 自动化的东西。你可以在这里得到它。但我最喜欢的永远是 MtPutty。
回答by Amit
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.
- 安装 AutoHotKey (ahk)
- 将脚本映射到一个键(例如 F9)
在 ahk 脚本中,a) 将命令 (.ksh) 文件 Ftp 到 linux 机器
b) 使用 plink 如下所示。如果你有腻子,应该安装 Plink。
plink sessionname -l username -pw password test.ksh
plink sessionname -l 用户名 -pw 密码 test.ksh
or
或者
plink -ssh example.com -l username -pw password test.ksh
plink -ssh example.com -l 用户名 -pw 密码 test.ksh
All the steps will be performed in sequence whenever you press F9 in windows.
每当您在 Windows 中按 F9 时,所有步骤将按顺序执行。
回答by huse.ckr
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:
示例命令.txt:
ps
ps
链接:https: //huseyincakir.wordpress.com/2015/08/27/send-commands-to-a-remote-device-over-puttyssh-putty-send-command-from-command-line/
回答by Alok Garg
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.
如果您使用基于密钥的身份验证,使用保存的 Putty 会话似乎效果很好,例如在远程服务器上运行 shell 脚本(在我的情况下是 ec2)。保存的配置将负责身份验证。
C:\Users> plink saved_putty_session_name path_to_shell_file/filename.sh
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.
请记住,如果您使用类似 (user@hostname) 的名称保存会话,此命令将不起作用,因为它将被视为远程命令的一部分。