0
 function put_SwVersion_MENAME()
{
    output=$(expect -c '
        log_user 0
        match_max 2000
        spawn ssh -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null username@IP
        expect "*?Password:*"
        send "dummmy\r"
        expect "*#"
        send "cat /storage/system/abcd.txt\r"
        expect "cat /torage/system/abcd.txt\n"
        puts '"\$expect_out(buffer)"'
    ')
        echo $output
        return $TRUE
    }

    put_SwVersion_MENAME

The output value is containing the prompt rather than the send output how can i parse to get the correct value CAT?

  • Look at this: http://stackoverflow.com/questions/15013481/in-bash-how-to-store-a-return-value-in-a-variable – Matt Jan 12 '16 at 12:01
  • @user : That is different, this is expect programming, getting the server executed value to the local script and actually i got it to working i will update the code soon, but the value i'm getting is wrong – Phaninder Gattu Jan 12 '16 at 12:56

2 Answers2

0

The prompt should be given properly. Since the command and pattern both are same, it leads to matching the command which is being sent by us itself. (i.e. cat /storage/system/abcd.txt\r).

Instead, a generalized approach for the matching the prompt is follows,

set prompt "\r\n(.*?)(#|%|>|\\\$) $"

Here, we have escaped the literal dollar sign with backslash and the last dollar is meant for the end-of-line regular expression.

#!/bin/bash
function put_SwVersion_MENAME() {
        output=$(expect -c '
        log_user 0
        set prompt "\r\n(.*?)(#|%|>|\\\$) $"
        spawn ssh dinesh@xxx.xx.xx.xxx
        expect "password"
        send "welcome123\r"
        expect -re $prompt
        send "cat /storage/system/abcd.txt\r"
        expect -re $prompt
        puts "$expect_out(1,string)"
        ')
        echo "===>$output<=="
}
put_SwVersion_MENAME
Dinesh
  • 16,014
  • 23
  • 80
  • 122
0

I too have face similar problem and it got resolved after adding an \r and expect in between. This can help you. please try.

    expect "password"
    send "welcome123\r"
    expect -re $prompt
    send "cat /storage/system/abcd.txt\r"
    expect -re $prompt
    send "\r"
    expect -re $prompt
    puts "$expect_out(buffer)"
vishnu
  • 21
  • 5