Chinaunix首页 | 论坛 | 博客
  • 博客访问: 2137500
  • 博文数量: 227
  • 博客积分: 10521
  • 博客等级: 上将
  • 技术积分: 3452
  • 用 户 组: 普通用户
  • 注册时间: 2006-10-20 14:59
个人简介

低调做人,高调做事!

文章分类

全部博文(227)

文章存档

2013年(4)

2012年(8)

2011年(16)

2010年(24)

2009年(92)

2008年(83)

分类: 系统运维

2009-06-06 13:18:58

上段时间已经把公司网络结构进行改造,统一换成了可以网管的S3100交换机,在对交换机配置了远程管理后,对工作效率有了很大的提高,下步就要实现交换机端口的ip和mac地址绑定,以实现更安全的管理。

由于采用了DHCP进行地址分配,已经可以获得ip和mac地址的对应关系,那么只需要在交换机上取得每个端口下的mac地址就可以了,然后就可以用perl来实现绑定操作的自动输出,下面一步一步进行说明。


1,转换mac地址格式
从交换机上取得的mac地址格式是“aaaa-bbbb-cccc”,但是DHCP那么的数据是这样的:
192.168.1.10 00247E14CFE8
192.168.1.11 001AA0275EB4

所以我们的把地址格式转换成交换机可以识别的,并且字母改成小写,,程序代码如下:

#!/usr/bin/perl -w

use strict;

my $filename = '/opt/switch/dhcp_data';
open FH, "< $filename" or die "can't open $filename  ..... ($!)";

foreach () {
    if ( $_ =~ /^\s*$/ ) {
        next;
    }

    my $rec = lc($_);
    my ( $ip, $mac ) = split( / +/, $_ );

    my $mac_1 = substr( $mac, 0, 4 );
    my $mac_2 = substr( $mac, 4, 4 );
    my $mac_3 = substr( $mac, 8, 4 );

    print "$ip $mac_1-$mac_2-$mac_3\n";

}


执行结果如下:
[root@localhost switch]# ./chang_format.pl
192.168.1.10 0024-7e14-cfe8
192.168.1.11 001a-a027-5eb4



2,转换完格式后,那么就要开始收集交换机每个端口下的mac地址信息。

交换机执行命令如下:
display mac-address interface Ethernet1/0/1
.......
display mac-address interface Ethernet1/0/24


最后获得的数据如下:
[WS-T402-A]display mac-address interface Ethernet1/0/1
No Mac address on the port!
[WS-T402-A]display mac-address interface Ethernet1/0/2
MAC ADDR        VLAN ID   STATE          PORT INDEX               AGING TIME(s)
0023-ae66-7a3f  10        Learned        Ethernet1/0/2            AGING

  ---  1 mac address(es) found on port Ethernet1/0/2 ---
[WS-T402-A]display mac-address interface Ethernet1/0/3
MAC ADDR        VLAN ID   STATE          PORT INDEX               AGING TIME(s)
0023-ae6d-05cb  10        Learned        Ethernet1/0/3            AGING

  ---  1 mac address(es) found on port Ethernet1/0/3 ---
[WS-T402-A]display mac-address interface Ethernet1/0/4
No Mac address on the port!
[WS-T402-A]display mac-address interface Ethernet1/0/5
MAC ADDR        VLAN ID   STATE          PORT INDEX               AGING TIME(s)
0019-d150-5353  5         Learned        Ethernet1/0/5            AGING


然后我们需要一个主程序来获得ip,mac,端口的信息,并实现批量绑定输出。
这个程序实现过程如下,过滤掉从交换机上获得的其他行数据,只保留含有mac地址的那一行,然后和DHCP的数据表进行比较,找出这2个文件都含有相同mac地址的信息,最后输出就可以了,代码如下:


#!/usr/bin/perl -w

use strict;

my $switch_data = '/opt/switch/switch_data';
    open SD, "< $switch_data" or die "can't open $switch_data  ..... ($!)";

my $ip_mac = '/opt/switch/ip_mac';
    open IM, "< $ip_mac" or die "can't open $ip_mac  ..... ($!)";

my @lines = ;
close IM;

foreach () {

    if ( $_ =~ /^\d/ ) {

        my ( $mac, $vlan, $state, $port, $agent ) = split( / +/, $_ );

        #print "$mac\t $port\n";

        foreach (@lines) {
            chomp $_;
            if ( $_ =~ /$mac/ ) {

                my ( $ip, $mac_1 ) = split( / +/, $_ );
                print "interface $port\n";
                print "am user-bind mac-addr $mac ip-addr $ip\n";
            }
        }

    }

}


输出结果如下:
[root@localhost switch]# ./am.pl
interface Ethernet1/0/5
am user-bind mac-addr 0019-d150-5353 ip-addr 192.168.1.105
interface Ethernet1/0/6
am user-bind mac-addr 001e-c954-15dc ip-addr 192.168.1.212
interface Ethernet1/0/7
am user-bind mac-addr 0013-20d0-9db9 ip-addr 192.168.1.146


剩下的工作就是在交换机上执行 am.pl 文件输出的结果就ok了。


这样就大大的较少了人工查找的时间及在交换机上绑定的误操作,以方便同事在交换机上的执行。

阅读(3065) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~