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