0

I'm getting an error that says:

 invalid command name "
    while executing
""\r""
    invoked from within
"expect {
    -ex "--More--" {send -- " "; exp_continue}
     "*>" {send "exit \r";"\r"}
}"
    (file "./scriptprueba4.sh" line 12)

in an expect script that used to work fine. I modified the script in order to get information from show CDP neighbor and then the error appeared.

This is the code:

#!/usr/bin/expect -f
#!/bin/sh
set DATE [exec date +%F]
spawn telnet 111.111.1.111
expect "Username:"
send "user\r"
expect "Password: "
send "this.is.the.pass\r"
expect "*>"
send "show cdp neighbors \r"
log_file -noappend CDP.dat
expect {                               #this is the part i just modified and caused the error.
    "More--" {send " "; exp_continue}
    ">" {send "exit \r"; "\r"}
}
expect "$ "                            #I want this part of the code to be executed after "$" but the script stops before doing it 
send "tail -n+6 CDP.dat > CDP2.dat\r"
expect "$ "
send "./primeros3.sh \r"
expect "$ "
send -- "tr -d '\\r' < CDPyPuerto.dat | awk '{ printf \"%s %s\", \$0, (length(\$1) > 16) ? OFS : ORS}' > TablaCDP.dat \r"
expect "$ "

The part of the code that is not working, is used to handle multiple pages of terminal output, the script is working fine until:

expect {                               
    "More--" {send " "; exp_continue}
    ">" {send "exit \r"; "\r"}
}

Any help?

Update: i have changed the code this way:

#!/usr/bin/expect -f
#!/bin/sh
set DATE [exec date +%F]
spawn telnet 111.111.1.111
expect "Username:"
send "user\r"
expect "Password: "
send "this.is.the.pass\r"
expect "*>"
send "show cdp neighbors \r"
log_file -noappend CDP.dat
expect -ex "--More--" {send -- " "; exp_continue}
expect "*>"
send "exit \r"
expect "$ "
send "tail -n+6 CDP.dat > CDP2.dat\r"
expect "$ "
send "./primeros3.sh \r"
expect "$ "
send -- "tr -d '\\r' < CDPyPuerto.dat | awk '{ printf \"%s %s\", \$0, (length(\$1) > 16) ? OFS : ORS}' > TablaCDP.dat \r"
expect "$ "

The error now says:

send: spawn id exp4 not open
    while executing
"send "tail -n+6 CDP.dat > CDP2.dat\r""
    (file "./scriptprueba4.sh" line 16)
  • 2
    What exactly is that ; "\r" for? – thrig Feb 3 '17 at 17:51
  • It's a try to make it work, i have tested different sintaxis, i only need the script to type "exit \r" if the prompt appears, but if i left the second argument empty, another error appears. – Cesar Alejandro Villegas Yepez Feb 3 '17 at 17:54
  • What happens when you remove that ; "\r"? – thrig Feb 3 '17 at 18:05
  • if i remove it it sends "exit \r" but this error appears: send: spawn id exp4 not open while executing "send "tail -n +6...." – Cesar Alejandro Villegas Yepez Feb 3 '17 at 18:08
  • exit\r makes the telnet session close. Because of this, the spawn is no longer open. You could exec a shell script to carry out those various shell commands afterwards. – thrig Feb 3 '17 at 19:12

Your Answer

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

Browse other questions tagged or ask your own question.