#!/usr/bin/perl -w # # netlock.pl - lock the Win2k workstation if it drops off the network # # $Id: netlock.pl,v 1.4 2001/10/24 01:32:22 thor Exp $ # # Tom Moertel # # Usage: netlock host... [delay] [retries] # - the host(s) to ping to determine whether we're on the network # - delay is the number of seconds between ping attempts # - retries is the number of failed pings to allow before locking # use strict # Win32::API causes "use strict" to complain use File::Basename; use Net::Ping; use Win32::API; #============================================================================== # parse command line #============================================================================== my @hosts = grep {!/^\d+$/} @ARGV; splice @ARGV, 0, scalar @hosts; my $delay = $ARGV[-2] || 10; my $retries = $ARGV[-1] || 3; unless (@hosts) { my $cmd = basename($0); print STDERR "Usage: $0 host... [delay] [retries]\n"; exit 1; } #============================================================================== # allocate global objects #============================================================================== my $lws = new Win32::API("user32", "LockWorkStation", [], I) || die "can't get LockWorkStation from USER32.DLL"; my $pinger = new Net::Ping "icmp" || die "can't create new Net::Ping for ICMP"; #============================================================================== # main loop #============================================================================== while (1) { my $retries_left = $retries; while ($retries_left) { # ping the hosts in turn, but stop as soon as one responds my $success; for my $host (@hosts) { last if $success = $pinger->ping($host); } # if we succeed, reset the retry count; otherwise, decrement it if ($success) { $retries_left = $retries; } else { print STDERR "Failed to reach any hosts at ", scalar localtime, "; retries left = $retries_left\n"; last unless --$retries_left; # exit loop NOW if out of retries } sleep $delay; } # out of retries, lock the workstation print STDERR "Locking workstation at ", scalar localtime, " !\n\n"; $lws->Call(); }