script 说明:

 

该script可以扫描指定服务器的如下信息,并发邮件通知。 用来自动创建服务器管理文档也很有效。
包括:域名,ip地址,mac地址, 服务器开放的端口。

 

  1. #!/usr/bin/perl -w 
  2. use strict; 
  3. use Mail::Sender; 
  4. my @servers=qw(10.13.55.1 10.13.55.2 10.13.55.3); #在这里添加需要扫描的主机
  5. foreach my $host (@servers) { 
  6.         my $nmap=`nmap -O $host`; 
  7.         open (AF, '>nmap_scan'or die "open error: $!\n"
  8.         print AF $nmap; 
  9.         close(AF); 
  10.         my @results; 
  11.         my @port; 
  12.         open (INPO, 'nmap_scan'); 
  13.         foreach my $list (<INPO>) { 
  14.                 chomp($list); 
  15.                 if (Match_word($list)) { 
  16.                         push(@results, Match_word($list)); 
  17.                 }; 
  18.                 if (Match_port($list)) { 
  19.                         push(@port, Match_port($list)); 
  20.                 }; 
  21.         }; 
  22.         close(INPO); 
  23.         print "@results\n"
  24.         @port=join("/", @port); 
  25.         print "opened port: @port\n\n"
  26.         @results=undef; 
  27.         @port=undef; 
  28. }; 
  29.  
  30.  
  31. sub Match_word { 
  32.         my @lists; 
  33.         my @result; 
  34.         my @ser_port; 
  35.         my $list=shift; 
  36.         if ($list=~/^Nmap scan report for/) { 
  37.                 @lists=split(/\s/, $list); 
  38.                 $lists[5]=~s/[(|)|\s+]//g; 
  39.                 push(@result, $lists[4], $lists[5]); 
  40.                 return @result; 
  41.         }; 
  42.  
  43.         if ($list=~/^MAC/) { 
  44.                 @lists=split(/\s/, $list); 
  45.                 $lists[2]=~s/\s+//g; 
  46.                 return $lists[2]; 
  47.         }; 
  48.  
  49.  
  50.         if ($list=~/^Running/) { 
  51.                 @lists=split(/:\s/, $list); 
  52.                 return $lists[1]; 
  53.  
  54.         }; 
  55.  
  56. sub Match_port { 
  57.         my $list=shift; 
  58.         if ($list=~/open/) { 
  59.                 my @lists=split(/\s+/, $list); 
  60.                 my @aaa=split(/\//, $lists[0]); 
  61.                 return "$aaa[0]"
  62.         }; 
  63. sub Send_mail { 
  64.         my $subject=shift; 
  65.         my $msg=shift; 
  66. my $sender=new Mail::Sender->MailMsg({ 
  67.          smtp => 'mail.aaa.com'
  68.          from => 'neo@aaa.com'
  69.          to =>'neo.@gmail.com'
  70.          subject => $subject, 
  71.          msg => $msg, 
  72.          auth => 'LOGIN'
  73.          authid => 'neo'
  74.          authpwd => '1234',} 
  75. or die "$Mail::Sender::Error\n"
  76. print "Mail sent ok"

 


script 执行结果:
包括:域名,ip地址,mac地址, 服务器开放的端口。

root@ubuntu:~/script# perl services_info_scan

link.aaa.com.cn 10.13.55.1 00:0D:48:09:02:0B

opened port: 22/80/443/2005


fs.aaa.com.cn 10.13.55.2 00:1F:C6:14:1F:9E Microsoft Windows 2003

opened port: 53/80/88/135/389/445/464/593/636/1025/1027/1036/1060/1723/2000/3268/3269/3389


mail.aaa.com.cn 10.13.55.3 00:0C:29:D9:58:6E Linux 2.4.X

opened port: 22/25/53/110/111/143/587/3000/32768