how to use spawn command in TCL
I am using the following code
#!/usr/bin/expect -f
#!usr/bin/expect
#package require Expect
puts "Hello world"
spawn ssh xyz@172.31.112.182
expect -nocase "password:"
send "abc123\r"
puts "done"
开发者_如何学运维
while executing, it throws error
Hello world
invalid command name "spawn"
while executing
"spawn ssh xyz@172.31.112.182"
(file "temp.tcl" line 9)
whats the wrong in my code
remove '#' before package require Expect.
The problem you've got is that while it is running in Tcl (I recognize that format of trace), the Expect package (which provides the spawn
command) is absent for some reason. The first thing to do is to make the requirement for the Expect package explicit by uncommenting that package require
line. That may be enough to fix your problem in itself, but if not it will complain about the package not being available. If it's not available, that means either that it just isn't installed, or that it's not being found. The former is... obvious to fix. :-) The latter is resolved by putting a line like this before that package require
:
lappend auto_path /full/path/to/Expect/package/installation
Note that if you run the script with the expect program instead of the tclsh program, that package require
will be done for you automatically. You're obviously not doing that…
Try running under tcl interpreter (!/usr/bin/tcl) and import Expect.
Did you installed Expect on your PC?
Please run the following command on your PC to check if Expect is available.
$which expect
/usr/bin/expect
- sudo apt-get update -y
- sudo apt-get install -y expect
- which expect
Run the above commands respectively. If installed correctly, "which expect" will show "/usr/bin/expect" as an output. Run your script after installation,
精彩评论