#!/usr/bin/perl use strict; use Win32::OLE qw( HRESULT ); my $NETWORK="192.168.0.0"; my $MASK="255.255.255.0"; my $NETWORKDRIVE="L:"; my $LOGONSCRIPT="KIX32.EXE"; my %HRESULT = ( success => HRESULT( 0x00000000 ), timeout => HRESULT( 0x80043001 ), ); my %EVENT_TYPE = ( error => 1, warning => 2, information => 3, audit_success => 4, audit_failure => 5, ); my $machine = Win32::NodeName(); my $SqlQuery = "SELECT TargetInstance.Name from __InstanceOperationEve +nt WITHIN 4 WHERE TargetInstance ISA 'Win32_NetworkAdapterConfigurati +on'"; my $WMIServices = Win32::OLE->GetObject( "winmgmts:{impersonationLevel +=impersonate,(security)}//$machine/" ) || die "Cannot impersonate $machine\n"; my $Events = $WMIServices->ExecNotificationQuery( $SqlQuery ); if( $HRESULT{success} != scalar Win32::OLE->LastError() ) { die "Error: " . Win32::OLE->LastError() . "\n"; } print "Listening for Win32 NetworkAdapterConfiguration events on machi +ne: $machine\n"; my $networknumber = IP2binary($NETWORK); my $masknumber = IP2binary($MASK); while (1) { my $Event = $Events->NextEvent( -1 ); last if $HRESULT{timeout} == scalar Win32::OLE->LastError(); my $ip = $Event->{TargetInstance}->{Ipaddress}; die "No IP Array!\n" if (ref($ip) ne "ARRAY"); if(grep { (IP2binary($_) & $masknumber) == ($networknumber & $masknu +mber) } @{$ip}) { print "Found $NETWORK/$MASK subnet\n"; my $logonserver = Win32::OLE->CreateObject("Wscript.Shell")->Enviro +nment("VOLATILE")->LOGONSERVER; my $objnet = Win32::OLE->CreateObject("Wscript.Network"); $objnet->RemoveNetworkDrive($NETWORKDRIVE, 1, 1); $objnet->MapNetworkDrive($NETWORKDRIVE,$logonserver."\\NETLOGON",0) +; chdir($NETWORKDRIVE); system($NETWORKDRIVE."/$LOGONSCRIPT"); } } # IP & mask == network address # so if $networknumber & $masknumber == $IPbeingtested & $masknumber # then it's on the same subnet sub IP2binary { my @IP = split(/\./,shift); my $binary; for (my $i=0; $i<scalar(@IP); $i++) { $binary += $IP[$i]*(2**(8 * (scalar(@IP)-$i-1))); } return $binary; }

In reply to VPN logon monitor to run logon scripts by lilphil

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.