LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 06-06-2013, 10:27 AM   #1
wolverene13
Member
 
Registered: May 2010
Location: Matiland, FL
Distribution: Debian Squeeze
Posts: 57

Rep: Reputation: 0
Expect/Tcl script combining foreach and while commands


Hello,

I already know how to read from a list of devices and log in to issue commands via expect, based on a list of commands for each specific device, like this:

Code:
set USER [lindex $argv 0]
set PASS [lindex $argv 1]
set NODE [lindex $argv 2]

set timeout 15

set LOAD_CMD [open "./cpe_check-files/rad-commands.txt" r]
set CMD_LIST [split [read $LOAD_CMD] "\n"]
close $LOAD_CMD

spawn ssh $USER@$NODE

expect {
        timeout {
                puts "\n\n##### $NODE - UNREACHABLE #####\n"; exit 3
        }
        -re ".*Are.*.*yes.*no.*" {
                send "yes\r"
        }
        "password:" {
                send "$PASS\r"
        }
}
        foreach COMMAND $CMD_LIST {
            if {$COMMAND != ""} {
                        expect {
                                "# $" {send "$COMMAND\r"; break}
                                }
            }
        }
        expect *# {
            send -- "logout\r"
        }

        close
~
And I also know how to compensate for a device where you cannot set terminal length to 0, like this:

Code:
set NODE [lindex $argv 0]
set USER [lindex $argv 1]
set PASS [lindex $argv 2]

set timeout 30

spawn ssh -o "StrictHostKeyChecking no" $NODE

expect {*assword: } {}\
       timeout {puts "\n\n**** INCORRECT LOGIN ****\n\n"; exit 3}
send "$PASS\n"
expect {*#} {}\
        timeout {puts "\n\n**** PASSWORD INCORRECT ****\n\n"; exit 3}
send "info\r"
        while (1) {
            expect {
               timeout break
               "more.." {send " "}
               "# $" {send "logout\r"; break}
            }
        }
But I am having a terrible time trying to combine these two things. I have a list of commands that may vary for a given device (hence the reading the commands from a list thing). The length of the output of each of these commands may end up being longer than the terminal allows, so a "more.." prompt will be seen. At that point, a human being would have to press the space bar to show the rest of the output until it is finished and gets back to the "#" prompt. I need the expect script to both read the commands from a list, then for each command, send a space any time it sees "more.." in the output, and move on to the next command until they are done, then log out. The device has a bug in it where if you set the terminal length to 0, it will stay tat way forever until the device is rebooted, so I cannot simply add "term len 0" or anything like that to the script.

Any help would be greatly appreciated.

Thanks in Advance,

Wolverene13
 
  


Reply

Tags
command, expect, foreach, loop, tcl



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Expect script: how do i send function key F12 in an expect script alix123 Programming 4 09-01-2013 09:06 PM
Executing tcl script by logging to a linux machine is there any way other than expect kamesp Linux - Software 2 06-15-2012 11:44 AM
Issue in running commands in expect script from shell script yadvinder Programming 0 05-31-2012 04:07 AM
tcl foreach loop not reading variables Stuart07 Programming 1 03-11-2011 02:45 PM
TCL e EXPECT gustavolinux Programming 1 10-17-2008 09:03 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 04:41 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration