Here is a post on how to do this.
I recently had a project requiring a script to complete an SSH session on a Cisco device and make some configuration changes. My favorite scripting language, of course, is perl.
Earlier, I wrote scripts using the Net :: Telnet module, which would connect to a Telnet device, detect various device requests, and send commands to the device to change its configuration. So, I thought it would be a fairly simple case to just find a suitable similar module that uses SSH instead of Telnet.
Another thing that I should mention is that I am generally limited to using Win32 platforms (i.e., Windows 200, 2003, XP) due to the fact that most of the client sites I work on these days are not use Unix (or Linux) .., in my opinion, it’s very unfortunate, but I do not set the corporate policies of my clients.
An initial look at the available perl modules showed a series of modules that seemed to be exactly what I needed:
Net :: Appliance :: Session Net :: SSH :: W32Perl Net :: SSH
So, I told my boss, “Yes, no problem” and proceeded to combine the script in SSH to a remote device and send him some commands to change his configuration.
After a day or so, to try the various modules, I thought: "Hmm, maybe it will not be as easy as I thought!"
Each of the modules that I was looking at seemed to have a problem when it came to using it in a Windows environment. To summarize: here are the problems I found with each module:
* 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 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 began to think that maybe it just won’t work at all on Windows ....
Then I remembered seeing a publication on perl mongers where someone mentioned that they used perl in cygwin to run one of the Net :: * modules.
So, although the idea of downloading and installing cygwin initially did not seem too attractive, I thought that I would step back from it because I had nowhere else to go.
Cygwin
Cygwin ... well, let me quote directly from the cygwin website:
`` Cygwin is a Linux-like environment for Windows. It consists of two parts:
A DLL (cygwin1.dll) that acts as a Linux API emulation layer, providing essential Linux API functionality. A collection of tools that provide Linux and look.
Currently, the Cygwin DLL works with all recently released x86 32-bit and 64-bit versions of Windows, with the exception of Windows CE.``
Thus, it allows you to run in a Linux-type environment ... sounds much more promising for the various Net :: * modules that I looked at.
In addition, once the base cygwin core is installed, it allows you to download and install a number of other useful properties, including perl!
So, I installed the cygwin installation by downloading the setup.exe file from the cygwin home page and running it on my PC.
Cygwin provides a fault-tolerant graphical interface that allows you to choose a mirror for downloading files and which packages you want to download.
Here are the packages that I installed in addition to the basic cygwin core packages that are installed for you:
GCC Core GnuPG Do ncftpget OpenSSH Perl Pearl ExtUtils Wget Zipper
You can install others (besides) if you want, but they did the job for me.
Perl
Once I installed cygwin (including the perl distribution that I included), I was configured to add modules that I wanted to try using (e.g. 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 to your perl distribution. Under cygwin, the process is exactly the same.
If you are used to using the perl version of Activestate, you probably use to use 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 need to use the CPAN shell, which is a little harder to use if you're not used to it. The good news is that you will have access to much more perl modules!
When you install cygwin, it will place the icon somewhere (on your desktop, in the shortcut bar or in the Start menu) so that you can launch the cygwin shell (it looks something like this:
If you click on the shell icon, you will get an open cygwin shell where you can enter various commands. You can see it as a Unix shell in every sense and purpose.
To load perl modules you will need to enter "cpan" at the command line. This will allow you to load new modules into your perl distribution.
Now, when you first go into the CPAN shell, you will have a lot of uncomfortable questions about where the various utilities are located and which mirror you would like to use. Basically, if you installed the modules that I recommended for cygwin, you can accept the default values. (You will need to find out the mirror for yourself - it depends on where you are in the world).
The only parameter that I would recommend you change by default is the question of whether you should “query or follow” for the pre-modules — I would go “follow” to save a lot of trouble.
Once you open the cpan shell (you can say, because you now have the cpan> prompt), you need to enter the following commands to load into some perl modules that you need:
install LWP (accept all defaults for any questions) install Net :: Appliance :: Session
When you enter these commands, you will see many things flying around the screen when the modules are installed for you.
Running scripts
Well, at the moment we have taken a rather whirlwind tour, having received cygwin and perl installed on your platform, but now you should be able to run some perl scripts.
We installed the Net :: Appliance :: Session module in your perl distribution so that we can run perl scripts that will use it to provide us with Telnet or SSH sessions on the target device.
Since we are working in a cygwin environment, now we can use this module on the Windows platform - whatever we could do by simply installing Activestate perl or even using our own version of perl under Windows. So now we can run perl scripts that will allow us to run SSH sessions for network devices that only support SSH.
You may be wondering: "Does this 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 are not ...! As long as you call the cygwin perl executable to run the script, it will work fine from the DOS prompt (or the system scheduler!). So this will work fine:
c: \ c: \ cygwin \ bin \ perl.exe myscript.pl
Anyway, I guess it's enough for you to know how I crack my particular problem. I hope to cover all of this in a bit more detail in future ariths, as well as provide more information on how to use the incredibly useful Net :: Appliance :: Session module to create useful scripts for managing and configuring network devices.
http://perlwin32ssh.blogspot.com/2007/07/test_4418.html