Bash script to audit Cisco configuration - bash

I'm currently writing a script to generate a report from cisco configuration for audit purposes. Using 'grep' command, I was able to successfully capture the global configurations.
But the challenge is doing it per interface. For example, I want to know which interfaces have these lines 'no ip redirects', 'no ip unreachables', etc. How can I accomplish this in bash?
Thank you in advance!

This can not be done easy with grep, but awk handle this:
cat file
!
interface GigabitEthernet0/13
description Server_32_main
spanning-tree portfast
no ip redirects
!
interface GigabitEthernet0/14
description Server_32_log
switchport access vlan 666
spanning-tree portfast
!
interface GigabitEthernet0/15
description UPS_20
spanning-tree portfast
!
As you see, each group is separated by !, so we use that to separate each record.
To get only interface name do like this:
awk -v RS="!" -F"\n" '/no ip redirects/ {print $2}' file
interface GigabitEthernet0/13
To get interface config do:
awk -v RS="!" '/no ip redirects/' file
interface GigabitEthernet0/13
description Server_32_main
spanning-tree portfast
no ip redirects
To get more patterns in one go:
awk -v RS="!" '/no ip redirects/ || /no ip unreachables/' file

Related

Ssh not working when host is taken using grep

When I'm hardcoding i. e.
server1=ip-10.237.40.10-aws-n-myhost
ssh - i /home/<passwordfile> akhil#$server1
It is working,but when I'm greping the host from other file its not working
Eg:
server2=$(grep - e host /home/akhil/configuration. File | awk FS, '{print $2} ' )
echo $server2
ssh - i /home/<passwordfile> akhil#$server2
While printing server 2 is fine, but when it is used in ssh I'm getting a error saying : not known host name.
I want to automate the script with a configuration file so that when ever the cluster changes I could simply change the ip address in configfile instead of changing in all my scripts.
I need help in this.
Thanks

Recursive text search whithin lines before the grep match in shell

I need help working with router config backup database. I need to get a list of interfaces that don't have vrf or shutdown in their configuration.
I get the list of all interfaces config passing the config file through awk '/^interface/,/!/'. This gives me the output below:
interface TenGigE0/3/0/0
description
service-policy output QOS
ipv4 mtu 1500
ipv4 address 13.24.15.3 255.255.255.252
carrier-delay up 3000 down 0
load-interval 30
dampening
!
interface TenGigE0/3/0/1
description Link To
!
interface TenGigE0/3/0/1.302
description
vrf 1671
ipv4 address 13.24.14.11 255.255.255.254
encapsulation dot1q 302
Now, i am stuck trying to exclude the interfaces that contain vrf line. What i was trying to do is to grep for vrf, and when there is a match, remove the line that contains the word "interface" above. Unfortunately with no luck. Maybe someone has a more sophisticated solution.
If you have the structured records awk can solve this problem. Given your intermediate file
2$ awk 'BEGIN{RS=ORS="!\n"} !/vrf/' interface
will print the records without "vrf"
interface TenGigE0/3/0/0
description
service-policy output QOS
ipv4 mtu 1500
ipv4 address 13.24.15.3 255.255.255.252
carrier-delay up 3000 down 0
load-interval 30
dampening
!
interface TenGigE0/3/0/1
description Link To
!

need to read IP from a text and set manually on centos

i have 10 IP which have been listed on a CSV or text file , i need to read each time one line and get the IP and set on eth0 interface of the server, i found the bellow script which some how show me how to create new network setting but i do not know how i could read one line from CSV and put it on variable to use with bellow script . i would be greatly thankful if you give me some hint , thanks
https://wiki.gogrid.com/index.php/Customer:Automatically_convert_your_Linux_server_to_a_static_IP
May something like this:
#!/bin/sh
lineNumber=1
ip=`head -n $lineNumber test.csv | tail -n 1 | line`
echo $ip

Use AWK to extract just the MAC addresses from a show mac address-table from a cisco switch

I'd need to take the output from a show mac address-table on a Cisco switch and extract only the Mac addresses and put them in a CSV file.
The output looks like this
vlan Mac Address Type Ports
----- ----------- ----- -----
All 0011.2233.4455 STATIC CPU
All 0011.2233.4466 STATIC CPU
All 0011.2233.4477 STATIC CPU
All 0011.2233.4488 STATIC CPU
Macs are displayed in groups of 4 as seen above. I need to grab each MAC address and output it to a csv file.
This works but it also grabs unwanted output.
awk '{print $2}' macTable.log > macTable.csv
Just add a condition that the field only contains digits and dots:
awk '$2~/^[0-9.]+$/{print $2}' macTable.log
You can test for the two types of mac-address STATIC and DYNAMIC
awk 'tolower($3)~/static|dynamic/ {print $2}' log
0011.2233.4455
0011.2233.4466
0011.2233.4477
0011.2233.4488
PS you need to use tolower since switch may use uppercase and routers uses lowercase.
You could also do it with grep:
egrep -o '([0-9]{4}\.){2}[0-9]{4}'
Output:
0011.2233.4455
0011.2233.4466
0011.2233.4477
0011.2233.4488

SED Escape in bash script

My tempfile.txt has below contents [Typical Cisco Configuration File]
interface GigabitEthernet1/11
no ip address
shutdown
rmon collection stats 6010 owner monitor
!
interface GigabitEthernet1/12
no ip address
shutdown
rmon collection stats 6011 owner monitor
!
interface GigabitEthernet1/13
no ip address
shutdown
rmon collection stats 6012 owner monitor
!
interface GigabitEthernet1/14
no ip address
shutdown
rmon collection stats 6013 owner monitor
!
I am trying to loop through to do some operation on each block
Example
interface GigabitEthernet1/14 --> start of interface block
no ip address
shutdown
rmon collection stats 6013 owner monitor
! --> end of interface block
I have another file which has below data intefaces.txt
interface GigabitEthernet1/11
interface GigabitEthernet1/12
interface GigabitEthernet1/13
interface GigabitEthernet1/14
Now want to do some changes in each block thats why I am trying to access block in loop via
while read line
do
sed -n '/$line/,/!/p' tempfile.txt # this should get me block
#code for doing some changes in block
done < interfaces.txt
Now since my $line will be one of line of interfaces.txt eg: interface GigabitEthernet1/11
how can I escape / as I am doing in loop & my interfaces will always contain / character
Tell sed which character to use in place of / in the search by prefixing it with a backslash:
sed -n "\#${line}#,/!/p" ...
To work with block of text, use gnu awk (due to multiple characters in RS)
awk -vRS="interface" '/1\/13/ {print RS $0}' file
interface GigabitEthernet1/13
no ip address
shutdown
rmon collection stats 6012 owner monitor
!
Here you get all of info for interface 1/13
You can prevent the escaping of / by getting it in as a variable.
awk -vRS="interface" -vtest="1/13" '$0~test {print RS $0}'
interface GigabitEthernet1/13
no ip address
shutdown
rmon collection stats 6012 owner monitor
!
In bash, you can use the parameter global replace operator, ${varname//old/new}, to replace the slashes with backslashed slashes, like this:
while read line; do
sed -n "/${line//\//\/\/}/",'/!/p' tempfile.txt | # do stuff
done <interfaces.txt
... but Holy Leaning Toothpicks, Batman! I would go with #JonathanLeffler's solution of changing sed's search delimiter.

Resources