Home: Perl Programming Help: Intermediate:
Creating Remote Directories with a wrapper



blues
New User

Jul 26, 2012, 11:22 AM


Views: 3145
Creating Remote Directories with a wrapper

I have a script that pushes out a perl script to multiple machines and collects system information. These are all Solaris 10 spare servers. Everything works great accept my directory creation on the remote machine.
If I login and run it manually it works fine, but does not work when I run it from my wrapper script.

My system_info.pl is copied to /tmp on the remote machine using scp and expect. It then runs with sudo locally.

Here are a few lines from the wrapper and system_info script.

my wrapper contains the following sudo command to execute the system_info.pl locally.


Code
 
$sudo_cmd = "cd /tmp ; sudo perl system_info.pl ; exit";


and here is part of the expect piece that runs the command


Code
 
[ qr/admin\@$server\s*\$\s/ => sub { $cmd->send("$sudo_cmd\n"); } ],



The system_info.pl script that is copied
contain's the following lines to create the directory. Again tis works if I login and run system_info.pl manually, but not remotely with the wrapper.


Code
 
if (-d "/usr/local/adm/log") {

open MYLOG, "+>", "/usr/local/adm/log/system_info.log" or die $!;
}
else {
mkdir ("/usr/local/adm", 0755);
mkdir ("/usr/local/adm/log", 0755);
open MYLOG, "+>>", "/usr/local/adm/log/system_info.log" or die $!;
}



Any help would be appreciated.

Thank you
-blues


rovf
Veteran

Jul 27, 2012, 4:14 AM


Views: 3125
Re: [blues] Creating Remote Directories with a wrapper


Quote
but does not work when I run it from my wrapper script.


What is does not work supposed to mean? Does it die silently? Does it blow up the system? ....?


blues
New User

Jul 27, 2012, 7:19 AM


Views: 3115
Re: [rovf] Creating Remote Directories with a wrapper

It does not create the directory on the remote hosts and the script exits. Then the wrapper script fails to copy the log back since it does not exist.

scp: /usr/local/adm/log/system_info.log: No such file or directory
Cannot open Log file No such file or directory

Then my terminal is messed up and I have to type
`stty sane` to get it back to normal.

Thanks
-blues


FishMonger
Veteran / Moderator

Jul 27, 2012, 7:32 AM


Views: 3114
Re: [blues] Creating Remote Directories with a wrapper

Besides the missing info on what you mean by "does not work", I see a logic issue.

If the directory exists, the log file gets opened on write mode clobbering everything that was in it, but if it doesn't exist, then it's opened in append mode after the directory is created.

The modes are reversed from what they should be.

Personally, I'd do it this way:

Code
use File::Path qw(make_path); 

my $path = '/usr/local/adm/log';

if ( not -d $path ) { make_path($path, {mode => 0755}) }

open my $log_fh, '>>', "$path/system_info.log"
or die "failed to open '$path/system_info.log' <$!>";



FishMonger
Veteran / Moderator

Jul 27, 2012, 7:35 AM


Views: 3112
Re: [blues] Creating Remote Directories with a wrapper

Is stderr on the remote side being directed back and are you capturing that info?

Do you have any logging in place that logs the errors/warnings generated on the remote side?

Is the wrapper script connecting with the same cradentials as when you login locally?


(This post was edited by FishMonger on Jul 27, 2012, 7:37 AM)


FishMonger
Veteran / Moderator

Jul 27, 2012, 7:43 AM


Views: 3109
Re: [blues] Creating Remote Directories with a wrapper

You might want to look at using File::Remote
http://search.cpan.org/~nwiger/File-Remote-1.17/Remote.pm