如何从Windows执行Unix shell脚本

时间:2022-08-29 01:13:47

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脚本的最佳方法吗?

4 个解决方案

#1


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怪胎的爱。 :-)

(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 - 这使事情变得更容易。

#2


ssh eximadmin@172.16.12.10 "cat somefile.log"

ssh eximadmin@172.16.12.10“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还是其他)。

#3


I second the cygwin suggestion, but one other option that you have is Microsoft's Services for UNIX.

我是第二个cygwin建议,但你有另一个选择是Microsoft的UNIX服务。

#4


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.

我要做的是在.Net应用程序可以运行的AIX机器上设置服务(https)接口。这实际上是您希望如何在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. (Commercial and FOSS)

正如其他人所建议的那样,你也可以使用Cygwin。您也可以使用.Net的SSH组件,这对您来说可能更透明。 (商业和FOSS)

#1


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怪胎的爱。 :-)

(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 - 这使事情变得更容易。

#2


ssh eximadmin@172.16.12.10 "cat somefile.log"

ssh eximadmin@172.16.12.10“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还是其他)。

#3


I second the cygwin suggestion, but one other option that you have is Microsoft's Services for UNIX.

我是第二个cygwin建议,但你有另一个选择是Microsoft的UNIX服务。

#4


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.

我要做的是在.Net应用程序可以运行的AIX机器上设置服务(https)接口。这实际上是您希望如何在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. (Commercial and FOSS)

正如其他人所建议的那样,你也可以使用Cygwin。您也可以使用.Net的SSH组件,这对您来说可能更透明。 (商业和FOSS)