I have a script that I didn’t write. When it is run, it outputs some information, then expects the user to press enter, and then outputs the last part of the information. This is what I need. I need to get that last part i.e. last line of output programmatically. When I run the script locally as follows:

RESULT=$(echo -ne '\n' | script $param)  

I can get the output and process it, but when I try to run the same remotely i.e.

RESULT=$(echo -ne '\n' | ssh remoteserver script $param)  

The script hangs. It Seems that the pipe of the new line does not work for the remote ssh.

How can I fix this?

The script takes input from terminal directly and is a perl script, just in case it matters

  • Are you able to modify the script to comment out the read that temporarily stops the script? – Kusalananda Mar 30 '18 at 8:29
  • What do you get if you run ssh remoteserver script $param as the same user with the same values as the script would do so? – roaima Mar 30 '18 at 8:30
  • @Kusalananda:No I can't control that – Jim Mar 30 '18 at 8:30
  • @roaima:Some information, prompt to press enter, some more information – Jim Mar 30 '18 at 8:31
  • 1
    Sounds like it should be easily fixed by removing the thing that outputs the prompts and waits for user input. If that's not possible in the script because of permissions, then it will be possible in a copy of the script. – Kusalananda Mar 30 '18 at 8:42

Fake a terminal and "type" the required data. First up our test program testproggie on the remote system

#!/usr/bin/env perl
use 5.14.0;
use warnings;

say "one thing";

open my $fh, '<', '/dev/tty' or die "nope on /dev/tty: $!\n";
readline $fh;

say "another thing";

Which will indeed fail if you remote a newline to it

$ printf "\n" | ssh test.example.edu ./testproggie
one thing
nope on /dev/tty: No such device or address

So now we fake a terminal with remotenl on the local system

#!/usr/bin/env expect

#set timeout 999
#match_max 99999

# this assumes the remote side does not do anything silly with
# the shell; if it does you may need to spawn a remote shell
# and then {send "./testproggie\r"} to that and then...
spawn -noecho ssh -q -t test.example.edu ./testproggie
# this can be improved if you know what the line before the
# wait-for-the-return-key will contain
expect -re .
send "\r"
expect eof

# this could be simplified with better expect calls, above
regexp {([^\r\n]+)\r\n$} $expect_out(buffer) unused lastline
puts ">>>$lastline<<<"

and run it

$ ./remotenl
one thing

another thing
>>>another thing<<<

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.