Showing posts with label Script language. Show all posts
Showing posts with label Script language. Show all posts

Tuesday, May 16, 2017

Script - Expect

Below expect script can be used to access to the EXTREME switch and enter into BCM shell, and gathering the counters from BCM shell.

#!/usr/bin/expect -f
proc ap_to_file {file string} {


   set log [open $file a]
   puts $log $string
   close $log


}


set log_file_name log_[join [split [clock format \
     [clock seconds] -format %D/%H/%M/%S] /] "_"]
set f_log [open $log_file_name w]
close $f_log


if { [file exist $argv ] } {
   set f_details [open $argv r]
} else {
   puts "Invalid file name $argv";
   return;
}


gets $f_details line
set delay [lindex $line 0]
set limit [lindex $line 1]


while { [gets $f_details line] != "-1" } {
   lappend details [lindex [split $line ,] 0]
   lappend details [lindex [split $line ,] 1]
   lappend details [lindex [split $line ,] 2]
   lappend details [lindex [split $line ,] 3]
   lappend details [lindex [split $line ,] 4]
   puts "$line"
}
close $f_details
set match_max 300000
for { set i 1 } { $i <= $limit } { incr i } {
ap_to_file $log_file_name "
---------------------
Iteration $i starts
---------------------
"
   foreach { ip_connect slot_list user pwd dbg_pwd } $details {
       if { $ip_connect == "" } {
           puts "ip value Null";
           return;
       }
       while { [catch {spawn telnet $ip_connect} err] == 1 } {
          puts "Couldn't connect to $ip_connect ; retrying after 5 seconds"
          after 5000
}
       expect login
send $user\r
expect "password"
       send "$pwd\r"
       ap_to_file $log_file_name "-----------------------------------"
       ap_to_file $log_file_name "Entering switch $ip_connect"
       ap_to_file $log_file_name "-----------------------------------"
       expect " # "
       ap_to_file $log_file_name $expect_out(buffer);
       send "disable clipaging\r"
       expect " # "
       ap_to_file $log_file_name $expect_out(buffer);
       send "show version\r";
       expect "#"
       ap_to_file $log_file_name $expect_out(buffer);
       send "show log\r";
       expect {
          "*\n" {
             ap_to_file $log_file_name $expect_out(buffer);
             exp_continue;
          }
       " # " { }
       }
       send "show memory\r";
       expect {
          "*\n" {
             ap_to_file $log_file_name $expect_out(buffer);
             exp_continue;
          }
       " # " { }
       }
       ap_to_file $log_file_name $expect_out(buffer);
       send "enable debug-mode\r";
       expect "EXOS*password:";
       ap_to_file $log_file_name $expect_out(buffer);
       send $dbg_pwd\r;
       expect "debug*#"
       ap_to_file $log_file_name $expect_out(buffer);
       send "debug hal show sys-health-check\r";
       expect {
          "*\n" {
             ap_to_file $log_file_name $expect_out(buffer);
             exp_continue;
          }
       " # " { }
       }
       ap_to_file $log_file_name $expect_out(buffer);
       send "debug hal show platform fdb 11\r";
       expect {
          "*\n" {
             ap_to_file $log_file_name $expect_out(buffer);
             exp_continue;
          }
       " # " { }
       }
       ap_to_file $log_file_name $expect_out(buffer);
       send "debug msg show msg\r";
       expect {
          "*\n" {
             ap_to_file $log_file_name $expect_out(buffer);
             exp_continue;
          }
       " # " { }
       }
       ap_to_file $log_file_name $expect_out(buffer);


       foreach slot $slot_list {
           ap_to_file $log_file_name "  --------------------------------"
           ap_to_file $log_file_name "  Entering slot $slot"
           ap_to_file $log_file_name "  --------------------------------"
           send "!telnet -v1 10.0.$slot.2\r";
           expect "~ #";
           ap_to_file $log_file_name $expect_out(buffer);
           send "cat /proc/meminfo_better\r";
           expect "~ #";
           ap_to_file $log_file_name $expect_out(buffer);
           send "cat /proc/slabinfo\r";
           expect {
              "*\n" {
                 ap_to_file $log_file_name $expect_out(buffer);
                 exp_continue;
              }
           "~ #" { }
           }
           send "top -b -n1 | head -n20\r";
           expect "~ #";
           ap_to_file $log_file_name $expect_out(buffer);
           ap_to_file $log_file_name "  --------------------------------"
           ap_to_file $log_file_name "  Entering BCM shell"
           ap_to_file $log_file_name "  --------------------------------"
           send "/extr/bin/bcm.shell\r";
           send "\r";
           expect "BCM*>";
           ap_to_file $log_file_name $expect_out(buffer);
           send "0:\r";
           expect "BCM*>"
           ap_to_file $log_file_name $expect_out(buffer);
           send "async\r";
           expect "BCM*>";
           ap_to_file $log_file_name $expect_out(buffer);
           send "soc\r";
           expect "BCM*>";
           ap_to_file $log_file_name $expect_out(buffer);
           send "show error\r";
           expect "BCM*>";
           ap_to_file $log_file_name $expect_out(buffer);
           send "get L2_ENTRY_PARITY_STATUS\r";
           expect "BCM*>";
           ap_to_file $log_file_name $expect_out(buffer);
           send "get L3_DEFIP_PARITY_STATUS\r";
           expect "BCM*>";
           ap_to_file $log_file_name $expect_out(buffer);
           ap_to_file $log_file_name "    ------------------------------"
           ap_to_file $log_file_name "    Switching to unit 1"
           ap_to_file $log_file_name "    ------------------------------"
           send "1:\r";
           expect "BCM*>";
           ap_to_file $log_file_name $expect_out(buffer);
           send "async\r";
           expect "BCM*>";
           ap_to_file $log_file_name $expect_out(buffer);
           send "soc\r";
           expect "BCM*>";
           ap_to_file $log_file_name $expect_out(buffer);
           send "show error\r";
           expect "BCM*>";
           ap_to_file $log_file_name $expect_out(buffer);
           send "get L2_ENTRY_PARITY_STATUS\r";
           expect "BCM*>";
           ap_to_file $log_file_name $expect_out(buffer);
           send "get L3_DEFIP_PARITY_STATUS\r";
           expect "BCM*>";
           ap_to_file $log_file_name $expect_out(buffer);
           send "0:\r"
           expect "BCM*>"
           send "quit\r";
           expect "~ #";
           ap_to_file $log_file_name $expect_out(buffer);
           send "exit\r";
           expect " # ";
           ap_to_file $log_file_name $expect_out(buffer);
       }


       send logout\r;


}
   if { $i != $limit  } {
       after [expr $delay * 1000];
   }
   ap_to_file $log_file_name "
------------------------
Iteration $i done
------------------------"
}
puts ""
puts "-----------------------"
puts "logs are available at ./$log_file_name"
puts "-----------------------"

Monday, April 24, 2017

Linux shell script

Linux shell script can be used usefully for network engineer.
For example, I need to initiate ssh connection to extreme switch to replicate some issue caused by multiple failed ssh connections. Below script can be used to execute the command periodically every 5 seconds.


[ file name : ssh ]
c=1
while [ $c -le 1001 ]
do
ssh 10.67.73.34
sleep 5
(( c++ ))
done


[ how to execute the file ]


1. change the mode of the file.
root@nms1:~$ chmod 764 ssh
root@nms1:~$ ls -al ssh
-rwxrw-r-- 1 root root 73 Apr 25 12:42 ssh*


2. execute the file, then linux system will try to connect the switch by ssh for 1000 times.
on this example, switch will deny the connection by configuration on the switch.
root@nms1:~# ./ssh
ssh_exchange_identification: Connection closed by remote host
ssh_exchange_identification: Connection closed by remote host
ssh_exchange_identification: Connection closed by remote host
ssh_exchange_identification: Connection closed by remote host