Training, Open Source computer languages
PerlPHPPythonMySQLApache / TomcatTclRubyJavaC and C++LinuxCSS 
Search for:
Home Accessibility Courses Diary The Mouth Forum Resources Site Map About Us Contact
 
For 2023 (and 2024 ...) - we are now fully retired from IT training.
We have made many, many friends over 25 years of teaching about Python, Tcl, Perl, PHP, Lua, Java, C and C++ - and MySQL, Linux and Solaris/SunOS too. Our training notes are now very much out of date, but due to upward compatability most of our examples remain operational and even relevant ad you are welcome to make us if them "as seen" and at your own risk.

Lisa and I (Graham) now live in what was our training centre in Melksham - happy to meet with former delegates here - but do check ahead before coming round. We are far from inactive - rather, enjoying the times that we are retired but still healthy enough in mind and body to be active!

I am also active in many other area and still look after a lot of web sites - you can find an index ((here))
invalid command name else error

Posted by sgdought (sgdought), 8 December 2007
The following script connect to various wireless controllers and quieries the model number.

If I use consecutive if commands, it works fine.  If I try to use elseif and else I get the error:
invalid cammand name else. (near the bottom of the code).

I'm running Activestate TCL 8.4 for Windows.

# The following line is required for Windows versions
# of Expect.
package require Expect
package require log


# Parse command line arguements
if {[llength $argv] != 6} {
    puts ""
    puts "Arguements not optional. Six arguements required!"
    puts ""
    puts "Usage: test.exp {-command (C:\\OpennSSH\\ssh.exe) -IP Address -Switch (should be -l)

-Username -Password -Enable Password}"
    puts ""
    puts "Example: tclsh84 c:\\scripts\\UpdAruba.exp c:\\OpenSSH\\bin\\ssh.exe

10.121.0.170 -l admin password EnablePassword"
    exit 1
}

# Set variables to arguements supplied with script
set host [lindex $argv 1]
set switch [lindex $argv 2]
set username [lindex $argv 3]
set password [lindex $argv 4]
set EnablePassword [lindex $argv 5]

set logFile  c:\\temp\\host.txt
puts "\nThe logfile is $logFile\n"

# open the input file
set InputFile [open "C:\\d'sdata\\Work_Jeffco\\ExpectInputfile.txt" "r"]

# Begin Input While
# read a line from input file until the EOF

while {[gets $InputFile line] != -1 } {
send_user "\n"
send_user "\nThe school is $line.\n"
gets $InputFile IP
send_user "The IP of the controller is  $IP.\n"

# Replace the arguement holding the IP Address
# with the next IP from the file.
set argv [lreplace $argv 1 1 $IP]

# start logging
exp_log_file -a $logFile

# Begin connect code body
# continue stops the current iteration of the while loop
# Use break to exit loop totally
set timeout 20

# spawn ssh command using script arguements
eval spawn [lrange $argv 0 3]

# make choice depending on results of ssh command
expect {
       timeout   {set ErrorLog [open "C:\\temp\\errorlog.txt" "a"]
                  puts $ErrorLog "Cannot connect to $line. Timed Out\r\n"
                  close $ErrorLog
                  continue}
       "refused" {set ErrorLog [open "C:\\temp\\errorlog.txt" "a"]
                  puts $ErrorLog "Connection refused to $line. Connection refused.\r\n"
                  close $ErrorLog
                  continue}
       "finger"  {send "yes\r"
                  sleep 2
                  send $password\r  
                  sleep 2}
       "word"    {send $password\r
                  sleep 2}
       }


set timeout 60
sleep 2

send "enable\r"

sleep 2

expect "word"

send "$EnablePassword\r"

expect "#"

sleep 1

send_user "\nconnected\n"

# expect "#"

sleep 2

send "Show Version\r"
expect "#"
sleep 2

# Extract model number from results of Show Version command

set output $expect_out(buffer)
if {[string last "Aruba800" ($output)] != -1}  {
    send_user "\r\nThe $line controller is an Aruba 800.\r\n"
} elseif {[string last "Aruba2400" ($output)] != -1}   {
          send_user "\r\nThe $line controller is an Aruba 2400.\r\n"
} elseif {[string last "Aruba200" ($output)] != -1}   {
          send_user "\r\nThe $line controller is an Aruba 200.\r\n"
}
 else {send_user "\r\nThe controller is not a 200, 800, or 2400"
       continue
}






send "exit\r"


# End Input while
}



log_file
close $InputFile
close $ErrorLog



Posted by admin (Graham Ellis), 9 December 2007
on 12/08/07 at 20:59:43, sgdought wrote:
If I use consecutive if commands, it works fine.  If I try to use elseif and else I get the error:
invalid cammand name else. (near the bottom of the code).

if {[string last "Aruba800" ($output)] != -1}  {
    send_user "\r\nThe $line controller is an Aruba 800.\r\n"
} elseif {[string last "Aruba2400" ($output)] != -1}   {
          send_user "\r\nThe $line controller is an Aruba 2400.\r\n"
} elseif {[string last "Aruba200" ($output)] != -1}   {
          send_user "\r\nThe $line controller is an Aruba 200.\r\n"
}
 else {send_user "\r\nThe controller is not a 200, 800, or 2400"
       continue
}


else isn't a separate command - it's an optional parameter within the if command.  You have ended your if command (a new line ends a command in Tcl !) on the line before the else.   Either add a \ on the end of that previous line, or move the else up.   Personally, I would write

}  else  {
       send_user "\r\nThe controller is not a 200, 800, or 2400"

to keep it consistent with the elseifs




Posted by sgdought (sgdought), 9 December 2007
Thanks, that worked.  I'm a network admin and I've been scripting in vbscript for a year or two.  I've just started to learn Expect and TCL.

I appreciate the help.

Posted by admin (Graham Ellis), 9 December 2007
My pleasure, ... and I'm a trainer so I will always tend to give an explanation as well as a solution



This page is a thread posted to the opentalk forum at www.opentalk.org.uk and archived here for reference. To jump to the archive index please follow this link.

Comment: "thanks ..."
Visitor Ranking 4.0 (5=excellent, 1=poor)

Comment by Anon (published 2011-03-19)
thanks [#3928]

You can Add a comment or ranking or edit your own comments

Average page ranking - 4.0

© WELL HOUSE CONSULTANTS LTD., 2024: Well House Manor • 48 Spa Road • Melksham, Wiltshire • United Kingdom • SN12 7NY
PH: 01144 1225 708225 • FAX: 01144 1225 793803 • EMAIL: info@wellho.net • WEB: http://www.wellho.net • SKYPE: wellho