现在有ipmac 文件.内容如下:
RFC1213-MIB::atPhysAddress.3239.1.122.224.97.83 = Hex-STRING: 00 1D 09 6B 97 E2
RFC1213-MIB::atPhysAddress.3239.1.122.224.97.84 = Hex-STRING: 00 1D 09 6B 97 A6
RFC1213-MIB::atPhysAddress.3239.1.122.224.97.85 = Hex-STRING: 00 1D 09 6B 98 2D
RFC1213-MIB::atPhysAddress.3239.1.122.224.97.86 = Hex-STRING: 00 1D 09 6B 94 A1
RFC1213-MIB::atPhysAddress.3239.1.122.224.97.87 = Hex-STRING: 00 1D 09 6B 97 9C
RFC1213-MIB::atPhysAddress.3239.1.122.224.97.88 = Hex-STRING: 00 1D 09 6B 98 1E
RFC1213-MIB::atPhysAddress.3239.1.122.224.97.89 = Hex-STRING: 00 1D 09 6B 94 83
RFC1213-MIB::atPhysAddress.3239.1.122.224.97.90 = Hex-STRING: 00 1D 09 6B 98 1E
RFC1213-MIB::atPhysAddress.3239.1.122.224.141.178 = Hex-STRING: 00 19 B9 EE 85 8C
RFC1213-MIB::atPhysAddress.3239.1.122.224.141.179 = Hex-STRING: 00 19 B9 EE 85 AD
RFC1213-MIB::atPhysAddress.3239.1.122.224.141.180 = Hex-STRING: 00 19 B9 EE 86 69
RFC1213-MIB::atPhysAddress.3239.1.122.224.141.181 = Hex-STRING: 00 19 B9 EE 86 71
RFC1213-MIB::atPhysAddress.3239.1.122.224.141.182 = Hex-STRING: 00 19 B9 EE 85 F5
交换机dis mac 看端口的mac情况如下:
0017-a43a-ae42 1 Learned GigabitEthernet0/4 AGING
00e0-fca7-68d8 1 Learned GigabitEthernet0/24 AGING
000f-1f9d-a074 1 Learned GigabitEthernet0/24 AGING
0019-b9e8-b447 1 Learned GigabitEthernet0/24 AGING
0011-43d6-0860 1 Learned GigabitEthernet0/24 AGING
0013-72fc-a5ff 1 Learned GigabitEthernet0/24 AGING
001a-4b0a-d7d6 1 Learned GigabitEthernet0/24 AGING
0017-a43f-a6be 1 Learned GigabitEthernet0/14 AGING
0017-a43a-1e0a 1 Learned GigabitEthernet0/24 AGING
0090-27a7-4bea 1 Learned GigabitEthernet0/24 AGING
0011-43d6-081b 1 Learned GigabitEthernet0/24 AGING
0017-a43f-a6b2 1 Learned GigabitEthernet0/13 AGING
目的:找出外网和他对应的端口是多少。通过服务器的Mac地址不变来实现。
先是对switch操作
1、首先剔除24口
sed '/0\/24/d' switch
0017-a43a-696a 1 Learned GigabitEthernet0/3 AGING
0017-a43a-5916 1 Learned GigabitEthernet0/5 AGING
0017-a478-0150 1 Learned GigabitEthernet0/19 AGING
0017-a48d-7e96 1 Learned GigabitEthernet0/6 AGING
0015-60a3-fb15 1 Learned GigabitEthernet0/1 AGING
0015-600b-5f86 1 Learned GigabitEthernet0/21 AGING
2、然后找出第一区域和第四区域
sed '/0\/24/d' switch|awk '{print $1 "\t" $4}'
0017-a43a-696a GigabitEthernet0/3
0017-a43a-5916 GigabitEthernet0/5
0017-a478-0150 GigabitEthernet0/19
0017-a48d-7e96 GigabitEthernet0/6
0015-60a3-fb15 GigabitEthernet0/1
0015-600b-5f86 GigabitEthernet0/21
0017-a43f-a5e8 GigabitEthernet0/11
0017-a43f-a5e4 GigabitEthernet0/10
0017-a43f-a586 GigabitEthernet0/16
3、
然后awk -F "-" '{print $2 $3}'输出第2区域,第3区域,这样找出mac+端口,形成对比点。取个名字叫switchnewer
a43a5916 GigabitEthernet0/5
a4780150 GigabitEthernet0/19
a48d7e96 GigabitEthernet0/6
60a3fb15 GigabitEthernet0/1
600b5f86 GigabitEthernet0/21
a43fa5e8 GigabitEthernet0/11
a43fa5e4 GigabitEthernet0/10
a43fa586 GigabitEthernet0/16
4、sed 's/G.*\///g'|tr a-z A-Z 把G到/之间的用空格来代替。然后转化大小写。
A43A696A 3
A43A5916 5
A4780150 19
A48D7E96 6
60A3FB15 1
600B5F86 21
5、
现在对ipmac操作: 以.作为分隔点,输出4区域以后的
cut -d. -f 4- ipmac
122.224.97.89 = Hex-STRING: 00 1D 09 6B 94 83
122.224.97.90 = Hex-STRING: 00 1D 09 6B 98 1E
122.224.141.178 = Hex-STRING: 00 19 B9 EE 85 8C
122.224.141.179 = Hex-STRING: 00 19 B9 EE 85 AD
122.224.141.180 = Hex-STRING: 00 19 B9 EE 86 69
122.224.141.181 = Hex-STRING: 00 19 B9 EE 86 71
6、
cut -d. -f 4- ipmac|awk '{print $1,$6$7$8$9}'
122.224.97.84 096B97A6
122.224.97.85 096B982D
122.224.97.86 096B94A1
122.224.97.87 096B979C
122.224.97.88 096B981c
输出外网IP 同时合并6,7,8,9。区域。取个名字叫ipamcnewer
这样第3步和第5步就形成了对比点:
找出相同的即可。
cat switchnewer |while read a b; do awk '/'$a'/ {print $1"\t"'$b'}' ipmacnewer;done
这里的read,是在while循环情况读取存为两个参数a,b $a $b 就相对于ipmacnewer来说是外部变量。引入外部变量用单引号。
如果把它写到一个文件中:
cat new.sh
#!/bin/bash -x
sed '/0\/24/d' switch|awk '{print $1 "\t" $4}'|awk -F "-" '{print $2 $3}'|sed 's/G.*\///g'|tr a-z A-Z>switchnewer
cut -d. -f 4- ipmac|awk '{print $1,$6$7$8$9}'>ipmacnewer
cat switchnewer |while read a b; do awk '/'$a'/ {print $1"\t"'$b'}' ipmacnewer;done
第二种方法:
cat shoushuai.sh
#!/bin/sh
#read the filename ipmac2 and give the section of 6,7 of value to the ip
awk '{print $6$7}' ipmac2|tr A-Z a-z|while read ip
do
#at first,read the filename called weiwei and extract the section of 1,2 of value to the myip
awk -v myip=$ip '{if(NR==FNR && $1$2==myip) print $3} {if(NR>FNR){if($6$7==myip) print $1}}' weiwei ipmac2
done
注意这个是利用数组的思想来的。
awk -v 是把变量赋值。首先把第6区域和第7区域的值循环的赋给变量IP。然后变量IP把值赋给Myip 。注意NR==FNR 也就是第一个文件weiwei 同时weiwei的第一区域和第二区域==变量myip的时候,输出第三区域,也就是端口。当NR>FNR的时候,读的是第二个文件ipmac2,这样他的第6,7区域==myip的时候 ,输出第一区域,也就是外网ip。
其中:
cat ipmac2
122.224.141.184 00 1D 09 08 69 12
122.224.141.185 00 1D 09 08 6B 18
122.224.141.186 00 1D 09 07 B7 5A
122.224.141.187 00 1D 09 07 B9 99
122.224.141.188 00 1D 09 07 B8 04
122.224.141.189 00 1D 09 07 B7 FF
cat weiwei
2c 08 11
1b c8 7
1b 2a 5
90 5c 20
e7 9a 2
75 40 19
7b 94 18
e8 49 16
阅读(1506) | 评论(0) | 转发(0) |