Chinaunix首页 | 论坛 | 博客
  • 博客访问: 48815
  • 博文数量: 19
  • 博客积分: 1504
  • 博客等级: 上尉
  • 技术积分: 190
  • 用 户 组: 普通用户
  • 注册时间: 2008-10-06 14:21
个人简介

系统运维

文章分类
文章存档

2013年(6)

2011年(2)

2009年(4)

2008年(7)

我的朋友

分类:

2009-03-10 21:15:08

 

#在前面【修改CISCO交换机密码】的基础上,用Perl TK写了一个图形工具,练手^_^。

#代码如下,详细说明等如附件。

use Tk;
use Tk::StrfClock;
use strict;
my (@log, @contents, $telnet_old_pw, $enable_old_pw, $telnet_new_pw, $enable_new_pw, $cost);
my $mw = MainWindow->new;
$mw->geometry("700x422");
$mw->title("pTk_Cisco_Dedicate it to IT masons that intend to get married in 2009");
my $main_menu = $mw->Menu();
$mw->configure(-menu => $main_menu);
my $main_frame = $mw->Frame()
                    ->pack(-side => 'top', -fill => 'x');
my $top_frame = $main_frame->Frame()

                           ->pack(-side => 'right', -fill => 'x');

$top_frame->StrfClock(-font=>"fixed 11 bold",

-foreground=>'#128110')->pack();
$mw->Frame(-background => "white")
   ->pack(-side => 'top', -ipady=>'0.1', -fill => 'x');
$mw->Label(-text => "#Input the Cisco_switch's IP address(One IP perl line)#",
           -font=>"fixed 10", -foreground => "black")
   ->pack(-side => "top");
my $file_menu = $main_menu->cascade(-label=>"File", -underline => 0, -tearoff=>0);
$file_menu->command(-label=>"Exit", -underline=>0, -command=>sub{exit});
my $look_menu = $main_menu->cascade(-label=>"View", -underline => 0, -tearoff=>0);
$look_menu->command(-label=>"log", -underline=>0, -command=>\&view_log);
my $tool_menu = $main_menu->cascade(-label=>"Tool", -underline => 0, -tearoff=>0);
$tool_menu->command(-label=>"set switch's pw", -underline=>0, -command=>\&user_passwd);
$tool_menu->command(-label=>"run", -underline=>0, -command=>\&tool_run);
my $top_text = $mw->Text(-background => 'white', -foreground => 'red', -width=>'40', -height=>'7', -font=>"ansi 10 bold")
                  ->pack(-side => "top");
$mw->Button(-text => "submit IP", -font=>"fixed 8 bold", -command => \&submit)
   ->pack(-side=>'top');
my $bottom_text = $mw->Text(-background => 'white', -foreground => 'black', -width=>'46', -height=>'7', -font=>"ansi 10")
                     ->pack(-side => "top");
$mw->Button(-text => "view log", -font=>"fixed 8 bold", -command => \&view_log)
   ->pack(-side=>'top');
$mw->Frame(-background => "gray")
   ->pack(-side => 'top', -ipady=>'0.1', -fill => 'x');
$mw->Label(-text => "Windows XP professional 2002 && Perl 5.8.8 && Tk 804.028\n\nAll Rights Reserved.\nDesign by Li Suzhong.", -font=>"fixed 8")
   ->pack(-side => "bottom");

sub view_log
 {
  open(LOG,")or die "$!";
         @log=<LOG>;
  $bottom_text->delete('1.0', 'end');
                $bottom_text->insert('end', "@log");
  close LOG;
 }
sub submit
 {
  my $contents = $top_text->get('1.0', 'end');
  open (FH,">D:/cisco_ip.txt") or die "$!";
  print FH $contents;
  close FH;
  
  open (FH,") or die "$!";
  while (<FH>)
   {
   if (/^\s+$/)
    {
    next;
    }
   elsif (/(\d+)\.(\d+)\.(\d+)\.(\d+)/ && $1<255 && $2<255 && $3<255 && $4<255)
    {
    push @contents,$_;
    }
    else
     {
     &window;
     last;
     }
   }
  close FH;
 }
sub look_log
 {
  open(LOG,")or die "$!";
  @log=<LOG>;
  close LOG;
 }
   
sub window
 {
  use Win32;
  my $Message = "ERROR IP address";
  my $Result = Win32::MsgBox( $Message , 0|MB_ICONSTOP ,"ERROR IP" );
 }
sub user_passwd
 {
  use Tk::DialogBox;
  use Tk::LabEntry;
  my $db = $mw->DialogBox(-title => 'Login', -buttons => ['Ok', 'Cancel'], -default_button => 'Ok');
  $db->add('LabEntry', -textvariable => \$telnet_old_pw, -width => 20, -background => 'white', -foreground => 'blue',
                    -label => "telnet_old", -labelPack => [-side => 'left'])
     ->pack;
  $db->add('LabEntry', -textvariable => \$enable_old_pw, -width => 20, -background => 'white', -foreground => 'blue',
                    -label => "enable_old", -show => '*',
                    -labelPack => [-side => 'left'])
     ->pack;
  $db->add('LabEntry', -textvariable => \$telnet_new_pw, -width => 20, -background => 'white', -foreground => 'blue',
                    -label => "telnet_new", -labelPack => [-side => 'left'])
     ->pack;
  $db->add('LabEntry', -textvariable => \$enable_new_pw, -width => 20, -background => 'white', -foreground => 'blue',
                    -label => "enable_new", -show => '*',
                    -labelPack => [-side => 'left'])
     ->pack;
  my $answer = $db->Show( );
 }
sub tool_run
 {
  
                my $first = localtime;
  my ($first_hour,$first_min,$first_sec) = (split/:/,(split /\s+/,$first)[3]);
                my $first_time = $first_hour * 3600 + $first_min * 60 + $first_sec;
  use Net::Telnet::Cisco;
  my ($username,$log)=("", "D:/log.txt");
  foreach my $host(@contents)
   {
   my $session = Net::Telnet::Cisco->new (Host => $host,Input_log=>"$log");
   
   $session->login($username,$telnet_old_pw);
   if ($session->enable($enable_old_pw))
    {
    $session->cmd('config terminal');
    $session->cmd('line vty 0 4');
    $session->cmd('login');
    $session->cmd("password $telnet_new_pw"); #modify $passwd

    $session->cmd("enable secret $enable_new_pw");
    $session->cmd('exit');
    }
    else
     {
     warn "$host--->Can't enable: " . $session->errmsg;
     &look_log;
     }
     $session->close;
   }
                my $last = localtime;
  my ($last_hour,$last_min,$last_sec) = (split/:/,(split /\s+/,$last)[3]);
                my $last_time = $last_hour * 3600 + $last_min * 60 + $last_sec;
  $cost = $last_time - $first_time;
                my $top_frame = $main_frame->Frame()
                                           ->pack(-side => 'left', -fill => 'x');
                $top_frame->Label(-text => "Run time: $cost s", -foreground=>'#930700', -font=>'fixed 11 bold')->pack();
 }

MainLoop;

文件: cisco_ptk.rar
大小: 1999KB
下载: 下载

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