Perl's system() may or may not invoke a shell. If it does, then it may not be the usual shell you get when you login (e.g. bash, sh, csh), or it may be that, but it may not be interactive and so it may be missing some crucial initialisation you otherwise get when running ssh at the command line.

If it doesn't call a shell, it will do an execvp which inherits the environment BUT it will not do the initialisations, conditional initialisations, it will not call other scripts etc. like one gets when opening a prompt and a new interactive shell.

If it does call a shell, it may not be the one you are normally using (sh vs bash) and so it will be doing initialisations which are not specific to your setting. Even if you explicitly specify which shell to use, it will not be "interactive" unless you specify so explicitly as well. That's important because many shell init scripts distinguish between interactive and non-interactive invocations. This further complicates things because a lot of functionality in these init scripts is conditional on interactivity.

If there is only one scalar argument, the argument is checked for shell metacharacters, and if there are any, the entire argument is passed to the system's command shell for parsing (this is "/bin/sh -c" on Unix platforms, but varies on other platforms). If there are no shell metacharacters in the argument, it is split into words and passed directly to "execvp", which is more efficient.

The bottomline is: read the manual and then choose the execution mode which exactly matches your requirement and be explicit on what you want, do not rely on Perl's defaults.

This will run your ssh command in an interactive (-i), bash shell, assuming this is the one you get when you login and open a prompt: system('/bin/bash', '-i', '-c', 'ssh root@remoteIP /usr/bin/ls'); . Whether that will do what you want is doubtful as there are other factors in your environment which I am not aware of.

bw, bliako


In reply to Re: Calling a remote ssh connection from perl by bliako
in thread Calling a remote ssh connection from perl by KenHorse

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.