PDA

View Full Version : Passing arguments to an Expect script as variables



piers
July 18th, 2007, 05:06 PM
Does anyone know how I can run a script with arguments and then use those arguments as variables in an Expect script?

So i would run $ myscript.exp thisuser thispass and then myscript.exp will be able to use thisuser and thispass as variables.

All the docs I can find for expect are really really old, does anybody else still use it?

fabianp
August 16th, 2008, 02:46 PM
>So i would run $ myscript.exp thisuser thispass and then >myscript.exp will be able to use thisuser and thispass as >variables.

You need to use lindex, try

set arg1 [lindex $argv 0]
set arg2 [lindex $argv 1]

send $arg1\n
send $arg2\n

I hope this helps,

--
Fabian

miahfost
June 18th, 2009, 10:30 PM
That helps me Fabian - thanks!

mikeabout
November 22nd, 2009, 02:34 AM
Thank you so much!!! Worked perfectly!

SterlingCamden
July 18th, 2010, 07:52 PM
Thanks, this was exactly what I was looking for!

sg552sg552
June 22nd, 2011, 06:59 AM
Thanks a lot! works for me!!!!
(Expect is really powerful .... it's not only a simple command.... )

lingeshram
April 12th, 2013, 01:28 PM
From your tutorial i write a script for copy scp files between two servers by passing password as an arguments thanks a lot







first install expect

sudo apt-get install expect



then create copyremotefiles.sh




#!/usr/bin/expect -f

set from [lindex $argv 0]
set to [lindex $argv 1]
set pass [lindex $argv 2]

# connect via scp
spawn scp -r $from $to
#######################
expect {
-re ".*es.*o.*" {
exp_send "yes\r"
exp_continue
}
-re ".*sword.*" {
exp_send $pass\n
}
}
interact

Then change file permission

chmod 755 copyremotefiles.sh

now run

./copyremotefiles.sh /path/myfile.txt root@45.65.89.22:/home/lingesh/ PASSWORD_OF_REMOTE_SERVER


PASSWORD_OF_REMOTE_SERVER is root password of 45.65.89.22

Enjoy.....

oldos2er
April 13th, 2013, 04:22 PM
Old thread closed.