How to Pass Argument in Expect Through the Command Line in a Shell Script

How to pass argument in Expect through the command line in a shell script

If you want to read from arguments, you can achieve this simply by

set username [lindex $argv 0];
set password [lindex $argv 1];

And print it

send_user "$username $password"

That script will print

$ ./test.exp user1 pass1
user1 pass1

You can use Debug mode

$ ./test.exp -d user1 pass1

How to pass arguments to inline expect in a bash script?

Based on Kenavoz answer, I found the solution. As he stated, my code was encased by simple quotes, so expect couldn't get the variables from the main script. As such, putting it with only normal quotes solved the problem, without the need to pass the variable as an argument:

expect -c "set timeout -1
spawn sh install_pipeline
expect \"Where should I install the software packages ? *\"
send \"/usr/local/$package\r\"
expect \"Where should I install the pipeline calibration files ? *\"
send \"/usr/local/$package\r\"
expect eof"

How to pass the argument in expect shell script during runtime

The [read] command reads until end of file so it's waiting for you to close the terminal. Use the [gets] command instead:

set password [gets stdin]

Also, you're using [read] wrong. The first argument is the channel id to read from. See the documentation for more info:

http://www.tcl.tk/man/tcl8.6/TclCmd/read.htm

http://www.tcl.tk/man/tcl8.6/TclCmd/gets.htm

How to pass variables from a shell script to an expect script?

From your shell script:

/mypath/abc $gateway

From your expect script:

#!/usr/bin/expect

set gateway [lindex $argv 0]; # Grab the first command line parameter

set timeout 3
spawn ssh "james@$gateway"
expect "password:"
send "TSfdsHhtfs\r";
interact

How to use expect to spawn with arguments from command line?

You'd want to use tcl's argument expansion syntax to do this:

spawn {*}$argv

Example

my.exp:

#!/usr/bin/env expect
log_user 0
puts "Spawning '$argv'"
spawn {*}$argv
expect *foo* { puts "Got: '[string trim $expect_out(buffer)]'" }

Usage:

$ ./my.exp echo foo bar
Spawning 'echo foo bar'
Got: 'foo bar'

TCL/Expect equivalent of Bash $@ or how to pass arguments to spawned process in TCL/Expect

To literally translate your example

set program [lindex $argv 0]
set arguments [lrange $argv 1 end]
spawn $program {*}$arguments

{*} is Tcl's "list expansion" syntax (rule 5 of Tcl's 12 rules of syntax). It splits a list into its element in the current command.

If $argv is foo bar baz, then

spawn [lindex $argv 0] [lrange $argv 1 end]

will invoke foo with 1 argument: "bar baz"

spawn [lindex $argv 0] {*}[lrange $argv 1 end]

will invoke foo with 2 arguments: "bar" and "baz"


Tangentially, I would code your lshift proc like this:

proc lshift {varname} {      
upvar 1 $varname var
set var [lassign $var first]
return $first
}

Then:

expect1.6> set argv {foo bar baz}
foo bar baz
expect1.7> set script [lshift argv]
foo
expect1.8> set script
foo
expect1.9> set argv
bar baz

spawn: How to pass arguments to spawn within an expect script?

set args [lrange $argv 1 end]
spawn ssh {*}$args
expect -re {The authenticity of .* can't be established.} {
send "yes"
}

interact

Inside "" variables and square brakets [] (evaluate command inside brackets and place result instead), will be expanded and evaluated. You need quote square brackets \[ or use {} instead of "".
BTW, you can supress asking this question (only warning showing) by openssh options:

ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no -p 9022 root@192.168.120.170


Related Topics



Leave a reply



Submit