[Kc] Using Perl

Eric eric at alliances.org
Thu Jul 27 19:35:38 PDT 2006


Second Offering:

bash-2.03# cat build_all_server_shutdown.sh
#!/bin/bash
USERNAME="username"
PASSWORD="password"

LOGFILE="all_server_shutdown.txt" \
COMMAND="psshutdown" \
CLOPTIONS="-s -t 1 -u ${USERNAME} -p ${PASSWORD}" \
/usr/bin/perl -ne 'BEGIN { $d=("-")x5; $r="REM"; $fn=$ENV{LOGFILE}; @cmd=($ENV{COMMAND}." ".$ENV{CLOPTIONS}." \\\\"," >> $fn"); $fl=<>; print "$r ${fl}erase $fn\n"; } next if /^(#)|($r)/; tr/a-z\r\n /A-Z/d; $_=(/^(SVR_)|(other)/i?"$r\n$r [$d$_$d]\n":join($_, at cmd)."\n"); print;' < $1 > $2


Eric

djgoku wrote:
> 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>
> _______________________________________________
> kc mailing list
> kc at pm.org
> http://mail.pm.org/mailman/listinfo/kc
>   



More information about the kc mailing list