#!/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.


In reply to Running tasks on remote Windows machines by Massyn

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.