Similar Threads:
1.How to use a variable in expect body which embedded in a shell script
Hi all,
My question is how to use expect in a shell script. For example, here
is a little program, it just telnet to some host, and login root
automatically.
#!/bin/bash
echo "This is to test how to call expect in a shell"
#hostname=10.0.0.1
expect -c '
set timeout 15
spawn telnet 10.0.0.1
expect "login:" { send "root\r" }
expect "assword:" { send "abcd1234\r" }
interact
'
Can I use variable in the body of expect? Such as the 10.0.0.1 is a
variation passed to the shell scripts, like the following, actually
this is wrong, expect can't handle $hostname
#!/bin/bash
echo "This is to test how to call expect in a shell"
hostname=10.0.0.1
expect -c '
set timeout 15
spawn telnet $hostname
expect "login:" { send "root\r" }
expect "assword:" { send "abcd1234\r" }
interact
'
it will issue the following error.
[tina@kgardenia test]$ ./test.expect
This is to test how to call expect in a shell
can't read "hostname": no such variable
while executing
"spawn telnet $hostname"
Does anybody know how to use a variable in the body of the expect
code, which is embedded in a bash?
Many thanks to you all :D
2.expect script with shell script
I am using expect script in shell script.
I want one value from expect script.
how can i get the value from expect script and use it in shell script?
3.Expect : Interact returns to shell instead of script
I'm new to tcl and expect, and am trying to do something very simple,
run a few commands by script, then interact, then run some more
commands. When i'm interacting, andI enter the '+' key expecting to
return to my script, I get bounced out of the script and get a new
shell prompt. "Hello World 2" never gets printed.
I've wondered if something in my shell set up is getting in the way,
and to that end I removed my .profile so I get a vanilla shell
environment. (I use vi command line editing with set -o vi and
thought that was a problem, but ....)
expect version 5.42.0
Running on MacOS v 10.4.11 inside "Terminal" application/ bash shell
#!/usr/bin/expect -f
set timeout -1
spawn $env(SHELL)
match_max 100000
send "echo Hello World 1\r"
expect "*$"
interact "+" return
send "echo Hello World 2\r"
expect "*$"
4.shell command as argument to expect script
Here is the scenario which is not working for me.
1. I have a shell script in BASH, which constructs a shell variable
containing some command e.g.
var=cd /usr/local/home/user/server/config; grep -i propname
properties.file
2. I called an expect script from this shell script. The purpose of
this expect script is to SSH to remote server and execute the command
mentioned in the variable $var.
3. In the Bash Script, I call the expect script in following way
expect.exp $host_name $user $password $var
4. In the Expect script, I read these command line arguments in
following way:
set host_name [lrange $argv 0 0]
set user [lrange $argv 1 1]
set password [lrange $argv 2 2]
set var [lrange $argv 3 end]
5. I spawn the ssh in expect in the following way:
spawn ssh $user@$host_name $var
Observations: It looks like that while interpreting the variable $var
(which contains the shell commands), it's appending the curly braces
and hence bash is not able to understand it. Here is the error message
I am getting:
spawn ssh XXXX@XXXXX.COM {cd /usr/local/site/
pvr/config/; grep -i jdbc ofoto.properties}
Password:
bash: {cd: command not found
grep: ofoto.properties}: No such file or directory
expect: spawn id exp7 not open
while executing
"expect "*""
(file "./ssh_prop.exp" line 36)
NOTE: Please see it's appending the curly braches while interpreating
the variable $var. Due to this bash is not able to understand and
throwing error.
Please help in this regard.
5.Problem with get exit status of shell script/command within expect
Hi, all
I'm caught in the problem with get exit status of shell script/command,
with ssh. For example:
% ssh hostname command
% echo $?
This will tell me the exit status of `command'.
The problem is that when using ssh, I need to do some interactive
thing.
Here I begin to do it with expect, like the following:
------------------------------------------------------------------------
#! /bin/bash
#
host=$1
shift
command=$@
expect_cmd=`cat << EOF
spawn ssh $host $command
set timeout -1
expect {
"*password:" { send "xxxxxxxx\r" }
"* (yes/no)?" { send "yes\r"; exp_continue }
eof exit
}
EOF`
expect -c "$expect_cmd"
echo $?
------------------------------------------------------------------------
But here I will get the exit status of `expect', not `command'.
So, how I can solve this problem?
Any help is appreciated.
Thanks in advance.
6. How to run the expect script using TCL oe expect 5.21
7. Importing variables from Main Expect Body to Procedures
8. Passing shell variable as command line argument to expect