使用 PuTTY 在 Windows 上自动运行 Linux 命令
我有一个场景,我需要从 Windows 频繁运行 a linux shell 命令(使用不同的文件名)。我正在使用 PuTTY 和 WinSCP 来执行此操作(需要登录名和密码)。通过 WinSCP 将文件复制到 Linux 计算机中的预定义文件夹,然后从 PuTTY 运行命令。有没有一种方法可以通过程序自动执行此操作。理想情况下,我想从 Windows 中右键单击该文件并发出命令,将文件复制到远程计算机并以文件名作为参数运行预定义命令(在 PuTTy 中)。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
Putty通常附带“plink”实用程序。
这本质上是作为 Windows .exe 实现的“ssh”命令行命令。
它在 putty 手册的“使用命令行工具 plink”下有很好的记录。
您只需将如下命令包装
在 .bat 脚本中。
您还可以使用常见的 shell 结构(例如分号)来执行多个命令。例如:
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:
in a .bat script.
You can also use common shell constructs, like semicolons to execute multiple commands. e.g:
常见的自动登录方法可能存在安全问题。
最简单的方法之一记录如下:
至于执行命令的部分
在 putty UI 中,连接 > SSH >有一个用于远程命令的字段。
简而言之,您的答案可能与下面的文本类似:
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.
in short, your answers might just as well be similar to the text below:
您可以编写 TCL 脚本并与该 Linux 计算机建立 SSH 会话并自动发出命令。检查 http://wiki.tcl.tk/11542 获取简短教程。
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.
您可以创建一个 putty 会话,并在启动会话时在服务器上自动加载脚本:
在远程命令中,指向远程脚本。
You can create a putty session, and auto load the script on the server, when starting the session:
At remote command, point to the remote script.
您可以使用 WinSCP 完成这两项任务(上传和命令执行)。使用 WinSCP 脚本,例如:
call
命令参考:https://winscp.net/eng/docs/scriptcommand_call
% 的参考1%
语法:https://winscp.net/eng/docs/scripting#syntax
然后您可以运行脚本,如下所示:
实际上,您可以将上述命令的快捷方式添加到 Windows 资源管理器的“发送到”菜单,这样您就可以右键单击任何文件并转到“发送”到>使用 WinSCP 上传并执行远程命令(=快捷方式的名称)。
为此,请转到文件夹
%USERPROFILE%\SendTo
并创建具有以下目标的快捷方式:请参阅 正在资源管理器的“发送到”菜单中创建条目。
You can do both tasks (the upload and the command execution) using WinSCP. Use WinSCP script like:
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:
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:See Creating entry in Explorer's "Send To" menu.
这是一个完全开箱即用的解决方案。
在 ahk 脚本中,
a) 将命令 (.ksh) 文件通过 FTP 传输到 Linux 计算机
b) 使用 plink,如下所示。如果您有 putty,则应安装 Plink。
或
当您在 Windows 中按 F9 时,所有步骤都会按顺序执行。
Here is a totally out of the box solution.
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.
or
All the steps will be performed in sequence whenever you press F9 in windows.
代码:
}
示例命令.txt:
ps
链接:
Code:
}
Sample commands.txt:
ps
Link: https://huseyincakir.wordpress.com/2015/08/27/send-commands-to-a-remote-device-over-puttyssh-putty-send-command-from-command-line/
尝试MtPutty,
您可以在其中自动执行 ssh 登录。它是一个很棒的工具,特别是当您需要多次登录多个服务器时。 此处试试
另一个值得尝试的工具是TeraTerm。对于 ssh 自动化来说,它真的很容易使用。您可以在此处获取它。但我最喜欢的始终是 MtPutty。
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.
如果您使用基于密钥的身份验证,使用保存的 Putty 会话似乎效果很好,例如在远程服务器上运行 shell 脚本(在我的例子中是 ec2)。保存的配置将负责身份验证。
C:\用户> plink save_putty_session_name path_to_shell_file/filename.sh
请记住,如果您使用类似(user@hostname)的名称保存会话,则此命令将不起作用,因为它将被视为远程命令的一部分。
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.