script 说明:
该script可以扫描指定服务器的如下信息,并发邮件通知。 用来自动创建服务器管理文档也很有效。 包括:域名,ip地址,mac地址, 服务器开放的端口。
- #!/usr/bin/perl -w
- use strict;
- use Mail::Sender;
- my @servers=qw(10.13.55.1 10.13.55.2 10.13.55.3); #在这里添加需要扫描的主机
- foreach my $host (@servers) {
- my $nmap=`nmap -O $host`;
- open (AF, '>nmap_scan') or die "open error: $!\n";
- print AF $nmap;
- close(AF);
- my @results;
- my @port;
- open (INPO, 'nmap_scan');
- foreach my $list (<INPO>) {
- chomp($list);
- if (Match_word($list)) {
- push(@results, Match_word($list));
- };
- if (Match_port($list)) {
- push(@port, Match_port($list));
- };
- };
- close(INPO);
- print "@results\n";
- @port=join("/", @port);
- print "opened port: @port\n\n";
- @results=undef;
- @port=undef;
- };
- sub Match_word {
- my @lists;
- my @result;
- my @ser_port;
- my $list=shift;
- if ($list=~/^Nmap scan report for/) {
- @lists=split(/\s/, $list);
- $lists[5]=~s/[(|)|\s+]//g;
- push(@result, $lists[4], $lists[5]);
- return @result;
- };
- if ($list=~/^MAC/) {
- @lists=split(/\s/, $list);
- $lists[2]=~s/\s+//g;
- return $lists[2];
- };
- if ($list=~/^Running/) {
- @lists=split(/:\s/, $list);
- return $lists[1];
- };
- }
- sub Match_port {
- my $list=shift;
- if ($list=~/open/) {
- my @lists=split(/\s+/, $list);
- my @aaa=split(/\//, $lists[0]);
- return "$aaa[0]";
- };
- }
- sub Send_mail {
- my $subject=shift;
- my $msg=shift;
- my $sender=new Mail::Sender->MailMsg({
- smtp => 'mail.aaa.com',
- from => 'neo@aaa.com',
- to =>'neo.@gmail.com',
- subject => $subject,
- msg => $msg,
- auth => 'LOGIN',
- authid => 'neo',
- authpwd => '1234',}
- ) or die "$Mail::Sender::Error\n";
- 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