Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Passing command line argument to an Expect script

3,020 views
Skip to first unread message

contracer

unread,
Apr 18, 2013, 4:48:16 PM4/18/13
to
Hi,
Please, I need send an argument to a expect script like below:

>> ./ip_address 200.244.20.10

Where ip_address script haves an Expect script like below:

/usr/bin/expect<<EOD
log_file -noappend /tmp/sftp.log
spawn sftp sw...@200.191.200.10
expect "ip_address:"
send "ip_address\r"
expect "sftp>"
sleep 1
send "bye\r"
EOD

Janis Papanagnou

unread,
Apr 18, 2013, 4:59:21 PM4/18/13
to
On 18.04.2013 22:48, contracer wrote:
> Hi,
> Please, I need send an argument to a expect script like below:
>
>>> ./ip_address 200.244.20.10
>
> Where ip_address script haves an Expect script like below:

Let the shell expand a parameter in the here-doc...

>
> /usr/bin/expect<<EOD
> log_file -noappend /tmp/sftp.log
> spawn sftp sw...@200.191.200.10

spawn sftp sword@${1}

Kenny McCormack

unread,
Apr 18, 2013, 6:00:15 PM4/18/13
to
In article <d4e43406-0f18-458e...@y12g2000yqb.googlegroups.com>,
The sensible way to do this is like this:

#!/usr/bin/expect --
log_file -noappend /tmp/sftp.log
spawn sftp sw...@200.191.200.10
expect "ip_address:"
send "[lindex $argv 0]\r"
expect "sftp>"
sleep 1
send "bye\r"

--
They say compassion is a virtue, but I don't have the time!

- David Byrne -

contracer

unread,
Apr 18, 2013, 6:47:35 PM4/18/13
to
On 18 abr, 19:00, gaze...@shell.xmission.com (Kenny McCormack) wrote:
> In article <d4e43406-0f18-458e-ab21-895881a2e...@y12g2000yqb.googlegroups.com>,
Thanks a lot.
How could I pass a file name to the expect script ?


for FILE in `sftp_func | awk '{print $9}'`
do
/usr/bin/expect<<EOD
log_file -noappend /tmp/sftp2.log
spawn sw...@200.192.100.10
expect "sw...@200.192.100.10's password:"
sleep 1
send "alastair\r"
expect "sftp>"
sleep 1
send "cd alsat1\r"
expect "sftp>"
send "get $FILE\r"
sleep 1

Bit Twister

unread,
Apr 18, 2013, 7:08:55 PM4/18/13
to
On Thu, 18 Apr 2013 13:48:16 -0700 (PDT), contracer wrote:
> Hi,
> Please, I need send an argument to a expect script like below:
>
>>> ./ip_address 200.244.20.10

Set a variable name and then use the variable with a preceding $.
Some short examples:

set arg1 [lindex $argv 0]
spawn $arg1
send -- "'$arg1'\r"

Bit Twister

unread,
Apr 18, 2013, 7:15:28 PM4/18/13
to
Forgot to mention those commands are in an expect script.

contracer

unread,
Apr 18, 2013, 7:25:44 PM4/18/13
to
Could you please put these commands in the example´s script ?

Bit Twister

unread,
Apr 18, 2013, 9:27:24 PM4/18/13
to
On Thu, 18 Apr 2013 16:25:44 -0700 (PDT), contracer wrote:
> Could you please put these commands in the example´s script ?
-----8<-----8<-----8<--- cut below this line ----8<-----8<----
#!/usr/bin/expect -f
#*********************************************
#* ip_address - send ip address
#*
#* Usage: ./ip_address x.x.x.x
#* Example: ./ip_address 200.244.20.10
#*
#*********************************************
log_file -noappend /tmp/sftp.log
set ip_addy [lindex $argv 0]
spawn sftp sword@$ip_addy
expect "ip_address:"
send "$ip_addy\r"
expect "sftp>"
sleep 1
send "bye\r"
#*********************** end ip_address *********************

contracer

unread,
Apr 18, 2013, 9:39:26 PM4/18/13
to
Thanks a lot for your assistance.
I´ll test this script tomorrow.
0 new messages