使用 PuTTY 在 Windows 上自动运行 Linux 命令

发布于 2024-11-10 04:08:38 字数 248 浏览 2 评论 0原文

我有一个场景,我需要从 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(9

音栖息无 2024-11-17 04:08:38

Putty通常附带“plink”实用程序。
这本质上是作为 Windows .exe 实现的“ssh”命令行命令。
它在 putty 手册的“使用命令行工具 plink”下有很好的记录。

您只需将如下命令包装

plink root@myserver /etc/backups/do-backup.sh

在 .bat 脚本中。

您还可以使用常见的 shell 结构(例如分号)来执行多个命令。例如:

plink read@myhost ls -lrt /home/read/files;/etc/backups/do-backup.sh

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
凤舞天涯 2024-11-17 04:08:38

常见的自动登录方法可能存在安全问题。
最简单的方法之一记录如下:

至于执行命令的部分
在 putty UI 中,连接 > SSH >有一个用于远程命令的字段。

4.17 SSH 面板

SSH 面板允许您配置
仅适用于 SSH 的选项
会议。

4.17.1 在服务器上执行特定命令

在 SSH 中,您不必运行
服务器上的一般 shell 会话。
相反,您可以选择运行
单个特定命令(例如
例如,邮件用户代理)。如果你
想要执行此操作,请输入命令
“远程命令”框。
http://the.earth.li/~sgtatham/putty/0.53 /htmldoc/Chapter4.html

简而言之,您的答案可能与下面的文本类似:

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:

夕色琉璃 2024-11-17 04:08:38

您可以编写 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.

昨迟人 2024-11-17 04:08:38

您可以创建一个 putty 会话,并在启动会话时在服务器上自动加载脚本:

putty -load "sessionName" 

在远程命令中,指向远程脚本。

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.

梦途 2024-11-17 04:08:38

您可以使用 WinSCP 完成这两项任务(上传和命令执行)。使用 WinSCP 脚本,例如:

option batch abort
option confirm off
open your_session
put %1%
call script.sh
exit

call 命令参考:
https://winscp.net/eng/docs/scriptcommand_call

% 的参考1% 语法:
https://winscp.net/eng/docs/scripting#syntax

然后您可以运行脚本,如下所示:

winscp.exe /console /script=script_path\upload.txt /parameter file_to_upload.dat

实际上,您可以将上述命令的快捷方式添加到 Windows 资源管理器的“发送到”菜单,这样您就可以右键单击任何文件并转到“发送”到>使用 WinSCP 上传并执行远程命令(=快捷方式的名称)。

为此,请转到文件夹 %USERPROFILE%\SendTo 并创建具有以下目标的快捷方式:

winscp_path\winscp.exe /console /script=script_path\upload.txt /parameter %1

请参阅 正在资源管理器的“发送到”菜单中创建条目

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

See Creating entry in Explorer's "Send To" menu.

遇到 2024-11-17 04:08:38

这是一个完全开箱即用的解决方案。

  1. 安装 AutoHotKey (ahk)
  2. 将脚本映射到某个键(例如 F9)
  3. 在 ahk 脚本中,
    a) 将命令 (.ksh) 文件通过 FTP 传输到 Linux 计算机

    b) 使用 plink,如下所示。如果您有 putty,则应安装 Plink。

plink 会话名 -l 用户名 -pw 密码 test.ksh

plink -ssh example.com -l 用户名 -pw 密码 test.ksh

当您在 Windows 中按 F9 时,所有步骤都会按顺序执行。

Here is a totally out of the box solution.

  1. Install AutoHotKey (ahk)
  2. Map the script to a key (e.g. F9)
  3. 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.

好倦 2024-11-17 04:08:38

代码:

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);
    }
}

}

示例命令.txt:

ps

链接:

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

Link: https://huseyincakir.wordpress.com/2015/08/27/send-commands-to-a-remote-device-over-puttyssh-putty-send-command-from-command-line/

我不吻晚风 2024-11-17 04:08:38

尝试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.

东风软 2024-11-17 04:08:38

如果您使用基于密钥的身份验证,使用保存的 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文