#!/fellow/monks.pl
I had to execute some batch files on a number of Windoze servers, but unfortunatly, Windoze is not blessed with an rexec process like Unix...
I wipped up this script to do the trick. You need to provide it with the server name, and script name, and optionally, a username and password. It will then authenticate to the box, read the time, and schedule the script to run two minutes later. Within a couple of minutes, your script/program will be executed.
You may need to use the full username (eg. DOMAIN\username). If you don't specify the username, the script will try to logon using your current credentials. This script is for Windows systems only.
#!/usr/bin/perl # 1 = server name (FQDN) # 2 = script name (on remote machine) # 3 = username (if any) # 4 = Password (if you specified a username) if($ARGV[0] eq "") { print <<HELP; $0 Execute programs on remote Windows NT/2k boxes. Usage : $0 [servername] [program name] {username} {password} servername == MANDATORY == Provide the server name (Netbios or FQDN, + or even IP) program name == MANDATORY == Provide the program name to execute, for example "c:\\temp\\runme.bat" {username} == OPTIONAL == Provide the username to use, either DOMAIN\\USERNAME or just USERNAME {password} == OPTIONAL == If you provide a username, you have to give the password Note: If you don't provide a username, a connection will be made using your current logged on credentials. Phillip Massyn Freeware January 2003 http://phillipmassyn.tripod.com HELP ; exit(1); } &execute($ARGV[0],$ARGV[1],$ARGV[2],$ARGV[3]); sub execute { $server = $_[0]; $scriptname = $_[1]; $user = $_[2]; $password = $_[3]; $it = 2; #--------------------------------------------------------------------- +------------------ #--------------------------------------------------------------------- +------------------ #--------------------------------------------------------------------- +------------------ #Authenticate if($user ne "") { $a = `net use \\\\$server\\ipc\$ /user:$user $password 2>&1`; } else { $a = `net use \\\\$server\\ipc\$ 2>&1`; } if($? != 0) { print "Could not authenticate!\n"; print "===========================================\n"; print "$a\n"; print "===========================================\n"; exit(1); } else { print "Authenticated to $server.\n"; } #--------------------------------------------------------------------- +------------------ #Get local time $a = `net time \\\\$server 2>&1`; if($? != 0) { print "Could not get local time!\n"; print "===========================================\n"; print "$a\n"; print "===========================================\n"; exit(1); } else { print "Got local time.\n"; } #--------------------------------------------------------------------- +------------------ #Calculate scheduled time local $sched = &increasetime(&readtime($a),$it); #--------------------------------------------------------------------- +------------------ #Schedule script #at \\\\$server $sched \" $aa = "at \\\\$server $sched \"$scriptname\" 2>&1"; print "Executing ==> $aa\n"; $a =`$aa`; if($? != 0) { print "Could not schedule execution!\n"; print "===========================================\n"; print "$a\n"; print "===========================================\n"; exit(1); } else { print "Execution scheduled for $sched.\n"; } #--------------------------------------------------------------------- +------------------ #Disconnect $a = `net use \\\\$server\\ipc\$ /delete 2>&1`; if($? != 0) { print "Could not disconnect!\n"; print "===========================================\n"; print "$a\n"; print "===========================================\n"; exit(1); } else { print "Disconnected.\n"; } } #--------------------------------------------------------------------- +------------------ #--------------------------------------------------------------------- +------------------ #--------------------------------------------------------------------- +------------------ sub increasetime { local $tme = $_[0]; local $increment = $_[1]; local ($hour,$min) = split(/:/,$tme); $min = $min + $increment; while($min > 59) { $min = $min - 60; $hour++; } if(length($min) == 1) { $min = "0$min"; } return "$hour:$min"; } sub readtime { local $txt = $_[0]; local $i; local $j; foreach $i (split(/\n/,$txt)) { if(index($i,"time") != -1) { $j = $i; } } local ($junk,$fulltime) = split(/is /,$j); local ($dte,$tme,$pm) = split(/ /,$fulltime); local ($hour,$min) = split(/:/,$tme); if((index($pm,"PM") != -1) && ($hour != 12)) { $hour = $hour + 12; } $nt = "$hour:$min"; return "$nt"; }
Thanks!
#!/massyn.pl The early worm gets caught by the bird.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Running tasks on remote Windows machines
by Aristotle (Chancellor) on Jan 15, 2003 at 22:22 UTC | |
|
Re: Running tasks on remote Windows machines
by draper7 (Scribe) on Jan 15, 2003 at 18:57 UTC | |
by Marza (Vicar) on Jan 16, 2003 at 03:17 UTC | |
by Anonymous Monk on Jan 15, 2003 at 21:41 UTC | |
|
Re: Running tasks on remote Windows machines
by 2mths (Beadle) on Mar 10, 2003 at 10:20 UTC | |
by Massyn (Hermit) on Mar 10, 2003 at 10:26 UTC |