0

I am new to using expect. I am running the below code to get the token from a server:

set timeout 20

set token ""

sleep 1

spawn ssh -l $serveruser1 $serverip

# -- is used to disable the interpretion of flags by Expect.
expect {
    -re "OK"        {
        send_user "** Enter command to get the token. **\n"
        send -- "-t $switchtype -s h -c \[Long_CLLI:$switchname\] -u $switchuser -l auto-sarm -e n \r"
    }
    timeout {
        send_user "** Session timeout on $serverip upon LOGIN**\n"
        exit -1
    }
}

sleep 1


expect {
    -indices -re "\n.+\n(.+\@)" {
        set token $expect_out(1,string)
        send_user "**Get the token $token **\n"}
    timeout {
        send_user "** Session timeout upon getting token**"
        exit -1}
}

The code is working fine for most of the switches, however for few it is failing by returning code RE006. Therefore, for those switches I need to change the switch type. I have made the below change:

set timeout 60

set token ""

sleep 1

spawn ssh -l $serveruser1 $serverip

# -- is used to disable the interpretion of flags by Expect.
expect {
    -re "OK"        {
        send_user "** Enter command to get the token. **\n"
        send -- "-t $switchtype -s h -c \[Long_CLLI:$switchname\] -u $switchuser -l auto-sarm -e n \r"
    }
    timeout {
        send_user "** Session timeout on $serverip upon LOGIN**\n"
        exit -1
    }
}

sleep 1

expect {
    -re "RE006"        {
        send_user "** Enter command to get the token. **\n"
        send -- "-t $switchtype1 -s h -c \[Long_CLLI:$switchname\] -u $switchuser -l auto-sarm -e n \r"
    }
    timeout {
        send_user "** Session timeout on $serverip upon LOGIN**\n"
        exit -1
    }
}

expect {
    -indices -re "\n.+\n(.+\@)" {
        set token $expect_out(1,string)
        send_user "**Get the token $token **\n"}
    timeout {
        send_user "** Session timeout upon getting token**"
        exit -1}
}

Now, this works fine for the previously failed switches. How can I handle both cases?

1
  • It would be nice to see the typical output from both types of switch. Reverse engineering what the output might be from the regular expressions matching them is hard! When the pattern is essentially a line with anything followed by some text ending in @, and the other case is some text with RE006 there is a lot of scope for overlap. Having random sleep 1 in an expect script is also a bad sign!
    – icarus
    Feb 17, 2020 at 1:13

1 Answer 1

0

This is a guess, more information is needed. Normally I would structure this with a command to look for a prompt, but since we don't know what the prompt looks like I am just adapting the OP's program which it is claimed is working.

The main change is to look for the RE006 and if it is seen then output the needed command and then issue exp_continue to repeat the current expect statement. This is generally better than looking for the RE006 with a shortish timeout and doing nothing if we timeout.

set timeout 60

set token ""

spawn ssh -l $serveruser1 $serverip

# -- is used to disable the interpretion of flags by Expect.
expect {
    -re "OK"        {
        send_user "** Enter command to get the token. **\n"
        send -- "-t $switchtype -s h -c \[Long_CLLI:$switchname\] -u $switchuser -l auto-sarm -e n \r"
    }
    timeout {
        send_user "** Session timeout on $serverip upon LOGIN**\n"
        exit -1
    }
}

# If we get a RE006 code in response to sending "-t $switchtype" then
# send a "-t $switchtype1".
# We then look for at least two lines of output, the first being non-empty
# and the second having a "@" character in it and at least one character before
# the "@". If there is more that one "@" on the second line all the text up
# to the last "@" inclusive will be returned as the token.

expect {
    "RE006"        {
        send_user "Got RE006 response, switching to alternative switchtype\n"
        send -- "-t $switchtype1 -s h -c \[Long_CLLI:$switchname\] -u $switchuser -l auto-sarm -e n \r"
        exp_continue}
    -re "\n.+\n(.+\@)" {
        set token $expect_out(1,string)
        send_user "**Get the token $token **\n"}
    timeout {
        send_user "** Session timeout upon getting token**"
        exit -1}
}

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .