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

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

Related

Get physical keyboard layout

Is there a way through command line to know what keyboard physical layout a macbook has? I cannot find that specific information in system_profiler. I want to know if the computer has a US, UK, ES, etc keyboard layout.
The aim is to script it, upload it to my MDM so I can run it in all the company's laptops and find out that information.
Thanks!
running the terminal command system_profiler SPSPIDataType
it shows this:
SPI:
Apple Internal Keyboard / Trackpad:
Product ID: 0x0278
Vendor ID: 0x05ac (Apple Inc.)
ST Version: 8.96
MT Version: 4.69
Serial Number: FM7741701QVGN41A5+RNZ
Manufacturer: Apple Inc.
Location ID: 0x01000000
I run it in two different macbooks, one UK, one ES. I was hoping that the Location ID would show something different but that was not the case.
If I do this in Terminal:
defaults read ~/Library/Preferences/com.apple.HIToolbox.plist AppleSelectedInputSources | egrep -w 'KeyboardLayout Name'
I get this:
"KeyboardLayout Name" = *name of my keyboard in readable form*
I think I just found the solution, like this:
ioreg -l | grep KeyboardLanguage | awk '{print substr( $0, 56, 20)}' | tr -d "\" =|"
Thanks,
Federico.

compare 2 csv files in shell using awk

Thanks.
I have 2 csv files which I need to compare them and report back if it's different. file format is same in both files and even first column data (column A) in both files have same content(it's header info).
Tried using awk command but have there is conditions which am not sure how to implement.
conditions :
a. Need to exclude first 2 rows (since those are not required for comparison). can this be achieved by doing :
NFR=NR > 2
b. If any of the value differ then in output need to report back with header info and it's respective servername along with values.
File1.csv :
Status Check
APP servers
Server name,abc,def,ghi,jkl,mno,
Summary,,,,,,
System Start Time,Nov/12/2016 20:12:24 GMT,Nov/12/2016 20:15:38 GMT,Nov/12/2016 20:15:37 GMT,Nov/12/2016 20:15:57 GMT,Nov/12/2016 20:11:42 GMT,
System Life Time,118day.14hr.15min.19sec,118day.14hr.12min.01sec,118day.14hr.12min.03sec,118day.14hr.11min.44sec,118day.14hr.16min.01sec,
OS Version,SunOS 5.10,SunOS 5.10,SunOS 5.10,SunOS 5.10,SunOS 5.10,
Service Pack Version,Generic_147148-26,Generic_147148-26,Generic_147148-26,Generic_147148-26,Generic_147148-26,
State,Up,Up,Up,Up,Up,
File2.csv :
Status Check
APP servers
Server name,abc,def,ghi,jkl,mno,
Summary,,,,,,
System Start Time,Nov/13/2016 20:12:24 GMT,Nov/13/2016 20:15:38 GMT,Nov/13/2016 20:15:37 GMT,Nov/13/2016 20:15:57 GMT,Nov/13/2016 20:11:42 GMT,
System Life Time,118day.14hr.15min.19sec,118day.14hr.12min.01sec,118day.14hr.12min.03sec,118day.14hr.11min.44sec,118day.14hr.16min.01sec,
OS Version,SunOS 5.10,SunOS 5.10,SunOS 5.11,SunOS 5.12,SunOS 5.10,
Service Pack Version,Generic_147148-26,Generic_147148-26,Generic_147148-26,Generic_147148-26,Generic_147148-26,
State,Down,Up,Down,Up,Down,
Result/Output:
OS Version value is different for server name ghi and jkl : 5.11,5.12
State value is different for server name abc, ghi and mno : Down,Down,Down
Is it possible to exclude 5/6 column for comparison as well since that will be date/time related so not required for comparison.
can just give key value(say column b/c) only those specific columns data gets compared b/w files ?
this may give you an idea how to approach the problem
$ paste -d, file{1,2} |
awk -F, 'NR<3 {next}
NR==3 {n=split($0,h); m=n/2}
NR!=5 && NR!=6 {for(i=2;i<=m-1;i++)
if($i!=$(i+m)) print $1,h[i],$i,$(i+m)}'
OS Version ghi SunOS 5.10 SunOS 5.11
OS Version jkl SunOS 5.10 SunOS 5.12
State abc Up Down
State ghi Up Down
State mno Up Down
your output formatting can be added but will complicate the code. Since your values contain space you may want to keep comma as output field separator as well.

What could prevent frequently switching default source ip of a machine with several interfaces

The goal was to frequently change default outgoing source ip on a machine with multiple interfaces and live ips.
I used ip route replace default as per its documentation and let a script run in loop for some interval. It changes source ip fine for a while but then all internet access to the machine is lost. It has to be remotely rebooted from a web interface to have any thing working
Is there any thing that could possibly prevent this from working stably. I have tried this on more than one servers?
Following is a minimum example
# extract all currently active source ips except loopback
IPs="$(ifconfig | grep 'inet addr:'| grep -v '127.0.0.1' | cut -d: -f2 |
awk '{ print $1}')"
read -a ip_arr <<<$IPs
# extract all currently active mac / ethernet addresses
Int="$(ifconfig | grep 'eth'| grep -v 'lo' | awk '{print $1}')"
read -a eth_arr <<<$Int
ip_len=${#ip_arr[#]}
eth_len=${#eth_arr[#]}
i=0;
e=0;
while(true); do
#ip route replace 0.0.0.0 dev eth0:1 src 192.168.1.18
route_cmd="ip route replace 0.0.0.0 dev ${eth_arr[e]} src ${ip_arr[i]}"
echo $route_cmd
eval $route_cmd
sleep 300
(i++)
(e++)
if [ $i -eq $ip_len ]; then
i=0;
e=0;
echo "all ips exhausted - starting from first again"
# break;
fi
done
I wanted to comment, but as I'm not having enough points, it won't let me.
Consider:
Does varying the delay time before its run again change the number of iterations before it fails?
Exporting the ifconfig & routes every time you change it, to see if something is meaningfully different over time. Maybe some basic tests to it (ping, nslookup, etc) Basically, find what is exactly going wrong with it. Also exporting the commands you send to a logfile (text file per change?) to see changes in them to see if some is different after x iterations.
What connectivity is lost? Incoming? Outgoing? Specific applications?
You say you use/do this on other servers without problems?
Are the IP's: Static (/etc/network/interfaces), bootp/DHCP, semi-static (bootp/DHCP server serving, based on MAC address), and if served by bootp/DHCP, what is the lease duration?
On the last remark:
bootp/dhcp will give IP's for x duration. say its 60 minutes. After half that time it will "check" with the bootp/dhcp server if it can keep the IP, and extend the lease to 60 minutes again, this can mean a small reconfig on the ifconfig (maybe even at the same time of your script?).
hth

Bash script to audit Cisco configuration

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

How do I echo username, full name, and login time from finger into columns? I'm using bash on openSUSE13.1

Basically I have three users logged in to my machine right now. Test User1, Test User2, and Test User3.
I would like to use finger to get username, full name and the time they logged into the machine.
I would like to output the information like so:
Login Name Login Time
testuser1 Test User1 1300
testuser2 Test User2 1600
testuser3 Tesr User3 1930
I have two tabs in between Login and Name and three tabs between Login Time. The same goes for the user information below each header.
I cannot figure out how to pull this data from finger very well and I absolutely cannot figure out how to get the information into nice, neat, readable columns. Thanks in advance for any help!
This might not be perfect so you'll have to play around with substr starting and ending points. Should be good enough to get you started:
finger -s testuser1 testuser2 testuser3 | awk '{print substr($0,1,31),substr($0,46,14)}'
Try :r!finger. On my Mac, I get nice columns. YMMV.
:help :r!
Here's another way using awk:
finger -l | awk '{ split($1, a, OFS); print a[2], a[4], substr($3, 20, 6) }' FS="\n" RS= | column -t
The -l flag of finger produces a multi-line format (and is compatible with the -s flag). This is useful when fields like 'name' are absent. We can then process the records using awk in paragraph mode. In my example above, you can adjust the sub-string to suit the datespec of your choice. If you have gawk, then you'll have access to some time functions that may interest you if you wish to change the spec. Finally, you can print the fields of interest into column -t for pretty printing. HTH.

Resources