我们如何在 Windows 中使用 Perl 配置 SSH?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3689945/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 15:14:20  来源:igfitidea点击:

How do we configure SSH using Perl in Windows?

windowsperlssh

提问by Arindam Paul

Can anyone suggest a a very simple way to use Net::SSH::Perl in Windows without all those cygwin hacks and all.

任何人都可以建议一种在 Windows 中使用 Net::SSH::Perl 的非常简单的方法,而无需所有那些 cygwin hacks 等等。

Whenever I am trying to install the modules it's taking me to some other dependent modules and the process seemed never ending..

每当我尝试安装模块时,它都会将我带到其他一些依赖模块,并且该过程似乎永无止境..

Thanks beforehand

预先感谢

采纳答案by user466909

Here is a post on how to do it I found.

这是我找到的有关如何执行此操作的帖子。

I recently had a project that required a script to perform an SSH session to a Cisco device and make some configuration changes. My favourite scripting language is, of course, perl.

我最近有一个项目需要一个脚本来执行到 Cisco 设备的 SSH 会话并进行一些配置更改。我最喜欢的脚本语言当然是 perl。

I had previously written scripts using the Net::Telnet module that would Telnet to a device, detect various device prompts and send commands to the device to change its configuration. So, I thought that this was going to be a fairly straightforward case of just finding a suitable, similar module that uses SSH instead of Telnet.

我以前曾使用 Net::Telnet 模块编写脚本,该模块可以 Telnet 到设备、检测各种设备提示并向设备发送命令以更改其配置。所以,我认为这将是一个相当简单的案例,只需找到一个合适的、使用 SSH 而不是 Telnet 的类似模块。

The other thing that I need to mention is that I am generally constrained to using Win32 platforms (i.e. Windows 200, 2003, XP) due to the fact that most client sites that I work on these days do not use Unix(or Linux) ..which is a real pity in my opinion, but I don't set the corporate policies of my clients.

我需要提及的另一件事是,由于我现在工作的大多数客户端站点都不使用 Unix(或 Linux),因此我通常只能使用 Win32 平台(即 Windows 200、2003、XP)。 .这在我看来真的很可惜,但我没有为我的客户制定公司政策。

An initial glance of the available perl modules revealed a number of modules which seemed to be just what I needed :

对可用的 perl 模块的初步浏览显示了许多似乎正是我需要的模块:

Net::Appliance::Session Net::SSH::W32Perl Net::SSH

Net::Appliance::Session Net::SSH::W32Perl Net::SSH

So, I told my boss, 'Yep, no problem' and set about pulling together a script to SSH in to a remote device and send it some commands to change its configuration.

所以,我告诉我的老板,“是的,没问题”,并着手编写脚本以通过 SSH 连接到远程设备,并向其发送一些命令来更改其配置。

After a day or so of trying various modules, I thought : 'Hmmm, maybe this isn't going to be as easy as I'd thought!'

在尝试了一天左右的各种模块后,我想:'嗯,也许这不会像我想象的那么容易!

Each of the modules I'd looked at seemed to have an issue when it came to using it in a Windows environment. To summarise, here are the issues I found with each module :

我看过的每个模块在 Windows 环境中使用时似乎都有问题。总而言之,以下是我在每个模块中发现的问题:

* Net::Appliance::Session : has a dependency on IO::Pty, which won't ever run on Windows
* Net::SSH::W32Perl : ``getpwuid function is unimplemented'' message`` when building from scratch. Older ppd's found at http://www.soulcage.net/ppds.58 just don't seem to work....even when installed in to the Activestate perl distribution
* Net::SSH : Unix only

So, at this point, I was beginning to think that maybe this just wasn't going to work at all on Windows....

所以,在这一点上,我开始认为这可能在 Windows 上根本不起作用......

Then, I remembered seeing a posting on perl mongers where someone has mentioned that they had used perl in cygwin to run one of the Net::* modules.

然后,我记得在 perl mongers 上看到一个帖子,有人提到他们在 cygwin 中使用 perl 来运行 Net::* 模块之一。

So, although the thought of having to download and install cygwin didn't initially seem too appealing, I thought I'd give it a go as I had no where else to go.

因此,虽然必须下载和安装 cygwin 的想法最初似乎不太吸引人,但我认为我应该试一试,因为我无处可去。

Cygwin

赛格温

Cygwin is...well, let me quote directly from the cygwin web site :

Cygwin 是......好吧,让我直接从 cygwin 网站引用:

``Cygwin is a Linux-like environment for Windows. It consists of two parts:

``Cygwin 是一个类似 Linux 的 Windows 环境。它由两部分组成:

A DLL (cygwin1.dll) which acts as a Linux API emulation layer providing substantial Linux API functionality. A collection of tools which provide Linux look and feel.

一个 DLL (cygwin1.dll),它充当 Linux API 仿真层,提供大量 Linux API 功能。提供 Linux 外观和感觉的工具集合。

The Cygwin DLL currently works with all recent, commercially released x86 32 bit and 64 bit versions of Windows, with the exception of Windows CE.``

Cygwin DLL 目前适用于所有最新的、商业发布的 x86 32 位和 64 位版本的 Windows,但 Windows CE 除外。``

So, it allows you to run in a Linux-type environment...sounds far more promising for the various Net::* modules that I had been looking at.

因此,它允许您在 Linux 类型的环境中运行……对于我一直在研究的各种 Net::* 模块来说,这听起来更有希望。

Also, once the basic cygwin core is installed, it allows you to download and install a whole host of other goodies, including perl !

此外,一旦安装了基本的 cygwin 核心,它就允许您下载和安装大量其他好东西,包括 perl!

So, I set about installing cygwin by downloading the 'setup.exe' file from the cygwin home page and running it on my PC.

因此,我开始通过从 cygwin 主页下载“setup.exe”文件并在我的 PC 上运行它来安装 cygwin。

Cygwin provides a failrly intuitive GUI to let you select a mirror to download files from, and which packages you would like to download.

Cygwin 提供了一个非常直观的 GUI,让您可以选择要从中下载文件的镜像以及要下载的包。

Here are the packages that I installed beyond just the basic core cygwin packages that are installed for you :

以下是我安装的软件包,而不仅仅是为您安装的基本核心 cygwin 软件包:

gcc-core gnupg make ncftpget openSSH perl perl-ExtUtils wget zip

gcc-core gnupg make ncftpget openSSH perl perl-ExtUtils wget zip

You can install others (in addition) if you like, but these did the job for me.

如果您愿意,您可以安装其他(另外),但这些为我完成了工作。

Perl

珀尔

Once I had cygwin installed (including the perl distribution that I included), I was all set to add in the modules that I wanted to try to use (e.g. Net::SSH::Appliance).

一旦我安装了 cygwin(包括我包含的 perl 发行版),我就准备好添加我想尝试使用的模块(例如 Net::SSH::Appliance)。

If you are familiar with perl on Unix systems, then you will probably be very familiar with the CPAN shell to add modules into your perl distribution. Under cygwin, the process is exactly the same.

如果您熟悉 Unix 系统上的 perl,那么您可能非常熟悉 CPAN shell 以将模块添加到您的 perl 发行版中。在cygwin下,过程完全一样。

If you are used to using the Activestate version of perl, you are probably used to using the Activestate PPM manager to add new modules to perl. Well, the bad news is that under cygwin, you don't have a PPM manager, and you have to use the CPAN shell, which is a little bit trickier to use if you aren't used to it. The good news however, is that you will have access to far more perl modules !

如果您习惯使用 Activestate 版本的 perl,您可能习惯使用 Activestate PPM 管理器向 perl 添加新模块。好吧,坏消息是在 cygwin 下,你没有 PPM 管理器,你必须使用 CPAN shell,如果你不习惯的话,使用它会有点棘手。然而,好消息是您将可以访问更多的 perl 模块!

When you installed cygwin, it will have put an icon somewhere (either on your desktop, quicklauch bar or start menu) so that you can launch the cygwin shell (it looks something like this :

当您安装 cygwin 时,它会在某处(在您的桌面、快速启动栏或开始菜单上)放置一个图标,以便您可以启动 cygwin shell(它看起来像这样:

If you click the shell icon, you will get a nice cygwin shell open up where you can type in various commands. You can think of it as a Unix shell to all intents and purposes.

如果单击 shell 图标,您将打开一个漂亮的 cygwin shell,您可以在其中键入各种命令。您可以将其视为所有意图和目的的 Unix shell。

To load in the perl modules you'll need, you need to enter 'cpan' at the command prompt. This will allow you to load new modules in to your perl distribution.

要加载您需要的 perl 模块,您需要在命令提示符下输入“cpan”。这将允许您将新模块加载到您的 perl 发行版中。

Now, the first time you go in to the CPAN shell, it will aks you lots of rather awkward questions about where various utilities are and which mirror you would like to use. In the main, if you have installed the modules that I recommended for cygwin, you can accept the defaults. (The mirror you will have to figure out for yourself - it depends where in the world you are).

现在,当你第一次进入 CPAN shell 时,它会问你很多关于各种实用程序在哪里以及你想使用哪个镜像的相当尴尬的问题。总的来说,如果你已经安装了我为 cygwin 推荐的模块,你可以接受默认值。(你必须自己找出镜子——这取决于你在世界的哪个地方)。

The only setting I would recommend you change from the default is the question about whether you should be 'asked or follow' for pre-requisite modules - I would go for 'follow' to save a lot of hassle.

我建议您更改默认设置的唯一设置是关于是否应该“询问或关注”先决模块的问题 - 我会选择“关注”以节省很多麻烦。

Once you have your cpan shell open (you can tell, because you now have a 'cpan>' prompt), you need to enter the following commands to load in some perl modules that you will need :

一旦你打开了你的 cpan shell(你可以知道,因为你现在有一个 'cpan>' 提示符),你需要输入以下命令来加载一些你需要的 perl 模块:

install LWP (accept all defaults to any questions asked) install Net::Appliance::Session

安装 LWP(接受任何问题的所有默认设置)安装 Net::Appliance::Session

When you enter these commands, you will see lots of things flying up the screen as the modules are installed for you.

当你输入这些命令时,你会看到很多东西在屏幕上飞起来,因为模块已经为你安装好了。

Running Scripts

运行脚本

Well, at this point, we have taken a rather whirlwind tour through getting cygwin and perl instaled on your platform, but you should now be in a position to run some perl scripts.

好吧,在这一点上,我们已经通过在您的平台上安装 cygwin 和 perl 进行了一次相当旋风之旅,但是您现在应该可以运行一些 perl 脚本了。

We installed the Net::Appliance::Session module in to your perl distribution, so that we can run perl scripts that will use it to give us either Telnet or SSH sessions to a target device.

我们在您的 perl 发行版中安装了 Net::Appliance::Session 模块,以便我们可以运行 perl 脚本,这些脚本将使用它为我们提供到目标设备的 Telnet 或 SSH 会话。

As we are running under the cygwin environment, we can now use this module on a Windows platform - something we couldn't do by just installing Activestate perl, or even by compiling our own version of perl under Windows. So, now, we CAN run perl scripts that will allow us to perform SSH sessions to network devices that only support SSH.

当我们在 cygwin 环境下运行时,我们现在可以在 Windows 平台上使用这个模块——这是我们无法通过安装 Activestate perl 或什至在 Windows 下编译我们自己的 perl 版本所无法做到的。所以,现在,我们可以运行 perl 脚本,这将允许我们对仅支持 SSH 的网络设备执行 SSH 会话。

You may be wondering : ``does that mean that I have to run my perl scripts from the cygwin shell every time I need to run them ?''. Well, the good news is that no, you don't..! As long as you call the cygwin perl executable to run your script, it will run fine from a DOS prompt (or system scheduler !). So, this would work fine :

您可能想知道:“这是否意味着每次我需要运行我的 perl 脚本时都必须从 cygwin shell 运行它们?”。好吧,好消息是,不,你没有..!只要您调用 cygwin perl 可执行文件来运行您的脚本,它就会从 DOS 提示符(或系统调度程序!)正常运行。所以,这会正常工作:

c:\ c:\cygwin\bin\perl.exe myscript.pl

c:\ c:\cygwin\bin\perl.exe myscript.pl

Anyhow, I guess that's enough to let you know how I cracked my particular issue. I hope to cover all of this in a little more detail in future aritcles, as well as providing more informatipon about how to use the incredibly useful Net::Appliance::Session module to create some useful scripts to control and configure your network devices.

无论如何,我想这足以让您知道我是如何解决我的特定问题的。我希望在以后的文章中更详细地介绍所有这些,并提供更多关于如何使用非常有用的 Net::Appliance::Session 模块来创建一些有用的脚本来控制和配置网络设备的信息。

http://perlwin32ssh.blogspot.com/2007/07/test_4418.html

http://perlwin32ssh.blogspot.com/2007/07/test_4418.html

回答by rpg

For ActiveState Perl, probably you can try for Net::SSH2 module. I tried with couple of online examples and it worked for me.

对于 ActiveState Perl,您可能可以尝试使用 Net::SSH2 模块。我尝试了几个在线示例,它对我有用。

You can go through following resources to install and run a sample example using Net::SSH2.

您可以通过以下资源使用 Net::SSH2 安装和运行示例示例。

Use this repository to install Net::SSH2 http://theoryx5.uwinnipeg.ca/ppms/

使用此存储库安装 Net::SSH2 http://theoryx5.uwinnipeg.ca/ppms/

couple of examples: http://www.perlmonks.org/?node_id=569657

几个例子:http: //www.perlmonks.org/?node_id= 569657

Hope this will work for you. I tried it and it worked for me.

希望这对你有用。我试过了,它对我有用。

回答by stealth916

The answer about using Perl from within Cygwin then calling it from the Windows command line is the correct answer. THANKS VERY MUCH FOR YOUR EFFORT!

关于在 Cygwin 中使用 Perl 然后从 Windows 命令行调用它的答案是正确的答案。非常感谢您的努力!

However, this method to call a PERL instance running in Cygwin from the Windows Command Line is notalways optimum:

但是,这种从 Windows 命令行调用在 Cygwin 中运行的 PERL 实例的方法并不总是最佳的:

c:\cygwin\bin\perl.exe myscript.pl

c:\cygwin\bin\perl.exe myscript.pl

That technique will work with some Perl scripts but not all. It was giving me heck trying to use NET:SSH:EXPECT and the login() method. It would abort with strange errors.

该技术适用于某些 Perl 脚本,但不是全部。它让我尝试使用 NET:SSH:EXPECT 和 login() 方法。它会因奇怪的错误而中止。

A better way to call your Perl instance running in Cygwin from the Windows command prompt is this:

从 Windows 命令提示符调用在 Cygwin 中运行的 Perl 实例的更好方法是:

c:\cygwin\bin\bash --login -c 'perl myscript.pl'

c:\cygwin\bin\bash --login -c 'perl myscript.pl'

This seems to execute the Perl script fully within the Cygwin environment and even complicated Perl scripts or those with many dependencies or not capable of dealing with MSDOS style path names, will still run. You can still return the output of the Perl script back to the Windows command window, for instance using print()from within your Perl script.

这似乎可以在 Cygwin 环境中完全执行 Perl 脚本,即使是复杂的 Perl 脚本或那些具有许多依赖项或无法处理 MSDOS 样式路径名的脚本,仍将运行。您仍然可以将 Perl 脚本的输出返回到 Windows 命令窗口,例如在 Perl 脚本中使用print()