KODE IKLAN DFP 1 Connect to switch via telnet or ssh use PERL and store data to MYSQL | LELAKU
IKLAN 2
IKLAN 1

Connect to switch via telnet or ssh use PERL and store data to MYSQL

IKLAN 3
IKLAN 3
Assalamualaikum, salam namaste, om swastiyastu, glenon, sampurasun, kulonuwun, lan sak piturute.......

Sugeng dalu kagem poro sederek, sanak kadang ingkang wonten tlatah nose wan't to row (red: nuswantoro) lan ngarcopodo lintunipun.
Dalu meniko kulo bade ngawiti mbabar caranipun saget mundut data saking switch utawi router injih ndamel pemrograman PERL kaliyan database MYSQL..

ngene ini Script gawe connect. nang kene aku gawe Expect::Simple lan PERL version 5.10

use Expect::Simple;
use Getopt::Long;
use Time::Local;
use Data::Dumper;
use POSIX;
use DBI;


$option = GetOptions ("n|ne=s" => \$ne, "debug" => \$debug, "expect_debug" => \$exp_debug);

$expect_debug = 0;
$expect_verbose = 0;
if($exp_debug eq 1) {
  $expect_debug = 4;
  $expect_verbose = 4;
}

debug('Check if Switch is in the database list');

$ip               = '';
$prompt_name      = $ne;
$user             = '';
$password_login   = '';
$password_enable  = '';
$is_ssh           = 0;
#check if Switch available in the database list
$dsn = "DBI:mysql:database=network_monitoring;host=202.192.21.90;port=3306";
$dbh = DBI->connect($dsn,'monitor','monitor*1');
                $sql =  "SELECT * FROM list_ne WHERE prompt_name=" . $dbh->quote($prompt_name);
                debug('SQL: ' . $sql);
                $sth = $dbh->prepare($sql);
                $sth->execute;
                while(my $ref = $sth->fetchrow_hashref()) {
                        $ip               =  $ref->{'ip'};
                                                $user             =  $ref->{'user'};
                                                $password_login   =  $ref->{'password_login'};
                                                $password_enable  =  $ref->{'password_enable'};
                                                $is_ssh           =  $ref->{'is_ssh'};
                        debug('Switch exists, IP address ' . $ip);
                                                if ($is_ssh) {
                                                        debug('Switch conection using SSH');
                                                } else {
                                                        debug('Switch conection using TELNET');
                                                }
                }
                $sth->finish;
                $sth = undef;
$dbh->disconnect();
$dbh = undef;

if ($ip eq '')
    {
        debug('Switch not found in database, exiting...');
        exit(0);
    }

if ($is_ssh)
    {

                debug('Connecting SSH to NE : ' . $ne  . ' [' . $ip . ']');

                $t = new Expect::Simple(
                  {

                         Cmd => ['ssh', $user . '@' . $ip_addr ],
                         Prompt => ['Are you sure you want to continue connecting (yes/no)? ', -re => '.*\'s password:', $prompt_name.'>','Password:', $prompt_name.'#'],
                         DisconnectCmd => 'exit',
                         Timeout => 60,
                         Verbose => $expect_verbose,
                         Debug => $expect_debug
                  }
                );

                debug('Login to NE : ' . $ne  . ' [' . $ip . ']');

                if($t->match_idx eq 1) {
                  $t->send('yes');
                }

                if($t->match_idx eq 2) {
                  $t->send($password_login);
                }

                if($t->match_idx ne 3) {
                  # ERROR, don't see standard > prompt, exiting ...
                  debug("No '>' prompt...exiting...");
                  exit(0);
                }

                $cmd = 'terminal length 0';
                $t->send($cmd);
                if($t->match_idx eq 3) {
                                $t->send('enable');
                }

                if($t->match_idx eq 4) {
                                $t->send($password_enable);
                }

                if($t->match_idx ne 5) {
                                debug("No '#' prompt...exiting...");
                                exit(0);
                }
    }
else

    { # telnet

                debug('Connecting TELNET to NE : ' . $ne  . ' [' . $ip . ']');

                $t = new Expect::Simple(
                  {

                         Cmd => ['telnet', $ip ],
                         Prompt => ['Username:', $prompt_name.'>','Password:', $prompt_name.'#', 'ogin:'],
                         DisconnectCmd => 'exit',
                         Timeout => 60,
                         Verbose => $expect_verbose,
                         Debug => $expect_debug
                  }
                );

                debug('Login to NE : ' . $ne  . ' [' . $ip . ']');

                if(($t->match_idx eq 1) or ($t->match_idx eq 5)) {
                  $t->send($user);
                }
                $t->{Prompt} = ['Username:', $prompt_name.'>','Password:', $prompt_name.'#'];
                if($t->match_idx eq 3) {
                  $t->send($password_login);
                }

                if($t->match_idx ne 4) {

                        if($t->match_idx ne 2) {
                          # ERROR, don't see standard > prompt, exiting ...
                          debug("No '>' prompt...exiting...");
                          exit(0);
                        }

                        $cmd = 'terminal length 0';
                        $t->send($cmd);
                        if($t->match_idx eq 2) {
                                        $t->send('enable');
                        }

                        if($t->match_idx eq 3) {
                                        $t->send($password_enable);
                        }

                        if($t->match_idx ne 4) {
                                        debug("No '#' prompt...exiting...");
                                        exit(0);
                        }
                } else {
                        $cmd = 'terminal length 0';
                        $t->send($cmd);
                }
    }




Sakwise script nang nduwur wis mari lan iso connect, nah saiki carane kirim perintah utowo command line nang switch:



 $cmd = 'sh clock';
        $t->send($cmd);
        ($res = $t->before) =~ tr/\r//d;
                @lines = split("\n", $res);

                foreach(@lines)
                {
                        debug($_);
                        if(m/(\d+):(\d+):(\d+)\.\d+\s+\S+\s+\S+\s+(\S+)\s+(\d+)\s+(\d+)/) {
                                $str_date = $6 . '-' . $MONTH{$4} . '-' . $5 . ' ' . $1 . ':' . $2 . ':' . $3;
                                debug('date --- ' . $str_date);
                        }
                        #16:58:18.389 WIB Tue Jul 30 2013
                }



nek wes mari kari ngleboke nang mysql carane koyok ngene script pe

$dbh = DBI->connect($dsn,'monitor','monitor*1');

                        $sql =  "INSERT INTO cpu_monitoring VALUES ( '" .
                                                                $ne . "', '" . $str_date . "', '" . $str_insert . "'," .
                                                                $max1 . ", " . $max2 .
                                 ")";
                        debug('SQL: ' . $sql);
                        $dbh->do($sql);

        $dbh->disconnect();
        undef $dbh;

debug('Disconnecting from NE : ' . $ne  . ' [' . $ip . ']');
$t->_disconnect;



Iki paling akhir scrip function debug gawe nggoleki masalah


sub debug {
        my $str = shift;
        if($debug eq 1) {
                $str =~ s/\s+$//g;
                if($exp_debug eq 1)
                {
                        print STDERR "\nDEBUG: $str\n";
                } else
                {
                        print STDERR "DEBUG: $str\n";
                }
 
close
==[ Klik disini 1X ] [ Close ]==
KODE IKLAN DFP 2
KODE IKLAN DFP 2