[Kc] Using Perl

djgoku djgoku at gmail.com
Thu Jul 27 15:08:16 PDT 2006


On 7/27/06, Eric <eric at alliances.org> wrote:
> First offering:
>
> bash-2.03# cat build_all_server_shutdown.sh
> #!/bin/bash
>
> /usr/bin/perl -ne 'next if /^(#)|(SVR_)|(other)|(REM)/; chomp; s/^(.*)$/psshutdown -s -t 1 -u username -p password \\\\U$1\\E \> all_server_shutdown.txt/; print "$_\n";' < $1 > $2
>

The syntax for psshutdown is (for what I use it for, to shutdown
windows boxes from a windows box):

psshutdown.exe -s -t 1 -u username -p password \\servername
! psshutdown.exe -s -t 1 -u username -p password \\Uservername\E

the \U I am using is just to UPPERCASE all $_, and \E to stop after
the end of $_.

Also there is a reason why there is an if ($count == 0) so the log
all_server_shutdown.txt is overwritten when batch file runs the first
shutdown then after that it is just concatenated to the end of the log
file. But other than that I don't think there is anything that won't
allow the batch file to run.

> bash-2.03# ./build_all_server_shutdown.sh test.dat all_server_shutdown.bat
>
>
> bash-2.03# cat test.dat
> SVR_12355 Hello
> other stuff goes here
> # This is a comment
> 1.2.3.4
> 192.168.0.1

Sample config file:

$ cat test
REM #Reason for this was I wanted it to print this at the start of the
all_server_shutdown.bat though I think I found a way around having to
do this =) see code sample below.
svr_name
server1
server2
svr_name2
server3
server4
other
server5

$ cat all_server_shutdown.bat
REM Info about this batch file
REM
REM [-----svr_name-----]
psshutdown -s -t 1 -u username -p password \\server1 > all_server_shutdown.txt
psshutdown -s -t 1 -u username -p password \\server2 >> all_server_shutdown.txt
REM
REM [-----svr_name2-----]
psshutdown -s -t 1 -u username -p password \\server3 >> all_server_shutdown.txt
psshutdown -s -t 1 -u username -p password \\server4 >> all_server_shutdown.txt
REM
REM [-----other-----]
psshutdown -s -t 1 -u username -p password \\server5 >> all_server_shutdown.txt

>
> bash-2.03# cat all_server_shutdown.bat
> psshutdown -s -t 1 -u username -p password \\U1.2.3.4\E > all_server_shutdown.txt
> psshutdown -s -t 1 -u username -p password \\U192.168.0.1\E > all_server_shutdown.txt
> bash-2.03#

See above ^^^.

<code>
#!perl

use strict;
use warnings;

my $count = 0;

open BATCH, ">", "all_server_shutdown.bat";

print BATCH "REM This batch file shuts down most/all servers.\n";

while (<>) {
       chomp;
       if (/(^SVR\_|^other)/i) {
               print BATCH "REM\n";
               print BATCH "REM [-----$_-----]\n";
       } elsif (/^\#/) {
               #All comments are lost forever!
               #print BATCH"REM $'\n";
       } else {
               if ($count == 0) {
                       print BATCH "psshutdown.exe -s -t 1 -u username
-p password
\\\\\U$_\E > all_server_shutdown.txt\n";
               } else {
                       print BATCH "psshutdown.exe -s -t 1 -u username
-p password
\\\\\U$_\E >> all_server_shutdown.txt\n";
               }
               $count++;
       }
}
</code>


More information about the kc mailing list