经常出差,有不少客户用的是静态IP,总是手工设置感觉很麻烦,所以写了这个小工具。
- #!/usr/bin/perl
- #use Acme::PerlTidy;
- package IpChange;
- use Cwd;
- sub new {
- my $self = shift;
- my $class = ref($self) || $self;
- my $rh = {};
- $rh->{type} = shift;
- $rh->{ip} = shift;
- $rh->{gateway} = shift;
- $rh->{dns} = shift;
- bless $rh, $class;
- return $rh;
- }
- sub SetIp {
- my $self = shift;
- my $dir = Cwd::getcwd();
- my $file = $dir . '/IPconfig';
- $self->_Write( $self->{type}, $self->{ip}, $self->{gateway},
- $self->{dns}, $file );
- system "netsh -f $file";
- }
- sub _Write {
- my ( $self, $type, $ip, $gateway, $dns, $file ) = @_;
- my $msg = <<"EOF";
- # ----------------------------------
- # 接口 IP 配置
- # ----------------------------------
- pushd interface ip
- set address name="$type" source=static addr=$ip mask=255.255.255.0
- set address name="$type" gateway=$gateway gwmetric=0
- set dns name="$type" source=static addr=$dns register=PRIMARY
- set wins name="$type" source=static addr=none
- popd
- # 接口 IP 配置结束
-
- EOF
- open my $fh, ">$file";
- print $fh $msg, "\n";
- close $fh;
- }
- package main;
- use POE;
- #################################################################
- #预设不同单位的IP地址
- my $hip = {
- '单位1' => {
- 'ip' => '192.168.0.128',
- 'gateway' => '192.168.0.1',
- 'dns' => '61.139.2.69',
- },
- '单位2' => {
- 'ip' => '192.168.0.129',
- 'gateway' => '192.168.0.1',
- 'dns' => '61.139.2.69',
- },
- };
- #################################################################
- #print $hip->{'单位1'}{'ip'},"\n";
- POE::Session->create(
- inline_states => {
- _start => \&Start,
- chooseFirm => \&chooseFirm,
- Set => \&Set,
- Exit => \&Exit,
- },
- );
- POE::Kernel->run;
- #选择网络连接类型
- sub Start {
- my ( $kernel, $heap ) = @_[ KERNEL, HEAP ];
- my @array = qw(本地连接 无线网络连接);
- push( @array, Exit );
- my $prompt = '请选择网络连接类型:';
- my $type = _choose( $prompt, @array );
- if ( $type eq 'Exit' ) {
- $kernel->yield(Exit);
- }
- else {
- #向chooseFirm()传递参数
- $kernel->yield( chooseFirm => $type );
- }
- }
- #接受输入
- sub _choose {
- my ( $prompt, @array ) = @_;
- return '' unless @array;
- print $prompt, "\n";
- print " $_: $array[$_]\n" for ( 0 .. $#array );
- my $n = <>;
- return $array[$n] if $n =~ /^\d+$/ and defined $array[$n];
- }
- sub chooseFirm {
- my ( $kernel, $heap, $type ) = @_[ KERNEL, HEAP, ARG0 ];
- my @array = keys( %{$hip} );
- push( @array, Exit );
- my $prompt = '请选择设置IP的单位:';
- my $firm = _choose( $prompt, @array );
- if ( $firm eq 'Exit' ) {
- $kernel->yield(Exit);
- }
- else {
-
- #将"网络连接类型"和"单位"传给package IpChange的Set()函数
- $kernel->yield( Set => ( $type, $firm ) );
- }
- }
- sub Set {
- my ( $kernel, $heap ) = @_[ KERNEL, HEAP ];
- my ( $type, $firm ) = @_[ ARG0 .. $#_ ];
- #从package main散列的引用$hip和chooseFirm()取得Set()函数所需参数
- my ( $ip, $gateway, $dns ) =
- ( $hip->{$firm}{'ip'}, $hip->{$firm}{'gateway'}, $hip->{$firm}{'dns'} );
- #实例化IpChange设置IP,传入网络连接类型、IP、gateway和dns
- print '请你耐心等候...',"\n";
- my $change = IpChange->new( $type, $ip, $gateway, $dns );
- $change->SetIp();
- print 'Done!',"\n";
- sleep(2);
- $kernel->yield(Exit);
- }
- sub Exit { exit 0 }
阅读(2425) | 评论(0) | 转发(0) |