Setting network hosts as variables with Nmap scan in Perl

269 Views Asked by At

I'm working on writing a Perl script that will automatically run and automatically access shared SMB drives on a network, when connected to said network. What I have gotten it to do so far, using the code below, is to check to make sure an internet connection is present on the network, and then run an Nmap scan for port 139. It's simple code and thus has obviously worked successfully, however, what would be a good way to automatically take each IP address with port 139 open and turn each one into a variable, for further commands with smbclient? In simpler terms, I want for each scanned IP address that has the open port to be set as $IP1, $IP2, $IP3, etc. Any help or feedback is appreciated.

#!/usr/bin/perl

use strict;
use Net::Ping;

## Check for an internet connection by pinging Google
my $p = Net::Ping->new("icmp");
while(1){
printf "Checking internet connection.....\n";
    if ($p->ping("172.217.4.132")){
    printf "Internet connection is active\n";
        sleep 1;
    last;
    }
    else{
    printf "Internet connection not established.\n";
        sleep 5*5;
    }
}
## Determine the gateway IP address of the network

my $IP = "192.168.1.1";

system( "nmap -vv $IP/24 -p 139 | grep open" );
0

There are 0 best solutions below