CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

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

 



parvees
Novice

Aug 21, 2012, 7:50 AM

Post #1 of 11 (2912 views)
extended login using perl script Can't Post

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

#!/usr/bin/perl
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();

if(!$ssh->connect($host)){
print("Connection Failed\n");
exit(1);
}

if(!$ssh->auth_password($user,$password)){
print("Authentication Failed");
exit(1);
}

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.

thanks,
PSM


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


rovf
Veteran

Aug 22, 2012, 1:14 AM

Post #2 of 11 (2897 views)
Re: [parvees] extended login using perl script [In reply to] Can't Post


Quote
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?


parvees
Novice

Aug 22, 2012, 1:17 AM

Post #3 of 11 (2896 views)
Re: [rovf] extended login using perl script [In reply to] Can't Post

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

thanks,
PSM

In Reply To


rovf
Veteran

Aug 22, 2012, 1:58 AM

Post #4 of 11 (2895 views)
Re: [parvees] extended login using perl script [In reply to] Can't Post

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?

Ronald


parvees
Novice

Aug 22, 2012, 3:24 AM

Post #5 of 11 (2894 views)
Re: [rovf] extended login using perl script [In reply to] Can't Post

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

i am a newbie in this field


rovf
Veteran

Aug 22, 2012, 6:21 AM

Post #6 of 11 (2892 views)
Re: [parvees] extended login using perl script [In reply to] Can't Post

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.


Laurent_R
Enthusiast / Moderator

Aug 22, 2012, 10:31 AM

Post #7 of 11 (2886 views)
Re: [parvees] extended login using perl script [In reply to] Can't Post

One easy possibility:


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



parvees
Novice

Aug 22, 2012, 10:37 AM

Post #8 of 11 (2884 views)
Re: [Laurent_R] extended login using perl script [In reply to] Can't Post

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?

PSM


Laurent_R
Enthusiast / Moderator

Aug 22, 2012, 11:45 AM

Post #9 of 11 (2881 views)
Re: [parvees] extended login using perl script [In reply to] Can't Post

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:


Code
# 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.


parvees
Novice

Aug 22, 2012, 9:10 PM

Post #10 of 11 (2866 views)
Re: [Laurent_R] extended login using perl script [In reply to] Can't Post

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.


FishMonger
Veteran / Moderator

Aug 23, 2012, 7:21 AM

Post #11 of 11 (2844 views)
Re: [parvees] extended login using perl script [In reply to] Can't Post

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)

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives