2

i wrote a little script which executes the ls command. Now I want to store the output of that command in a variable to work with it in the further perl script. Unfortunately I cannot grab the output from the ls command.

my ($core) = "servername";
    my $tel_con = "ssh ".$core. " -l $user";

    $ssh = Expect->spawn("$tel_con");
    $ssh->log_stdout(1);

    unless ( $ssh->expect( 5, -re => '$' ) ) {
            return "Never connected " . $ssh->exp_error() . "\n";
    }
    sleep 1;
    $ssh->send_slow(0, "ls -l\r");
    $ssh->clear_accum();
    my $str = $ssh->exp_after();
    print "STR = '$str'\n";

Maybe you can give me some help please?

6 Answers 6

1
use Net::OpenSSH;

my $ssh = Net::OpenSSH->new($core, user => $user);
$ssh->error and die $ssh->error;

my $output = $ssh->capture('ls -l');

print "command output:\n$output\n\n";
Sign up to request clarification or add additional context in comments.

Comments

1

In case you can not or don't want to use Net::OpenSSH you may do:

my @output = $exp->expect(5);
print 'OUT: '.$output[3].'END';

To get the whole output (including the used command, return string, console information)

Comments

0

you could call expect in a seperate process and grab the output via qx or open a pipe

3 Comments

Maybe you can give me a short example?
Example: copy the expect-code in an extra file ssh.pl and do a my $ls_content = qx(ssh.pl $args);
Sorry but I'm really new to expect. So I call the upper posted script with ls_content = qx(ssh.pl) ?
0

What is send_slow? Depending on how this command sends the ls command, the output can be received in different ways.

Most probably, the error output of the command is stored in the $? variable, possibly byte-shifted.

Comments

0

It seems that Expect will redirect everything to STDOUT and log it internally. Since you enabled output with $ssh->log_stdout(1), you should be able to get the results of ls -l directly from STDOUT (maybe by redirecting standard out to a variable). You can also see try grab the data from the internal logging. Just make sure to grab the output before doing clear_accum().

From CPAN: $object->send_slow($delay, @strings);

... After each character $object will be checked to determine whether or not it has any new data ready and if so update the accumulator for future expect() calls and print the output to STDOUT and @listen_group if log_stdout and log_group are appropriately set.

1 Comment

Thanks. Can you give me a little code example please? I tried the following: $telnet = Expect->spawn("$tel_con"); $telnet->debug(3); $telnet->exp_internal(1); $telnet->log_stdout(0); $telnet->set_group($ret); $telnet->log_group(1); $telnet->expect(0); unless ( $telnet->expect( 5, -re => '$' ) ) { return "Never connected" . $telnet->exp_error() . "\n"; } sleep 1; $telnet->send_slow(0, "ls -l\r"); my $str = $telnet->before(); print "STR = '$str'\n"; print "ret = $ret\n";
0

Layman's terms?

I must obligatorily post this link - I still go there from time-to-time, it is the most layman-y explanation I've ever found of all things regex:

http://www.anaesthetist.com/mnm/perl/Findex.htm

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.