Chinaunix首页 | 论坛 | 博客
  • 博客访问: 225724
  • 博文数量: 36
  • 博客积分: 1188
  • 博客等级: 军士长
  • 技术积分: 802
  • 用 户 组: 普通用户
  • 注册时间: 2010-04-08 21:45
文章分类

全部博文(36)

文章存档

2020年(1)

2017年(2)

2015年(1)

2014年(1)

2013年(1)

2012年(3)

2011年(27)

分类: WINDOWS

2012-04-17 01:31:29

经常出差,有不少客户用的是静态IP,总是手工设置感觉很麻烦,所以写了这个小工具。

点击(此处)折叠或打开

  1. #!/usr/bin/perl
  2. #use Acme::PerlTidy;

  3. package IpChange;
  4. use Cwd;

  5. sub new {
  6.     my $self = shift;
  7.     my $class = ref($self) || $self;
  8.     my $rh = {};
  9.     $rh->{type} = shift;
  10.     $rh->{ip} = shift;
  11.     $rh->{gateway} = shift;
  12.     $rh->{dns} = shift;
  13.     bless $rh, $class;
  14.     return $rh;
  15. }

  16. sub SetIp {
  17.     my $self = shift;
  18.     my $dir = Cwd::getcwd();
  19.     my $file = $dir . '/IPconfig';

  20.     $self->_Write( $self->{type}, $self->{ip}, $self->{gateway},
  21.         $self->{dns}, $file );
  22.     system "netsh -f $file";

  23. }

  24. sub _Write {
  25.     my ( $self, $type, $ip, $gateway, $dns, $file ) = @_;

  26.     my $msg = <<"EOF";

  27. # ----------------------------------
  28. # 接口 IP 配置
  29. # ----------------------------------
  30. pushd interface ip

  31. set address name="$type" source=static addr=$ip mask=255.255.255.0
  32. set address name="$type" gateway=$gateway gwmetric=0
  33. set dns name="$type" source=static addr=$dns register=PRIMARY
  34. set wins name="$type" source=static addr=none

  35. popd
  36. # 接口 IP 配置结束
  37.     
  38. EOF

  39.     open my $fh, ">$file";
  40.     print $fh $msg, "\n";
  41.     close $fh;
  42. }

  43. package main;

  44. use POE;
  45. #################################################################
  46. #预设不同单位的IP地址
  47. my $hip = {
  48.     '单位1' => {
  49.         'ip' => '192.168.0.128',
  50.         'gateway' => '192.168.0.1',
  51.         'dns' => '61.139.2.69',
  52.     },
  53.     '单位2' => {
  54.         'ip' => '192.168.0.129',
  55.         'gateway' => '192.168.0.1',
  56.         'dns' => '61.139.2.69',
  57.     },
  58. };
  59. #################################################################
  60. #print $hip->{'单位1'}{'ip'},"\n";

  61. POE::Session->create(
  62.     inline_states => {
  63.         _start => \&Start,
  64.         chooseFirm => \&chooseFirm,
  65.         Set => \&Set,
  66.         Exit => \&Exit,
  67.     },
  68. );
  69. POE::Kernel->run;
  70. #选择网络连接类型
  71. sub Start {
  72.     my ( $kernel, $heap ) = @_[ KERNEL, HEAP ];
  73.     my @array = qw(本地连接 无线网络连接);
  74.     push( @array, Exit );
  75.     my $prompt = '请选择网络连接类型:';
  76.     my $type = _choose( $prompt, @array );
  77.     if ( $type eq 'Exit' ) {
  78.         $kernel->yield(Exit);
  79.     }
  80.     else {

  81.         #向chooseFirm()传递参数
  82.         $kernel->yield( chooseFirm => $type );
  83.     }
  84. }
  85. #接受输入
  86. sub _choose {
  87.     my ( $prompt, @array ) = @_;
  88.     return '' unless @array;
  89.     print $prompt, "\n";
  90.     print " $_: $array[$_]\n" for ( 0 .. $#array );
  91.     my $n = <>;
  92.     return $array[$n] if $n =~ /^\d+$/ and defined $array[$n];
  93. }

  94. sub chooseFirm {
  95.     my ( $kernel, $heap, $type ) = @_[ KERNEL, HEAP, ARG0 ];
  96.     my @array = keys( %{$hip} );
  97.     push( @array, Exit );
  98.     my $prompt = '请选择设置IP的单位:';
  99.     my $firm = _choose( $prompt, @array );
  100.     if ( $firm eq 'Exit' ) {
  101.         $kernel->yield(Exit);
  102.     }
  103.     else {
  104.     
  105.     #将"网络连接类型""单位"传给package IpChange的Set()函数
  106.         $kernel->yield( Set => ( $type, $firm ) );
  107.     }

  108. }

  109. sub Set {
  110.     my ( $kernel, $heap ) = @_[ KERNEL, HEAP ];
  111.     my ( $type, $firm ) = @_[ ARG0 .. $#_ ];

  112.     #从package main散列的引用$hip和chooseFirm()取得Set()函数所需参数
  113.     my ( $ip, $gateway, $dns ) =
  114.       ( $hip->{$firm}{'ip'}, $hip->{$firm}{'gateway'}, $hip->{$firm}{'dns'} );

  115.     #实例化IpChange设置IP,传入网络连接类型、IP、gateway和dns
  116.     print '请你耐心等候...',"\n";
  117.     my $change = IpChange->new( $type, $ip, $gateway, $dns );
  118.     $change->SetIp();
  119.     print 'Done!',"\n";
  120.     sleep(2);
  121.     $kernel->yield(Exit);
  122. }
  123. sub Exit { exit 0 }

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