LinuxQuestions.org
Share your knowledge at the LQ Wiki.
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-24-2009, 07:39 AM   #1
frater
Member
 
Registered: Jul 2008
Posts: 121

Rep: Reputation: 23
looping breaks when I call a script with expect in it


I wrote a script to check cpe's (modems) remotely.
It telnets / ssh's into the cpe gets some parameters and writes them to a file.
This script uses 'expect' and works fine.

After writing this script I wanted to write a script which parses the file with all the cpe-credentials and have them checked with only 1 command. "Quite easy" I thought...
Code:
while read line ; do
   HOST="`echo "${line}" | awk -F\; '{print $1}' | grep "\."`"
   PORT="`echo "${line}" | awk -F\; '{print $3}'`"
   if [ ! -z "${HOST}" ] ; then
     echo -n "\"${HOST}\"," >>/var/run/allcpes
     chkcpe $HOST >>/var/run/allcpes
   fi
done </etc/chkcpe.cpe
echo ""
This script would only call the first modem in the file /etc/chkcpe.cpe.
The script didn't crash because it did execute the 'echo ""'

I did some extensive debugging and it turned out it that it only happened after invoking "expect" within the script "chkcpe"

I didn't want to surrender so I wrote this workaround. It also proofs somehow that there's something tricky going on with expect.

Code:
n=1
while true ; do
   line="`cat /etc/chkcpe.cpe | awk "NR == $i"`"
   let i+=1
   [ -z "$line" ] && exit

   HOST="`echo "${line}" | awk -F\; '{print $1}' | grep "\."`"
   PORT="`echo "${line}" | awk -F\; '{print $3}'`"
   if [ ! -z "${HOST}" ] ; then
     echo -n "\"${HOST}\"," >>/var/run/allcpes
     chkcpe $HOST >>/var/run/allcpes
   fi
done
echo ""
Although I found a solution that works, I'd still like to know why it is doing this. To make it complete... This is the code that breaks it.

Code:
    expect -c "set timeout -1;\
    spawn ssh -o StrictHostKeyChecking=no -p $PORT $HOST -l ${USERNAME} ;\
    match_max 100000;\
    expect *assword:*;\
    send -- ${CREDENTIAL}\r;\
    expect ${WAITFOR}*;\
    send -- get\ WANDevice.\r;\
    expect OK;\
    expect ${WAITFOR}*;\
    send -- get\ DeviceInfo.\r;\
    expect OK;\
    expect ${WAITFOR}*;\
    send -- exit\r;\
    interact;" >$FOUTPUT 2>$FERROR
BTW...
I also tested:
cat /etc/chkcpe.cpe | while read line ; do ; script ; done
 
Old 06-24-2009, 10:53 AM   #2
frater
Member
 
Registered: Jul 2008
Posts: 121

Original Poster
Rep: Reputation: 23
Another thing I noticed with "expect" is that it stops halfway when I run it as a daemon. Anyone noticed this? I did test it on 2 seperate machines (Red Hat Linux 9 and Fedora Core 9)

And more important.....
The cure?

I think both problems have the same cause......

Code:
    expect -c "set timeout -1;\
    spawn ssh -o StrictHostKeyChecking=no -p $PORT $HOST -l ${USERNAME} ;\
    match_max 100000;\
    expect *assword:*;\
    send -- ${CREDENTIAL}\r;\
    expect ${WAITFOR}*;\
    send -- get\ WANDevice.\r;\
    expect OK;\
    expect ${WAITFOR}*;\
    send -- get\ DeviceInfo.\r;\
    expect OK;\
    expect ${WAITFOR}*;\
    send -- exit\r;\
    interact;"
 
Old 06-25-2009, 04:20 AM   #3
frater
Member
 
Registered: Jul 2008
Posts: 121

Original Poster
Rep: Reputation: 23
After I changed the way I was calling expect I was able to use a normal "while read line ; do ; code ; done <file" construct.

In the script I was calling "expect -c "<expect commands>"

I changed this by making a seperate executable file:
Code:
#!/usr/local/bin/expect -f
set HOST [lindex $argv 0]
set PORT [lindex $argv 1]
set USERNAME [lindex $argv 2]
set CREDENTIAL [lindex $argv 3]
set WAITFOR [lindex $argv 4]

set timeout 90
spawn ssh -o StrictHostKeyChecking=no -p $PORT $HOST -l "${USERNAME}"
match_max 100000
expect "*assword:*"
send "${CREDENTIAL}\r"
expect "${WAITFOR}*"
send "get WANDevice.\r"
expect "OK"
expect "${WAITFOR}*"
send "get DeviceInfo.\r"
expect "OK"
expect "${WAITFOR}*"
send "exit\r\r"
expect eof
exit 0
And just invoke that expect-script from within the 2nd bash script.
It then didn't escape the loop...

Daemonizing the command wasn't necessary at all.... I can just set the timeout to a reasonable timespace to be sure it doesn't wait forever.
I also got rid of the "interact" command which I just copy/pasted from an example..
I also prefer to call expect from a file after all.
Less escape characters.....
 
  


Reply



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
How to call expect in shell script? john.daker Programming 2 08-25-2008 08:33 PM
Looping in script help needed ZAMO Linux - General 5 08-18-2008 05:17 AM
Looping a script Keentolearn Linux - Newbie 4 01-30-2007 03:54 PM
shell script looping ust Linux - General 2 01-19-2005 04:05 AM

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

All times are GMT -5. The time now is 08:43 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