LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 02-24-2009, 05:16 PM   #1
0.o
Member
 
Registered: May 2004
Location: Raleigh, NC
Distribution: Debian, Solaris, HP-UX, AIX
Posts: 208

Rep: Reputation: 35
SSH or Telnet to Cisco


Hello,

I am trying to write a perl script that will ssh to a switch/router and execute some commands. If the ssh connection is refused, it should execute a small block of code to push the IP to an array. Once all the SSH sessions are complete, it should run a telnet sub and use the array created above as input. All is well with the exception of pushing elements to the array. Here is the code:

Code:
#!/usr/bin/perl

use warnings;
use strict;
use Net::SSH::Expect;
use Net::Telnet::Cisco;

my %SwitchIPs = (
       ##Large hash of IP => hosts
);

my %RouterIPs = (
         ##Large hash of IP => hosts
);



sub _SSH
{

        my @Telnet;
        foreach my $key ( keys %SwitchIPs)
        {
                open(LOG,">/tmp/perl_ssh.$key") or die "Can't open file for writing: $!";

                my $ssh= Net::SSH::Expect->new(
                        host => $key,
                        password => 'pass',
                        user => 'user',
                        raw_pty => 1,
                        timeout => 5
                );

                my $login_output = $ssh->login();
                if($login_output =~ /refused/)
                {

                        print $login_output;
                        push(@Telnet, $key);


                }



                $ssh->exec("term length 0");

                print LOG "Output of show except: \n";
                print LOG my @Except = $ssh->exec("sh except");

                print LOG "\n\n";

                print LOG "Output of show service: \n";
                print LOG my @Service = $ssh->exec("sh service");


                print LOG "Output of show log: \n\n";
                print LOG my @Log = $ssh->exec("sh log");


                print LOG "Output of show interface: \n\n";
                print LOG my @Int = $ssh->exec("sh interface");


                print LOG my @Count = $ssh->send("clear count\r");


                if("$key" eq "<special_ip>")
                {
                        $ssh->exec("debug dialer");
                }

                $ssh->send("undebug all");

                $ssh->close();
        }
        #_TELNET(\@Telnet);
#       print \@Telnet;
close(LOG);
}

sub _TELNET
{
        my @Telnet = @{$_[0]};
        foreach my $host (@Telnet)
        {
                my $telnet = Net::Telnet::Cisco->new(host => "$host");
                $telnet->login('user','pass');
                print $host;
        }
}


_SSH();

For some reason after the login attempt fails, the script dies. According to the examples on CPAN, this shouldn't be happening.
 
Old 02-24-2009, 10:33 PM   #2
seefor
Member
 
Registered: Mar 2006
Posts: 34

Rep: Reputation: 15
To be honest I've attempt this a while back and filed

I found an easier way using Shell and Expect, if you can use Expect here is the script I use:
Code:
spawn ssh root@10.0.0.1
match_max 100000
expect "(yes/no)?" {send "yes\n"}
expect "password:" {send "password\n"}
expect ">" {send "enable\n"}
expect "Password:" {send "password\n"}
exp "#"
send -- "sh service\r"
exp "#"
send -- "sh log\r"
exp "#"
send -- "clear count\r"
send -- "\r"
exp "#" 
send -- "exit\r"
expect eof
I can post more about my shell script if you need me too.

Sorry it's been a long day.

SeeFor
www.sifizm.com
 
Old 02-25-2009, 02:29 AM   #3
acid_kewpie
Moderator
 
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 43,417

Rep: Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985
Expect all the way, no second thoughts. Expect is available for use within perl too if you're more comfortable.
http://search.cpan.org/~rgiersig/Expect-1.15/Expect.pod
 
Old 02-25-2009, 05:19 AM   #4
0.o
Member
 
Registered: May 2004
Location: Raleigh, NC
Distribution: Debian, Solaris, HP-UX, AIX
Posts: 208

Original Poster
Rep: Reputation: 35
I am using expect within that perl script. I have used expect in shell scripts for years now. I want to move everything to perl. I guess i could put the devices that only support telnet into their own hash...
 
Old 02-25-2009, 05:59 AM   #5
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,360

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
How about Net::SSH2 http://search.cpan.org/~dbrobins/Net...ib/Net/SSH2.pm ?
 
Old 02-25-2009, 01:36 PM   #6
seefor
Member
 
Registered: Mar 2006
Posts: 34

Rep: Reputation: 15
Ok, I had to dig up a real old script, it works but every ugly
Code:
#!/usr/bin/perl

#
#############################################################################
#
#
#     SIFIZM 2006, January 08
#     www.sifizm.com
#     sifizm(at)gmail.com
#     
#     Released under the terms and conditions of SIFIZM
#
#     $Source: "http://www.pageresource.com/cgirec/ptut14_1.htm"
#     $Revision: 1.13 $
#     $Date: 2006/01/08 $
#     $Author: SeeFor $
#############################################################################
#
use Symbol;
use Net::Telnet ();

#
#############################################################################
#Open the Data File and put it in a list in array, comma separated.
#
#############################################################################
#
$data_file="test.dat";
open(DAT, $data_file) || die("Could not open the popHD.dat file, please create it!");
@raw_data=<DAT>;
close(DAT);

#
#############################################################################
#Useing the split method to create the variables we need each time through the loop. 
#Since we used the pipe symbol as the separator, that is the character we will use to split the data. 
#Notice that the pipe symbol needs to be escaped with a \ character since it is a special character in Perl: 
#############################################################################
#
foreach $hostname (@raw_data)
{
# chop($tracedata);
# ($hostname,$port,$path,$ch)=split(/\,/,$tracedata);

#
#############################################################################
# define commands
# list in array, comma separated, 
# double quote if multiple strings
#############################################################################
#
@CMD_LIST = (
"sh interface",
"sh log"
);

                $username = "perl";
                $passwd = "script";

                open(LOG, ">> SDlecs.log");
                        $t = Net::Telnet->new(-errmode => 'return');
                        $t->open($hostname);
                        $t->login($username, $passwd);
                foreach $session_cmd (@CMD_LIST) {
                        @lines =  $t->cmd($session_cmd);
                        print LOG "\n","-" x 80, "\n", "Host IP address:", $hostname, "\n", @lines;

        }
}
                close LOG;
Don't know if this will help but there you go
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
ssh and telnet !! rajesh486 Linux - Newbie 2 01-14-2009 10:51 PM
ssh and telnet rahul_khare15 Linux - Newbie 4 10-16-2007 12:25 AM
Cisco, Telnet, and Bash Scripting gss6 Linux - General 2 04-20-2007 02:21 PM
SSH over Cisco VPN Client on Fedora Core 2 mkochco Linux - Networking 1 11-17-2005 02:56 PM
Telnet into Cisco router via /dev/ttyS0 (aka COM1 serial port) naesyllek Linux - Networking 2 06-25-2003 05:33 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 03:24 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration