Find the answer to your Linux question:
Results 1 to 3 of 3
Hello All, I have an expect script, which does SSH into an remote machine and executes a certain command. I capture the result of command using Expect and is available ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    Apr 2009
    Posts
    17

    Returning a string from EXPECT SCRIPT


    Hello All,

    I have an expect script, which does SSH into an remote machine and executes a certain command. I capture the result of command using Expect and is available within script under $expect_out(buffer). My issue is that i am calling my expect script from another PERL script and i need to access the string stored in $expect_out(buffer) in my calling PERL SCRIPTING FOR SOME PROCESSING, please suggest if it possible.
    Thanks in advance for your suggestions
    Regards,
    Nitesh Bansal

  2. #2
    Trusted Penguin
    Join Date
    May 2011
    Posts
    4,353
    first thing that leaps to mind: is there some reason why you are not using the Perl Expect module?

    i'll assume that there is, and you must use an external expect script. in your case, the first thing i'd do is modify the expect script to output the buffer in an obvious line, something like:

    Code:
    set foo $expect_out(buffer)
    
    send_user "\n\n<output>$foo</output>\n\n"
    then in your perl script, when you call the expect script, capture the output of the script and parse it, looking for the above <output> string, e.g.:

    Code:
    #!/usr/bin/perl
    
    local $/ = undef;
    open(PH,'/tmp/script.exp |') or die "can't run script.exp: $!\n";
    my $lines = readline(*PH);
    close(PH);
    
    # replace carriage return & newline from expect w/just newline
    $lines =~ s/\r\n/\n/g;
    my @lines = split(/\n/,$lines);
    
    my $done;
    my @match;
    for(@lines){
      if(/<output>(.*)$/){
        push(@match,$1);
      }elsif(/^(.*)<\/output>/){
        push(@match,$1);
        $done = 1;
      }else{
        push(@match,$_) if($#match>=0);
      }
    }
    
    for(@match){
      print "MATCH: ",$_,"\n";
    }
    Note: that could have been done more easily, i just got sideways and kept going...

  3. #3
    Just Joined!
    Join Date
    Apr 2009
    Posts
    17
    Thanks for reply mate.
    It is working fine now, your suggestion has been helpful and yeah its working

  4. $spacer_open
    $spacer_close

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •