#!/usr/bin/perl -w -s use Getopt::Long; use Config::Auto; #use Data::Dumper; my @servers = (); my @commands = (); my @files = (); my $user = $ENV{USER}; my $file = (); my $dest = (); my $help = (); my $config = 0 ; GetOptions ("user=s" => \$user, "server=s" => \@servers, "command=s" = +> \@commands, "file=s" =>\$file, "dest=s"=> \$dest, "config=s" => \$c +onfig, "help=s" => \$help); print "config: \"$config\"\n"; =pod =============================== + Functions Below: + + + =============================== =cut sub get_config_file_data { # The Config::Auto::parse module returns a reference to a hash wic +h contains references to arrays # (I think) # In order to make life simpler, I'm going to put them into the re +gular arrays, @server, @commands, @files, etc. my $config_return = Config::Auto::parse(); # goes and gets stuf +f from config file, puts it into @{ %config_return } $using_config="TRUE"; # We are using the config if (! @user && @{ $config_return->{user} }){ # move the + field from the referenced array in the hash to a @user = @{$config_return->{user} } ; # regular +array. } if (! @servers && @{$config_return->{server} }){ # ditto @servers = @{$config_return->{server} } ; } if (! @commands && @{$config_return->{command} }){ # ditto @commands = @{$config_return->{command} } ; } if (! @files && @{$config_return->{file} }){ # ditt +o @files = @{$config_return->{file} } ; } } sub run_command(){ chomp($command); system("ssh ${user}\@${server} $command") ; #or print "\nCould not + execute ssh command\n"; } sub run_copy(){ chomp($files[1]); #print "\n----hey i'm in run_copy!-----\n"; #print "\nscp $files[0] ${user}\@${server}:$files[1]\n"; system("scp $files[0] ${user}\@${server}:$files[1]"); } =pod =============================== + MAIN PROGRAM BEGINS: + + + =============================== =cut #print $help; if ($help) { =pod Print's a help message - there was a built in way to do this but I didn't know at the time. =cut print "\n\n STREW HELP MESSAGE:\n \tOPTIONS:\n \t\t--server foo.bar.com *OR* --server =\"foo.bar.com\"\n \t\t--command=\"/etc/init.d/someinit restart\" *OR* command \"/etc +/init.d/someinit restart\"\tUSE QUOTES!\n \t\t--file /home/user/somefile --dest /etc/ *OR* --file=\"/home/u +ser/somefile\" --dest=\"/etc/\"\n \t\t--help : brings you here." ; exit; } =pod This is weird.... if $config has a size > 0 , then run get_config_file_data subroutine However, even if I do not use the --config flag on the command line, i +t runs anyway!! =cut if ($config){get_config_file_data}; if ($config){print "\nCONFIG: \"$config\"\n"}; # We are more than probably going to get servers from --server="foo.do +cmagic.com bar,docmagic.com bah.docmagic.com" # The below takes in the list, and splits on whitespace. # for $server (@servers) { if ($server =~ /\s/){ @server_list=split(/\s/, $server) ; } } if ($using_config && @commands) { $command = join(' ', @commands); #print "$real_command \n"; } if (@servers){ for $server (@servers){ #print "COMMAND: ". $command . "\n"; print "Server: $server \n"; if (@commands){run_command} ; if (@files){run_copy}; } } if (@server_list){ for $server (@server_list){ run_command(); run_copy(); } }
thanks a lot for the help, btw. I'm pretty new to perl and how quickly you all got back to me with advice was amazing. I appreciate it a lot.
thanks again,
Nick
In reply to Re: Re: Re: Re: Problem with GetOpts::Long
by niku
in thread Problem with GetOpts::Long
by niku
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |