Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked

back tick to do ssh

by convenientstore (Pilgrim)
on Dec 30, 2007 at 23:45 UTC ( [id://659670]=perlquestion: print w/replies, xml ) Need Help??

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

Since i could do below in shell script,

o=`ssh host1 tail -1 /dir/results`
I thought I could easily do below in perl

my $result = `ssh host1 echo something | /bin/others`
but I coudln't do it... I know there are SSH modules to do this but since I have public key stored in server, I don't need to enter passwd so I thought I could easily do this but it doesn't work..
It seems to totally try to run entire line at remote machine, from my $result to end of the line.
Is there anyway to get this done w/out using the ssh modules?

Replies are listed 'Best First'.
Re: back tick to do ssh
by graff (Chancellor) on Dec 31, 2007 at 00:24 UTC
    There's an important difference between your first example and your second one, and I'm not sure what your intention really is (maybe because your examples are too contrived).

    Did you actually try your second example in a shell script (or at the shell command-line prompt)? If so, were you aware that the part following the pipe symbol ("/bin/others") ends up being a process that executes on your local machine? The "echo something" part is being executed via ssh on host1, the output of that operation is then being fed through a pipe on your local machine, and your own local executable file "/bin/others" is being run to process the output.

    If that is your intention, and if you are saying that the perl backtick expression is trying to execute the entire pipeline command on host1 (including the "/bin/others" part), I'd have to ask, how do you know it's happening that way? (A common refrain, seen all too often at PerlMonks: "it doesn't work" is not an adequate description of the problem.)

    Try a real example whose results, if successful, would leave no doubt that the stuff between "ssh" and the pipe symbol is being run on the remote machine, and the stuff after the pipe is being run locally. Make sure the example works from the command line, then make sure the perl runtime environment matches your shell environment in the relevant respects (PATH, permissions, etc). E.g. for *n*x style machines:

    # create a "special" symlink to your "cut" command: ln -s `which cut` ~/my_cut # now, assuming your home directory on host1 is different from your lo +cal one, # and you don't have a "my_cut" command in your home directory on host +1: o=`ssh host1 tail -1 /etc/passwd | ~/my_cut -d: -f3-5` echo $o # now with perl: perl -e '$_=`ssh host1 tail -1 /etc/passwd | $ENV{HOME}/my_cut -d: -f3 +-5`; print'
    If that doesn't work as expected, show us the actual results.
      You are right. My example is not enough and I assumed what was happening.
      It is however my intention that entire commands run on remote machine but results being shown in the local machine.
      I will try others and get back to you on this

        Hi convenientstore,

        Enclose the command you want to be executed on the remote host in quotes, like this:

        my $result = `ssh host1 'echo something | /bin/others'`;

        This way echo something | /bin/others will be run on host1 and the result will end up in $result.

        If you want to run any sort of complex (pipeline or other sequential) operation entirely on a remote host, you will probably want to use a module. If, for reasons no one else can fathom, you really don't want to use a module, you'll need to break your complex operations down into simple, single-step-at-a-time commands and waste a lot of time doing a series of backtick operations to slog through the sequence.

        Either that, or else you'll simply want to create a script on the remote machine that does everything you want, and run that as a single backtick operation from your local perl script.

Log In?

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

How do I use this?Last hourOther CB clients
Other Users?
Others sharing their wisdom with the Monastery: (5)
As of 2024-05-18 00:08 GMT
Find Nodes?
    Voting Booth?

    No recent polls found