sed - Bash merge variable -
i'm trying status information vpn connections. it's done, except part return actual ph2 name:
step 1
#!/bin/bash oldifs=$ifs export ifs=`/bin/echo -ne " \t\n"` vpn="vpn-aaa-bbb-1" ph2table=$(snmpwalk 192.168.1.1 -c public -v2c fgvpntunentphase2name -m /usr/share/snmp/mibs/fortinet-fortigate-mib.mib | sed "s/fortinet-fortigate-mib:://" | sed "s/string: //" | grep $vpn)
$ph2table
contains:
fgvpntunentphase2name.71 = vpn-aaa-bbb-1-p2-0.1 fgvpntunentphase2name.72 = vpn-aaa-bbb-1-p2-0.2 fgvpntunentphase2name.73 = vpn-aaa-bbb-1-p2-0.3 fgvpntunentphase2name.74 = vpn-aaa-bbb-1-p2-0.4 fgvpntunentphase2name.75 = vpn-aaa-bbb-1-p2-1.5 fgvpntunentphase2name.76 = vpn-aaa-bbb-1-p2-1.6 fgvpntunentphase2name.77 = vpn-aaa-bbb-1-p2-1.7 fgvpntunentphase2name.78 = vpn-aaa-bbb-1-p2-1.8 fgvpntunentphase2name.79 = vpn-aaa-bbb-1-p2-2.9 fgvpntunentphase2name.80 = vpn-aaa-bbb-1-p2-3.10
step 2
clean=$(echo $ph2table | sed 's/fgvpntunentphase2name/fgvpntunentstatus/' | sed 's/=.*//')
$clean
contains:
fgvpntunentstatus.71 fgvpntunentstatus.72 fgvpntunentstatus.73 fgvpntunentstatus.74 fgvpntunentstatus.75 fgvpntunentstatus.76 fgvpntunentstatus.77 fgvpntunentstatus.78 fgvpntunentstatus.79 fgvpntunentstatus.80
step 3
export ifs=$oldifs in "$clean" ; snmpget 192.168.1.1 -c public -v2c $i -m /usr/share/snmp/mibs/fortinet-fortigate-mib.mib ; done | sed "s/fortinet-fortigate-mib::fgvpntunentstatus.//" | sed 's/= integer: //'
this return:
71 up(2) 72 down(1) 73 up(2) 74 up(2) 75 up(2) 76 up(2) 77 up(2) 78 up(2) 79 up(2) 80 up(2)
what i'm trying final output is:
vpn-aaa-bbb-1-p2-0.1 71 up(2) vpn-aaa-bbb-1-p2-0.2 72 down(1) vpn-aaa-bbb-1-p2-0.3 73 up(2) vpn-aaa-bbb-1-p2-0.4 74 up(2) vpn-aaa-bbb-1-p2-1.5 75 up(2) vpn-aaa-bbb-1-p2-1.6 76 up(2) vpn-aaa-bbb-1-p2-1.7 77 up(2) vpn-aaa-bbb-1-p2-1.8 78 up(2) vpn-aaa-bbb-1-p2-2.9 79 up(2) vpn-aaa-bbb-1-p2-3.10 80 up(2)
i can't seem find way merge things together. idea's one?
capturing stuff in variables not how it's done veteran shell script programmers. keep in pipeline.
this simple, want explain why changed things, annotated version included below.
#!/bin/bash vpn="vpn-aaa-bbb-1" snmpwalk 192.168.1.1 -c public -v2c fgvpntunentphase2name \ -m /usr/share/snmp/mibs/fortinet-fortigate-mib.mib | sed -e 's/fortinet-fortigate-mib:://' -e 's/string: //' -e "/$vpn/!d" | while read _ id; snmpget 192.168.1.1 -c public -v2c "$i" \ -m /usr/share/snmp/mibs/fortinet-fortigate-mib.mib | sed -e 's/fortinet-fortigate-mib::fgvpntunentstatus.//' -e 's/= integer: //' \ -e "s/^/$id /" done
(there isn't bash-specific here, change shebang #!/bin/sh
actually. hardly important here, if need run on systems bash large or slow or unavailable, option use dash instead perhaps attractive.)
the annotated version bit longwinded , has pesky vertical scroll bar, comments explain changed , why.
#!/bin/bash #oldifs=$ifs #export ifs=`/bin/echo -ne " \t\n"` # no need export (you don't want or need in subprocesses) # no need echo (bash has adequate internal facilities representing strings) # ifs=$' \t\n' # commented out because required parsing in sed instead vpn="vpn-aaa-bbb-1" #ph2table=$(snmpwalk 192.168.1.1 -c public -v2c fgvpntunentphase2name \ #-m /usr/share/snmp/mibs/fortinet-fortigate-mib.mib | #sed "s/fortinet-fortigate-mib:://" | sed "s/string: //" | grep $vpn) # no need use multiple processes; sed can in 1 go # no need variable -- feed pipeline snmpwalk 192.168.1.1 -c public -v2c fgvpntunentphase2name \ -m /usr/share/snmp/mibs/fortinet-fortigate-mib.mib | sed -e 's/fortinet-fortigate-mib:://' -e 's/string: //' -e "/$vpn/!d" | # following added replace "clean" take different approach # -e 's/fgvpntunentphase2name/fgvpntunentstatus/' -e 's/=.*//' | #clean=$(echo $ph2table | sed 's/fgvpntunentphase2name/fgvpntunentstatus/' | #sed 's/=.*//')| # no need variable # $ph2table absolutely needs quoted here -- "$ph2table" # don't want -- instead, keep in output #export ifs=$oldifs # no longer necessary -- see above #for in "$clean" ; # instead of loop on variable, while loop on output lines pipe # split i, equals sign (in $_, ignore), , id while read _ id; #snmpget 192.168.1.1 -c public -v2c $i \ #-m /usr/share/snmp/mibs/fortinet-fortigate-mib.mib ; done | # need quote "$i" snmpget 192.168.1.1 -c public -v2c "$i" \ -m /usr/share/snmp/mibs/fortinet-fortigate-mib.mib | #sed "s/fortinet-fortigate-mib::fgvpntunentstatus.//" | # sed 's/= integer: //' # again, use single sed invocation # factor inside while loop, , add id output sed -e 's/fortinet-fortigate-mib::fgvpntunentstatus.//' \ -e 's/= integer: //' \ -e "s/^/$id /" done
Comments
Post a Comment