Home: Perl Programming Help: Intermediate:
extended login using perl script


Aug 21, 2012, 7:50 AM

Views: 6977
extended login using perl script

Hi i have one server from where i login to all my network devices to execute command. I need to execute some commands on daily basis on all network device (show processes cpu history and show log) , after loggin in to the terminal server and from their accessing individual network devices.

i wrote the script to access the terminal server and it is successful but how to login to all the network devices from there and execute the command i have no clue. i am newbie in the scripting field and hence any suggestion is highly appreciate.

following is the .pl for login in to terminal server

use Net::SSH2;
use warnings;
use strict;

@servers = ("EGHQ-PR-1","EGHQ-PR-2","PR-1","PR-2");
my $host = "taco";
my $user = "s******";
my $password = "s****";

my $ssh = Net::SSH2->new();

print("Connection Failed\n");

print("Authentication Failed");

my $channel = $ssh->channel();
my $command = $channel->exec("nslookup taco") or die $_;
my $output;
my $len = $channel->read($output,2048);
print "output is $output\n";

it works fine.

please assist how to proceed from here.


(This post was edited by parvees on Aug 21, 2012, 9:15 AM)


Aug 22, 2012, 1:14 AM

Views: 6962
Re: [parvees] extended login using perl script

how to login to all the network devices from there and execute the command

How would you log in to them from the command line?

Can't you again use SSH? Or maybe Net::Telnet?


Aug 22, 2012, 1:17 AM

Views: 6961
Re: [rovf] extended login using perl script

Yes ,

from the terminal server i need to telnet to the network devices.

But how to call the array of devices and pass the credential to this and execute commands for all.

there should be some loop to call all the network devices and execute the commands right?

how to do this is what i am stuck with


In Reply To


Aug 22, 2012, 1:58 AM

Views: 6960
Re: [parvees] extended login using perl script

Of course you have to loop through all of them. If you can telnet to them, you can use the Net::Telnet module.

What is the problem with looping? Do you worry that it will take too long and you need parallelism?



Aug 22, 2012, 3:24 AM

Views: 6959
Re: [rovf] extended login using perl script

could you please provide me how to do the looping considering my current script.

i am a newbie in this field


Aug 22, 2012, 6:21 AM

Views: 6957
Re: [parvees] extended login using perl script

I'm not sure that I understand you correctly. Since you posted this in the "Intermediate" forum, I didn't expect that you are not familiar with looping constructs in Perl.

If this is *really* what you are looking for, check out

perldoc perlsyn

Scroll to the section saying 'Compound Statements', and have a look at the for and while.

If this is not what you are looking for, please provide in more detail what you want to know. You could also sketch a solution of the desired algorithm in English, Pseudo-Code or any programming language you are familiar with, and we can then discuss the Perl related issues.

Veteran / Moderator

Aug 22, 2012, 10:31 AM

Views: 6951
Re: [parvees] extended login using perl script

One easy possibility:

foreach my $current_server (qw/ server1 server2 server3/) { 
# code to connect to $current_server, shose value will take successively server1, server2 and server3 .


Aug 22, 2012, 10:37 AM

Views: 6949
Re: [Laurent_R] extended login using perl script

if i simplify my query , i think you will get better clarity

1) login (terminal server)
2) enter username
3) enter password

4) from terminal server
5) execute command (telnet network device 1)
6) enter username for network device1
7) enter password for network device1
8) execute command (show logging )
9) execute command (show proc cpu history)
10) exit from network device 1

go back to step 4 and from 5-10 repeat for network device 2 ,then 3,so on....

output of executed command (show logging , show proc cpu history ) should send to a file.

can this be possible , in windows what i know is calling shell inside a shell because actual logging in and activities happen from another server where you initially login ..

so can this be achieved?


Veteran / Moderator

Aug 22, 2012, 11:45 AM

Views: 6946
Re: [parvees] extended login using perl script

Just put steps 5 to 10 within the foreach loop I have given you.

Put the list of devices in the qq// part, and foreach will loop on the devices one after the other.

Something like this:

# login 
# username
# password
# from server
foreach my $current_device (qq/device1 device2 device3 /){
# execute command (telnet network $current_device)
# enter username for network $current_device
# enter password for network $current_device
# execute command (show logging )
# execute command (show proc cpu history)
# exit from network $current_device

The first time in the foreach loop, the value of $current_device will be device1, the seond time device2, etc.


Aug 22, 2012, 9:10 PM

Views: 6931
Re: [Laurent_R] extended login using perl script

can some one assist to change the above in perl language?

as i said i am newbie to this and dont know how to convert this.

Veteran / Moderator

Aug 23, 2012, 7:21 AM

Views: 6909
Re: [parvees] extended login using perl script

First thing you should be aware of is that there are some very good open source tools that do what you need and a lot more.

Please review these 2 tools.
Cacti http://www.cacti.net/
Nagios http://www.nagios.org/

Going back to your script, I think one aspect of your requirements was missed in the proposed solution. You're looking at wanting to cascade the ssh calls. Meaning that all ssh connections to the @servers need to originate from the $host server that you ssh'd into (not from where the script is running) and those "child ssh sessions" need to execute the subsequent commands.

I'm sure it can be done, but I don't see an easy way to do it. The better solution would be to use the tools that I mentioned above rather than what you're currently attempting.

(This post was edited by FishMonger on Aug 23, 2012, 7:22 AM)