colttt
colttt

Reputation: 492

if statement, doesn't work

I've the following script:

#!/bin/bash

pw=$1
pw2=$2
file=$3
user="admin"

if [ $# -eq 0 ]
        then
                echo "bitte PW und IP-file als Parameter angeben"
fi


for ip in `cat $file`;
        do
                ping -c1 $ip >/dev/null 2>&1
                if [ $? -eq 0 ];  
                        then      
                                echo $ip
                                /usr/bin/expect <<EOD
                                spawn /usr/bin/telnet $ip
                                if { expect "Please Enter Password:" } {
                                        send $pw\r }
                                } elseif{ expect "Please Enter Login Name:" } {
                                        send $user\r
                                        send $pw\r }
                                expect telnet@*
                                send ena\r
                                expect "Password:"
                                send $pw2\r
                                expect "telnet@*"
                                send skip-page-display\r
                                expect telnet@*
                                send "show running-config\r"
                                log_file "$ip.conf"
                                expect "telnet@*"
                                send "exit\r"
                                expect "telnet@*"
                                send "exit\rn"
EOD                               
                else              
                        echo "$i nicht erreicht"
fi                                
done  

but it didn't work, I got the following error:

spawn /usr/bin/telnet IP
invalid bareword "expect"
in expression " expect "Please Enter Passwor...";
should be "$expect" or "{expect}" or "expect(...)" or ...
    (parsing expression " expect "Please Enter ...")
    invoked from within
"if { expect "Please Enter Password:" } {
                                        send top_SECRET\r }"

any ideas how I get this to work? Some Switches have need a username, some others doesn't, so I need an If-Statement to check what ist expected. Thanks in advance for you help!

Upvotes: 0

Views: 318

Answers (2)

glenn jackman
glenn jackman

Reputation: 247210

You conditionally expect different things by using expect with several pattern/acction pairs, and use exp_continue. Instead of

if { expect "Please Enter Password:" } {
    send $pw\r }
} elseif { expect "Please Enter Login Name:" } {
    send $user\r
    send $pw\r 
}
expect telnet@*

Do this

expect {
    "Please Enter Password:"   { send $pw\r; exp_continue }
    "Please Enter Login Name:" { send $user\r$pw\r; exp_continue }
    telnet@*
}

You had an error: elseif requires a space between it and the open brace. And yes, expect is unknown to a Tcl expression (docs)

After you send exit, you should expect eof

Upvotes: 1

msw
msw

Reputation: 43527

As the TCL reference documentation shows, if requires an expression as its condition.

if expr1 ?then? body1 elseif expr2 ?then? body2 elseif ... ?else? ?bodyN?

expect "string" is not a valid expression as evaluated by expr.

Upvotes: 0

Related Questions