LISTING 1: FixNullShares.pl
use Net::Ping;
use Win32::Registry;
my $Host;
my $Subnet = shift @ARGV ¦¦ "192.168.1";
my $Host_Min = shift @ARGV ¦¦ 0;
my $Host_Max = shift @ARGV ¦¦ 255;
# Change the @VALID_SHARES array to contain the list of
# null session shares you permit a machine to have.
my @VALID_SHARES = qw(
DFS$
CHEYUPD$
CHEYALERT$
COMCFG
);
my $Ping_Timeout = 2;
my $PingObject = Net::Ping->new( "icmp", $Ping_Timeout ) ¦¦ die;
my $LogFile = $0;
$LogFile =~ s/\.[^.]*$/.log/;
if( open( LOG, "> $LogFile" ) )
{
my $BackupHandle = select( LOG );
$¦ = 1;
select( $BackupHandle );
}
# Check for any online hosts.
foreach my $Host ( $Host_Min .. $Host_Max )
{
my $IP = "$Subnet.$Host";
print "Checking $IP...";
if( $PingObject->ping( $IP ) )
{
# We found a host on the net, so let's process it.
print "found";
ProcessHost( $IP );
}
else
{
print "no node found.";
}
print "\n";
}
close( LOG ) if( fileno LOG );
sub ProcessHost
{
my $Host = shift @_;
my( $Root, $MachineName, $MachineSpeed, $UserName,
$NullSessionShares );
# Try to connect to the remote machine.
if( $HKEY_LOCAL_MACHINE->Connect( $Host, $Root ) )
{
my $Key;
# Determine computer name.
if( $Root->Open( "SYSTEM\\CurrentControlSet\\Control\\ComputerName
\\ComputerName", $Key ) )
{
my $Type;
$Key->QueryValueEx( "ComputerName", $Type, $MachineName );
print " \\\\$MachineName ";
$Key->Close();
}
# Determine last user to log on.
if( $Root->Open( "SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion
\\Winlogon", $Key ) )
{
my $Type;
$Key->QueryValueEx( "DefaultUserName", $Type, $UserName);
print " (Last User: $UserName) ";
$Key->Close();
}
# Determine approximate CPU speed.
if( $Root->Open( "HARDWARE\\DESCRIPTION\\System
\\CentralProcessor\\0", $Key ) )
{
my $Type;
$Key->QueryValueEx( "~MHz", $Type, $MachineSpeed );
print " (approx. $MachineSpeed MHz) ";
$Key->Close();
}
# Try to open the specified registry key.
if( $Root->Open( "SYSTEM\\CurrentControlSet\\Services\\lanmanserver
\\parameters", $Key ) )
{
my $Type;
if( $Key->QueryValueEx( "NullSessionShares", $Type,
$NullSessionShares ) )
{
my( @AllowedList, @DeniedList );
foreach my $ShareName ( split( /\0/, $NullSessionShares ) )
{
if( IsShareAllowed( $ShareName ) )
{
push( @AllowedList, $ShareName );
}
else
{
push( @DeniedList, $ShareName );
}
}
# If we have any shares that aren’t allowed,
# then do what we need to do.
if( scalar @DeniedList )
{
print "\n\tNull shares not allowed:\n\t\t'" .
join( "'\n\t\t'", @DeniedList ) . "'\n";
# If you uncomment the next line, it will change the
# registry, so make sure you uncomment it only if you’re
# comfortable with making such changes.
# $Key->SetValueEx( "NullSessionShares", 0, REG_MULTI_SZ,
# join( "\0", @AllowedList ) );
}
}
$Key->Close();
}
$Root->Close();
}
else
{
print " Registry service is not enabled.";
}
printf( LOG "%s\t%s\t%s\t%s\t%s\n",
$Host,
$UserName,
$MachineName,
$MachineSpeed,
$NullSessionShares ) if( fileno LOG );
}
sub IsShareAllowed
{
my( $ShareName ) = @_;
foreach my $ValidShare ( @VALID_SHARES )
{
return( 1 ) if( lc $ValidShare eq lc $ShareName );
}
return( 0 );
}
sub ProcessHost
{
my $Host = shift @_;
my( $Root, $MachineName, $MachineSpeed, $UserName, $NullSessionShares );
# Try to connect to the remote machine...
if( $HKEY_LOCAL_MACHINE->Connect( $Host, $Root ) )
{
my $Key;
# Determine computer name...
if( $Root->Open( "SYSTEM\\CurrentControlSet\\Control\\ComputerName\\ComputerName", $Key ) )
{
my $Type;
$Key->QueryValueEx( "ComputerName", $Type, $MachineName );
print " \\\\$MachineName ";
$Key->Close();
}
# Determine last user logon...
if( $Root->Open( "SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\Winlogon", $Key ) )
{
my $Type;
$Key->QueryValueEx( "DefaultUserName", $Type, $UserName);
print " (Last user: $UserName) ";
$Key->Close();
}
# Determine approximate cpu speed...
if( $Root->Open( "HARDWARE\\DESCRIPTION\\System\\CentralProcessor\\0", $Key ) )
{
my $Type;
$Key->QueryValueEx( "~MHz", $Type, $MachineSpeed );
print " (approx. $MachineSpeed MHz) ";
$Key->Close();
}
printf( LOG "%s\t%s\t%s\t%s\t%s\n",
$Host,
$UserName,
$MachineName,
$MachineSpeed,
$NullSessionShares ) if( fileno LOG );
}
sub IsShareAllowed
{
my( $ShareName ) = @_;
foreach my $ValidShare ( @VALID_SHARES )
{
return( 1 ) if( lc $ValidShare eq lc $ShareName );
}
return( 0 );
}
this is the updated code.. but still not able to ping or get cpu and memory utilization.pls help
[edited by: jatar_k at 5:46 pm (utc) on July 29, 2005]
[edit reason] removed url [/edit]