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

Hi monks,
I have tried to execute this below command inside the perl script by using backtick (``) operator.

my $cmd = "ssh -x -i ~/.ssh/bkp <server name> mminfo -q \"savet +ime>3/1/12,name=DISASTER_RECOVERY:\\\\\" -av -ot -r client >/home/rbr +/clnts"; `$cmd`;

But while executing it I have faced this below error,
bash: 3/1/12,name=DISASTER_RECOVERY: -av: No such file or directory

Please help me to solve this problem.
I need to execute this command in the remote machine.
I have used this same method in another sever.
but I didn't face any problem there. It has executed perfectly.

Replies are listed 'Best First'.
Re: Command Executing problem in perl.
by McA (Priest) on Jun 27, 2012 at 08:57 UTC
    Hi,

    Just a hint to help yourself:

    Print the $cmd string, copy&paste it to your shell and see what happens. Often you see unbalanced "

    You have to be careful with output-redirection and using ssh-command. You must be aware of where the output file wil be created, on the remote side or the local side

    With the backtick operator you want to grab the output of the script, but as soon as you redirect STDOUT you'll get nothing. So `$cmd` with no assignment is functional more or less equivalent to system($cmd).

      Thanks for ur rply.
      yeah I have executed in shell. It has displayed the same error. It should create the file in the remote machine, not in local machin.
      This same script is working in some other machine.

Re: Command Executing problem in perl.
by mantager (Sexton) on Jun 27, 2012 at 12:57 UTC

    Hi leslie,
    I'm having troubles parsing the command line as you wrote it, so maybe your shell is having troubles, too.
    What's the command that's supposed to run on the remote machine? I mean: can you tell us how you would write it if you were on the remote machine's shell?
    Also, note that the remote shell is trying to redirect output on the file "3/1/12,name=DISASTER_RECOVERY: -av" because of the first > in the command line.

    I tried this:

    my @cmd = ('ssh', '-x', 'user@remotehost', '"ls -la > mylsfile"'); my @output = qx/@cmd/;

    @output is empty, since all the output was redirected into the file "mylsfile" on the remote host. I suppose this is more or less what you're trying to accomplish. Maybe trying to split the command into an array may help you sort out the quotes.
    If I right interpreted your command, what you're trying to do is:
    my @cmd = ( 'ssh', '-x', '-i', '~/.ssh/bkp', '<server name>', q[mminfo -q "savetime>3/1/12,name=DISASTER_RECOVERY:\\\\" -av -ot - +r client >/home/rbr/clnts] );

    I'm not sure how many \ you will need on that command line (assuming you're trying to put some \ after that colon).

      Hi mantager,
      Thanks for ur input.
      This below command should have to run in the remote machine and output should be redirect to the file.

      mminfo -q "savetime>3/1/12,name=DISASTER_RECOVERY:\\" -av -ot -r clien +t >/home/rbr/clnts

      I am executing this command from my local machine using perl script through SSH as I said earlier.
      I have tried to create the output file in the remote machine. But now I am cleared that, we can't create the output file in the remote machine
      by redirecting the output from one of our monks reply.

      Note:
      - After colon there should be two slash.
      - We can use single quotes also instead of double quotes.

        Hi leslie.
        Creating the file on the remote machine is not impossible, it's only a matter of quoting the string the right way.
        In your case, it's also a matter of isolating that > sign you have inside your parameters, because otherwise it's interpreted by the remote shell as a redirection.

        With this:

        my @cmd = ( 'ssh', '-x', $remotehost, q['mminfo -q "savetime>3/1/12,name=DISASTER_RECOVERY:\\\\" -av -ot + -r client > /home/rbr/clnts'] ); print qx/@cmd/;
        you should get the result you expect. I assumed the \ after the colon is just one, because if you launch directly the command:
        mminfo -q "savetime>3/1/12,name=DISASTER_RECOVERY:\\" \ -av -ot -r client >/home/rbr/clnts
        from the shell, one of the \ is eaten by the shell. The four \\\\ should be equivalent. I tried this with a "mminfo" command that is just a script echoing its arguments, and this is what I get:
        -q savetime>3/1/12,name=DISASTER_RECOVERY:\ -av -ot -r client
        and it's written in a file on the remote machine.

        Cheers.

Re: Command Executing problem in perl.
by pvaldes (Chaplain) on Jun 27, 2012 at 16:20 UTC
    use Net::SSH::Perl; my $ssh = Net::SSH::Perl->new( "www.blah.blah.blah.com", identity_files => '~/.ssh/bkp', debug => true, #same as -v use_pty => 1, #request pseudo-tty, same as -t option, I think options => [ "ForwardX11 no", # same as -x "ForwardAgent no", # same as -a if I'm not wrong # all desired options passed as # "option value", # you can choose also any of these: ... "AddressFamily 'any'", "BatchMode yes/no", "BindAddress ... ", "ChallengeResponseAuthentication yes/no", "CheckHostIP yes/no", "Cipher 3des", "Ciphers ...", "ClearAllForwardings ...", "Compression ...", "CompressionLevel ...", "ConnectionAttempts ...", "ConnectTimeout ...", "ControlMaster ...", "ControlPath ...", "ControlPersist ...", "DynamicForward ...", "EscapeChar ...", "ExitOnForwardFailure ...", "ForwardX11Timeout ...", "ForwardX11Trusted ...", "GatewayPorts ...", "GlobalKnownHostsFile ...", "GSSAPIAuthentication ...", "GSSAPIDelegateCredentials ...", "HashKnownHosts ...", "Host ...", "HostbasedAuthentication ...", "HostKeyAlgorithms ...", "HostKeyAlias ...", "HostName ...", "IdentityFile '~/.ssh/bkp'", "IdentitiesOnly ...", "IPQoS ...", "KbdInteractiveAuthentication ...", "KbdInteractiveDevices ...", "KexAlgorithms ...", "LocalCommand ...", "LocalForward ...", "LogLevel ...", "MACs ...", "NoHostAuthenticationForLocalhost ...", "NumberOfPasswordPrompts ...", "PasswordAuthentication ...", "PermitLocalCommand ...", "PKCS11Provider ...", "Port 9999999", "PreferredAuthentications ...", "Protocol '2,1'", # 2 preferred, 1 also acceptable "ProxyCommand ...", "PubkeyAuthentication ...", "RekeyLimit ...", "RemoteForward ...", "RequestTTY ...", "RhostsRSAAuthentication ...", "RSAAuthentication ...", "SendEnv ...", "ServerAliveInterval ...", "ServerAliveCountMax ...", "StrictHostKeyChecking ...", "TCPKeepAlive ...", "Tunnel ...", "TunnelDevice ...", "UsePrivilegedPort ...", "User ...", "UserKnownHostsFile ...", "VerifyHostKeyDNS ...", "VisualHostKey ...", "XAuthLocation ..." ]);