regex - Saved format output in columns [grep, sed, awk or ?] -


i have created expect script file telnet multiple +200 switches.

my code:

#!/usr/bin/expect -f #slurp input file set fp [open "ip.txt" r] # avoid empty lines, 'nonewline' flag used set file_data [read -nonewline $fp] close $fp set prompt ">" log_file -noappend switch_port_status.txt foreach ip [split $file_data "\n"] { puts "switch $ip interface status" spawn telnet $ip expect "username:" send "myusername\r" expect "assword:" send "mypassword\r" expect $prompt # avoid sending 'enter' key on huge configurations send "show ip int br\r" expect { -ex "--more--" { send -- " "; exp_continue } "*>" { send "exit\r" } } #expect $prompt set timeout 1; # reverting default timeout # sending 'exit' @ global level prompt close connection expect eof 

for expect script "log_file -noappend switch_port_status.txt", have saved output file below:

trying 192.168.0.1... connected 192.168.0.1 (192.168.0.1). escape character '^]'.  authorized access system property of company name disconnect if not authorized users!   user access verification  username: myusername password:  switch-hostname1>show ip int br interface              ip-address  ok? method          status                 protocol vlan1                  unassigned  yes  nvram          administratively down  down vlan308                192.168.0.1 yes  nvram                              vlan603                unassigned  yes  nvram          administratively down  down gigabitethernet0/1     unassigned  yes  unset                              gigabitethernet0/2     unassigned  yes  unset          down                   down longreachethernet0/1   unassigned  yes   unset         administratively down  down longreachethernet0/2   unassigned  yes   unset         administratively down  down longreachethernet0/3   unassigned  yes   unset         administratively down  down longreachethernet0/4   unassigned  yes   unset         administratively down  down longreachethernet0/5   unassigned  yes   unset         administratively down  down longreachethernet0/6   unassigned  yes   unset                             longreachethernet0/7   unassigned  yes   unset                             longreachethernet0/8   unassigned  yes   unset         administratively down  down longreachethernet0/9   unassigned  yes   unset         administratively down  down longreachethernet0/10 unassigned  yes   unset                             longreachethernet0/11 unassigned  yes   unset         administratively down  down longreachethernet0/12 unassigned  yes   unset         administratively down  down longreachethernet0/13 unassigned  yes   unset         administratively down  down longreachethernet0/14 unassigned  yes   unset         administratively down  down longreachethernet0/15 unassigned  yes   unset         administratively down  down longreachethernet0/16 unassigned  yes   unset                             longreachethernet0/17 unassigned  yes   unset         administratively down  down longreachethernet0/18 unassigned  yes   unset         administratively down  down longreachethernet0/19 unassigned  yes   unset         administratively down  down longreachethernet0/20 unassigned  yes   unset                             longreachethernet0/21 unassigned  yes   unset         administratively down  down longreachethernet0/22 unassigned  yes   unset         administratively down  down longreachethernet0/23 unassigned  yes   unset         administratively down  down longreachethernet0/24 unassigned  yes   unset         administratively down  down switch-hostname1>exit connection closed foreign host. trying 192.168.0.2... connected 192.168.0.2 (192.168.0.2). escape character '^]'.  authorized access system property of company name disconnect if not authorized users!   user access verification  username: myusername password:  switch-hostname2>show ip int br interface             ip-address  ok? method   status                protocol vlan1                 unassigned  yes nvram    administratively down down vlan308               192.168.0.2 yes nvram                       vlan603               unassigned  yes nvram    administratively down down gigabitethernet0/1    unassigned  yes unset                       gigabitethernet0/2    unassigned  yes unset    down                  down longreachethernet0/1  unassigned  yes unset    administratively down down longreachethernet0/2  unassigned  yes unset    administratively down down longreachethernet0/3  unassigned  yes unset    administratively down down longreachethernet0/4  unassigned  yes unset    down                  down longreachethernet0/5  unassigned  yes unset    administratively down down longreachethernet0/6  unassigned  yes unset    administratively down down longreachethernet0/7  unassigned  yes unset    administratively down down longreachethernet0/8  unassigned  yes unset    administratively down down longreachethernet0/9  unassigned  yes unset    administratively down down longreachethernet0/10 unassigned yes unset     administratively down down longreachethernet0/11 unassigned yes unset     administratively down down longreachethernet0/12 unassigned yes unset     administratively down down longreachethernet0/13 unassigned yes unset     administratively down down longreachethernet0/14 unassigned yes unset     administratively down down longreachethernet0/15 unassigned yes unset     administratively down down longreachethernet0/16 unassigned yes unset     administratively down down longreachethernet0/17 unassigned yes unset     administratively down down longreachethernet0/18 unassigned yes unset     up longreachethernet0/19 unassigned yes unset     administratively down down longreachethernet0/20 unassigned yes unset     administratively down down longreachethernet0/21 unassigned yes unset     administratively down down longreachethernet0/22 unassigned yes unset     up longreachethernet0/23 unassigned yes unset     administratively down down longreachethernet0/24 unassigned yes unset     administratively down down switch-hostname2>trying 192.168.0.3... connected 192.168.0.3 (192.168.0.3). escape character '^]'.  authorized access system property of company name disconnect if not authorized users!   user access verification  username: myusername password:  switch-hostname3#show ip int br interface            ip-address  ok? method     status protocol vlan1                192.168.0.3 yes nvram          fastethernet0/1      unassigned  yes unset          fastethernet0/2      unassigned  yes unset          fastethernet0/3      unassigned  yes unset          fastethernet0/4      unassigned  yes unset          fastethernet0/5      unassigned  yes unset          fastethernet0/6      unassigned  yes unset          fastethernet0/7      unassigned  yes unset          fastethernet0/8      unassigned  yes unset      down   down fastethernet0/9      unassigned  yes unset      down   down fastethernet0/10    unassigned  yes unset       down   down fastethernet0/11    unassigned  yes unset       down   down fastethernet0/12    unassigned  yes unset       down   down fastethernet0/13    unassigned  yes unset       down   down fastethernet0/14    unassigned  yes unset       down   down fastethernet0/15    unassigned  yes unset       down   down fastethernet0/16    unassigned  yes unset       down   down fastethernet0/17    unassigned  yes unset       down   down fastethernet0/18    unassigned  yes unset       down   down fastethernet0/19    unassigned  yes unset       down   down fastethernet0/20    unassigned  yes unset       down   down fastethernet0/21    unassigned  yes unset       down   down fastethernet0/22    unassigned  yes unset           fastethernet0/23    unassigned  yes unset       down   down fastethernet0/24    unassigned  yes unset       down   down gigabitethernet0/1  unassigned  yes unset           gigabitethernet0/2  unassigned  yes unset       down   down switch-hostname4>exit connection closed foreign host.  trying 192.168.0.4... connected 192.168.0.4 (192.168.0.4). escape character '^]'.  authorized access system property of company name disconnect if not authorized users!   user access verification  username: myusername password:  switch-hostname4#show ip int br 

............so on , forth.

can done awk/sed/grep utility in linux??

desired output format:

192.168.0.1 switch-hostname1 vlan1,administratively down vlan308,up  vlan603,administratively down gigabitethernet0/1,up  gigabitethernet0/2 ,down longreachethernet0/1,administratively down longreachethernet0/2,administratively down longreachethernet0/3,administratively down longreachethernet0/4,administratively down longreachethernet0/5,administratively down longreachethernet0/6,up longreachethernet0/7,up longreachethernet0/8,administratively down longreachethernet0/9,administratively down longreachethernet0/10,up longreachethernet0/11,administratively down longreachethernet0/12,administratively down longreachethernet0/13,administratively down longreachethernet0/14,administratively down longreachethernet0/15,administratively down longreachethernet0/16,up longreachethernet0/17,administratively down longreachethernet0/18,administratively down longreachethernet0/19,administratively down longreachethernet0/20,up longreachethernet0/21,administratively down longreachethernet0/22,administratively down longreachethernet0/23,administratively down longreachethernet0/24,administratively down  192.168.0.2 switch-hostname2 vlan1,administratively vlan308,up vlan603,administratively gigabitethernet0/1,up gigabitethernet0/2,down longreachethernet0/1,administratively longreachethernet0/2,administratively longreachethernet0/3,administratively longreachethernet0/4,down longreachethernet0/5,administratively longreachethernet0/6,administratively longreachethernet0/7,administratively longreachethernet0/8,administratively longreachethernet0/9,administratively longreachethernet0/10,administratively longreachethernet0/11,administratively longreachethernet0/12,administratively longreachethernet0/13,administratively longreachethernet0/14,administratively longreachethernet0/15,administratively longreachethernet0/16,administratively longreachethernet0/17,administratively longreachethernet0/18,up longreachethernet0/19,administratively longreachethernet0/20,administratively longreachethernet0/21,administratively longreachethernet0/22,up longreachethernet0/23,administratively longreachethernet0/24,administratively  192.168.0.3 switch-hostname3 vlan1,up fastethernet0/1,up fastethernet0/2,up fastethernet0/3,up fastethernet0/4,up fastethernet0/5,up fastethernet0/6,up fastethernet0/7,up fastethernet0/8,down fastethernet0/9,down fastethernet0/10,down fastethernet0/11,down fastethernet0/12,down fastethernet0/13,down fastethernet0/14,down fastethernet0/15,down fastethernet0/16,down fastethernet0/17,down fastethernet0/18,down fastethernet0/19,down fastethernet0/20,down fastethernet0/21,down fastethernet0/22,up fastethernet0/23,down fastethernet0/24,down gigabitethernet0/1,up gigabitethernet0/2,down  192.168.0.4 switch-hostname4 

............so on , forth.

thanks in advance.

as perl implementation, i'd suggest this:

#!/usr/bin/perl -nl  print "\n$1" if (/connected (\s+)/); if ($line_num = (/^(\s+?)(?:>|#)show ip int br/ ... /^.*?(?:>|#)/)) {     if ($line_num == 1) {         print $1;     } elsif ($line_num > 2 && /^(\s+)\s+\s+\s+\s+\s+\s+\s+(.*)\s+\s+$/)  {         print "$1;$2";     } elsif ($line_num > 2 && /^\s*$/)  {   # retian empty lines after                                              # 'show ip int br' if there         print;     } } 

usage: squeeze.pl your_swicth_status_script_output.txt squeeze.pl name of script above.


Comments

Popular posts from this blog

Magento/PHP - Get phones on all members in a customer group -

php - .htaccess mod_rewrite for dynamic url which has domain names -

Website Login Issue developed in magento -