如何从 Windows 执行 Unix shell 脚本
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/772935/
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
How to execute a Unix shell script from Windows
提问by John M Gant
I've got an AIX script I would like to run from a .NET application. I'm not opposed to kicking off a Windows batch file if that gives me better options.
我有一个 AIX 脚本,我想从 .NET 应用程序运行。如果这给了我更好的选择,我不反对启动 Windows 批处理文件。
Most of what I've seen on Google relates to using a tool such as rsh or Plink, or using ssh or telnet. I don't have access to rsh or Plink (although I do have PuTTY and could probably get Plink). I can ssh to the AIX box but telnet is disabled.
我在 Google 上看到的大部分内容都与使用诸如 rsh 或 Plink 之类的工具有关,或者使用 ssh 或 telnet。我无法访问 rsh 或 Plink(尽管我有 PuTTY 并且可能可以获得 Plink)。我可以通过 ssh 连接到 AIX 框,但禁用了 telnet。
I'll need a way of passing the user's credentials.
我需要一种传递用户凭据的方法。
Can anyone recommend the best way to call a Unix script from a Windows application?
任何人都可以推荐从 Windows 应用程序调用 Unix 脚本的最佳方法吗?
采纳答案by Tanktalus
Install cygwin. Include the ssh function. Set up your ssh keys - private key on windows, public key on the AIX box. Then you can run "ssh user@aixbox script-on-aix-box.sh".
安装 cygwin。包括 ssh 功能。设置您的 ssh 密钥 - Windows 上的私钥,AIX 机器上的公钥。然后你可以运行“ssh user@aixbox script-on-aix-box.sh”。
Best to have the script on AIX, though you can use scp to copy the file to AIX first. Passing a long command line through ssh is error prone - not due to it being ssh, but because getting escapes properly set up to pass from .NET through ssh to ksh or whatever is running on AIX is just painful. Best to keep things as simple as possible, preferably just a single command which can then be a script that does multiple things.
最好将脚本放在 AIX 上,尽管您可以先使用 scp 将文件复制到 AIX。通过 ssh 传递长命令行很容易出错——不是因为它是 ssh,而是因为正确设置转义以从 .NET 通过 ssh 传递到 ksh 或在 AIX 上运行的任何东西都非常痛苦。最好让事情尽可能简单,最好只有一个命令,然后它可以是一个执行多项操作的脚本。
To answer your comments, which would take too long for another comment:
要回答您的评论,另一条评论需要很长时间:
(1) Why is it better to use cygwin's ssh utility instead of the Windows ssh utility?
(1) 为什么使用 cygwin 的 ssh 实用程序比使用 Windows ssh 实用程序更好?
a) I know it better, so I wouldn't know to suggest any windows ssh utility (I didn't even know Windows came with an ssh utility), and b) cygwin's ssh utility stands a better chance of being standard ssh with all the familiarity us unix freaks love. :-)
a) 我更了解它,所以我不知道推荐任何 windows ssh 实用程序(我什至不知道 Windows 带有 ssh 实用程序),并且 b) cygwin 的 ssh 实用程序更有可能成为标准 ssh熟悉我们unix freaks爱。:-)
(2) What are the keys for? Does that allow cross-platform authentication, or is it just a way of asymmetrically encrypting the password?
(2) 钥匙有什么用?这是否允许跨平台身份验证,还是只是一种非对称加密密码的方式?
The keys allow passwordless access. If you set up your private key as passwordless (not usually recommended, but if you can keep your machine secure, it works well for automated tools), your tool will be able to just ssh over with no password prompts - this makes things much easier.
密钥允许无密码访问。如果您将您的私钥设置为无密码(通常不推荐,但如果您可以保证您的机器安全,它适用于自动化工具),您的工具将能够在没有密码提示的情况下通过 ssh 连接 - 这使事情变得更加容易.
回答by jsight
ssh [email protected] "cat somefile.log"
ssh [email protected] "cat somefile.log"
(where cat somefile.log is replaced with the command that you want to run)
(其中 cat somefile.log 替换为您要运行的命令)
I think the easiest way to do it would be to call your ssh program (whether that be ssh, plink, or something else) from a batch file that is called from your .net app.
我认为最简单的方法是从您的 .net 应用程序调用的批处理文件中调用您的 ssh 程序(无论是 ssh、plink 还是其他程序)。
回答by Jason Baker
I second the cygwin suggestion, but one other option that you have is Microsoft's Services for UNIX.
我支持 cygwin 的建议,但您拥有的另一种选择是 Microsoft's Services for UNIX。
回答by Tracker1
What I would do, would be to setup a service (https) interface on the AIX box that the .Net application can run. This is really a matter of how you want to expose your script on the AIX box. IMHO Java + Tomcat would be easy enough to get setup on the AIX box, just make sure you limit your exposure, and/or require an authentication ticket for the AIX service. This isn't so much a .Net question as an architectural decision.
我要做的是在 AIX 机器上设置一个服务 (https) 接口,以便 .Net 应用程序可以运行。这实际上是您希望如何在 AIX 机器上公开您的脚本的问题。恕我直言,Java + Tomcat 很容易在 AIX 机器上进行设置,只需确保限制您的暴露,和/或需要 AIX 服务的身份验证票。这与其说是一个架构决策,不如说是一个 .Net 问题。
--edit--
- 编辑 -
As others have suggested, you can use Cygwin as well. There are SSH components for .Net you can use as well, which may be more transparent for you. (Commercialand FOSS)
正如其他人所建议的那样,您也可以使用 Cygwin。您也可以使用 .Net 的 SSH 组件,这对您来说可能更透明。(商业和自由和开放源码软件)