[Kc] Using Perl
Eric
eric at alliances.org
Thu Jul 27 14:32:14 PDT 2006
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
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
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#
Eric
On Thu, 27 Jul 2006 11:01:49 -0500, djgoku <djgoku at gmail.com> wrote:
> On 7/26/06, Garrett Goebel <ggoebel at goebel.ws> wrote:
>> Using Perl for sysadmin tasks was how I got started too.
>>
>> Did you read the article about SysInternals and Winternals being
>> bought by Microsoft? Download their source code while you can ;)
>
> Yah I seen that. Yah psshutdown is all I use ATM.
>
>> If you don't mind constructive criticism, consider posting your
>> script to the list. There's nothing that delurks list subscribers
>> better than posting code that they feel they could have done
>> better ;) And you get the added benefit of seeing the different ways
>> people might approach solving the same problem.
>
> It really isn't much at all really haha.
>
> <code>
> #!perl
>
> use strict;
> use warnings;
>
> my $count = 0;
>
> open BATCH, ">", "all_server_shutdown.bat";
>
> while (<>) {
> chomp;
> if (/^REM/i) {
> print BATCH "REM This batch file shuts down most/all servers.\n";
> } elsif (/(^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>
>
>> We used to do a variant of Perl Golf (http://www.xs4all.nl/~thospel/
>> golf/rules.html)... where those who decided to participate could aim
>> for either the shortest script (standard golf) or the fastest script.
>
> Haha, sounds like fun. =)
> _______________________________________________
> kc mailing list
> kc at pm.org
> http://mail.pm.org/mailman/listinfo/kc
--
Eric
More information about the kc
mailing list