Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

Net::OpenSSH: Trouble accessing a mount point mounted via the perl script

by cebundy (Novice)
on Oct 09, 2013 at 19:26 UTC ( [id://1057611]=perlquestion: print w/replies, xml ) Need Help??

cebundy has asked for the wisdom of the Perl Monks concerning the following question:

I am writing a script where I ssh to remote hosts, where I mount a smb share, cd to the scripts directory on that share, and run a perl script. (The perl script is a TCPClient and connects to the script running on the local host). I am using Net::OpenSSH to ssh from my local linux host to a remote linux host:
my $ssh = Net::OpenSSH->new( $ip, master_opts => [-o => "StrictHostKeyChecking=no"], password=>$password, user => $user);
my resulting remote client object contains my ssh object:
client obj: $VAR1 = bless( { 'hostname' => 'qaclient6.omneon.local', 'ip' => '10.4.96.106', 'os' => 'linux', 'password' => 'passwd', 'ssh' => bless( { '_batch_mode' => undef, '_ctl_path' => '/root/.libnet-opens +sh-perl/root-10.4.96.106-27838-53771', '_default_argument_encoding' => und +ef, '_default_ssh_opts' => undef, '_default_stderr_fh' => undef, '_default_stdin_fh' => undef, '_default_stdout_fh' => undef, '_default_stream_encoding' => undef +, '_error' => 0, '_error_prefix' => [], '_expand_vars' => undef, '_external_master' => undef, '_gateway_args' => undef, '_home' => '/root', '_host' => '10.4.96.106', '_host_squared' => '10.4.96.106', '_key_path' => undef, '_kill_ssh_on_timeout' => undef, '_login_handler' => undef, '_master_opts' => [ '-o', 'StrictHostKeyC +hecking=no' ], '_master_stderr_discard' => undef, '_master_stderr_fh' => undef, '_master_stdout_discard' => undef, '_master_stdout_fh' => undef, '_mpty' => bless( \*Symbol::GEN1, ' +IO::Pty' ), '_passphrase' => undef, '_passwd' => '@BAJ@A"', '_perl_pid' => 27838, '_pid' => 27840, '_port' => undef, '_proxy_command' => undef, '_rsync_cmd' => 'rsync', '_scp_cmd' => undef, '_ssh_cmd' => 'ssh', '_ssh_opts' => [ '-l', 'root' ], '_target_os' => 'unix', '_thread_generation' => 0, '_timeout' => undef, '_user' => 'root', '_vars' => {}, '_wfm_bout' => '' }, 'Net::OpenSSH' ), 'user' => 'root' }, 'MGRemoteHost' );
then I mount a smb share on the remote host with the command:
my $cmd = 'mount -t cifs //$server->ip}/myscripts /mnt/mgscripts -o us +er=qauser,password=Tester-01'; push @cmds,$cmd; $ssh->open_ex( { stdin_pipe => 1,stdout_pipe => 1, stderr_pipe => 1 }, + @cmd ) or my $error = "open_ex failed: " . $opts->{ssh}->error;
I can see the mount is successful via another ssh session to the remote host, where I can cd to a directory on the mount point. Then I send the command:
"cd /mnt/mgscripts/latest' via open_ex and I get: 'bash: line 0: cd: /mnt/mgscripts/latest: No such file or directory'
So to simplify, I stopped doing the mount in the script and used the mount point that the script already created on the previous run. This is what mount shows:
'//10.20.56.64/myscripts/ on /mnt/mgscripts type cifs (rw,mand)'
However if issue this command via open_ex:
'cd /mnt/mgscripts/latest;ls'
It works! So I thought, OK, since what I want to do is mount a share, cd to it then run a perl script, this command should work: 'mount;cd /mnt/mgscripts/latest;perl CreateFiles -fspath=/mnt/qa5' Yes! it worked. Then I tried it again... Nope! I got:
$VAR1 = [ 'bash: line 0: cd: /mnt/mgscripts/latest: No such file or di +rectory', 'Can\'t open perl script "CreateFiles.pl": No such file or d +irectory' ];
So I kept testing and I found that the commands work (both the "mount;cd ...;ls" and the "mount;cd ...;perl...") exactly every other run of the script. I tried putting sleeps in between the commands:
'mount;sleep 10;cd /mnt/mgscripts/latest;sleep 10;ls'
but that didn't help either.
Note that the script always succeeds if I use a mount point on the remote host that was not created via Net::OpenSSH but just a ssh session from my local linux host.
'//10.20.56.64/myscripts/ on /mnt/mgscripts type cifs (rw,mand)'
Any ideas about what could be happening? Thanks, cebundy

Replies are listed 'Best First'.
Re: Net::OpenSSH: Trouble accessing a mount point mounted via the perl script
by salva (Canon) on Oct 10, 2013 at 07:41 UTC
    open_ex is a low level method. It starts the remote command and returns the file descriptors attached to its IO streams. Then you have to handle the IO and the finalization of the process. Don't use it unless you understand how it works.

    For common SSH operations there are easier to use, higher level methods such as capture or system which run commands synchronously, or if you want to run a command asynchronously spawn.

    I guess the issue here is a race condition because you are mounting the share asynchronously and CIFS is slow at setup.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1057611]
Approved by salva
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others romping around the Monastery: (4)
As of 2024-03-29 11:05 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found