?
Solved

another EXPECT problem

Posted on 2001-09-10
2
Medium Priority
?
496 Views
Last Modified: 2012-06-27
Dear Solutions,

I have now one problem with expect where I am trying to generate one script to automate file transfer. The script will be run from machine A which will then telnet to machine B and create another Expect script on machine B which will then spawn another process into machine C and do the file transfer.

Since there could be more than 1 file to be transfer, i wanted to have a loop where, if there
is still file on then, continue transfering.

This is how the script looks like:

#!/usr/local/bin/expect -f

spawn telnet fieldu6
expect "login:"
send "spett\r"
expect "Password:"
send "spett\r"

expect "spett@fieldu6:"
send "cat>next-hop<<EOF\r"
expect "?"
send "#!/usr/local/bin/expect -f\r"
expect "?"
send "spawn ftp eqm31s14p2\r"
expect "?"
send "set transfer_flag 1 \r"
expect "?"
send "expect \"Name (eqm31s14p2:spett):\"\r"
expect "?"
send "send \"root\\r\"\r"
expect "?"
send "expect \"Password:\"\r"
expect "?"
send "send \"wpp_admin\\r\"\r"
expect "?"
send "expect \"ftp>\"\r"
expect "?"
send "send \"bin\\r\"\r"
expect "?"
send "send \"cd /Core/log/\\r\"\r"
expect "?"
send "expect \"ftp>\"\r"
expect "?"
send "send \"mget *.Z\\r\"\r"
expect "?"
send "send \"mget *.Z\\r\"\r"
expect "?"
send "expect \"*Z?\"\r"
expect "?"
send "send \"y\\r\"\r"
expect "?"
send "while {\$transfer_flag} { \\\r" #problem.
expect "?"
send "if {expect \"*Z?\"} { \\\r"
expect "?"
send "send \"y\\r\" \\\r"
expect "?"
send "} else { \\\r"
expect "?"
send "set transfer_flag 0 \\\r"
expect "?"
send "} \r"
expect "?"
send "interact\r"
expect "?"
send "EOF\r"
expect "spett@fieldu6:"
send "chmod +x next-hop\r"
expect "spett@fieldu6:"
send "./next-hop\r"
interact

>>end of script
....continue here
? expect "ftp>"
? send "mget *.Z\r"
? expect "*Z?"
? send "y\r"
? while {$transfer_flag} { \  #problem here
transfer_flag: Undefined variable.
spett@fieldu6: /home/spett                       42 > if {expect "*Z?"} { \
? send "y\r" \
? } else { \
? set transfer_flag 0 \
? }
Missing }.
spett@fieldu6: /home/spett                        43 > interact


When i try to run this code, i have this error message, which to me seems that I didnt really escape the '$'sign which i think i already did.
How to solve this problem.

thanks alot.
0
Comment
Question by:mafendee
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 2

Accepted Solution

by:
kyrmit earned 400 total points
ID: 6532857
Your main problem is that the cat<<EOF>next-hop is a shell command and thus subject to the usual shell escapes. So not only does expect interpolate the $, but also the shell.

You can avoid a lot of hassle caused by the interpolation of expect here by using {} :
send {cat<<EOF>next-hop
...
while { \$transfer_flag } {
..
EOF\r
}

this allows the secondary script to be passwd without any interpolation, i.e. $ stay $ and " stay ", particularely the \$ stays \$ for the shell.

However, you may find the ftp command 'prompt' useful which
toggles the question during mget from on to off. See the ftp man page for it's usage. By issuing "bin" and "prompt" the ftp client will never ask you if you really want to get a file during mget. This bypasses the need for the while loop in this case altogether.

One more thing: there is an error in the loop in that expect is used as the parameter to an if. This won't work, try something the like. I am using a catchall fallback by
using the regular expression .*.

while { $transfer_flag } {
        expect -re ".*\? " {
                send "y\r"
        } -re ".*" {
                set transfer_flag 0
        }
}

Hope this helps
0
 

Author Comment

by:mafendee
ID: 6630724
its works! thanx
0

Featured Post

Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Attention: This article will no longer be maintained. If you have any questions, please feel free to mail me. jgh@FreeBSD.org Please see http://www.freebsd.org/doc/en_US.ISO8859-1/articles/freebsd-update-server/ for the updated article. It is avail…
Introduction Regular patching is part of a system administrator's tasks. However, many patches require that the system be in single-user mode before they can be installed. A cluster patch in particular can take quite a while to apply if the machine…
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
Learn how to navigate the file tree with the shell. Use pwd to print the current working directory: Use ls to list a directory's contents: Use cd to change to a new directory: Use wildcards instead of typing out long directory names: Use ../ to move…
Suggested Courses
Course of the Month11 days, 16 hours left to enroll

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question