I need to update configurations of several gateways...I have two files:

File 1 (ip.txt) : Holds gateways’ IP addresses ---> This files gets updated every month.

File 2 (cmd.txt): Holds commands to make config change on gateway ---> This also gets updated from time to time.

So far I have two scripts......

Script 1 i.e., script1.sh: basically script1 is reading Gateway IPs and passign it to expect script to login to the Gateway.

for device in `cat /home/ip.txt`;do
./step_3 $device;

Second Script

#!/bin/expect -f

set IP [lindex $argv 0]

spawn ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no user@$IP

expect "password: "

send "difficult123\r"

expect "GBU_0:"

send "su -\r"
expect "Password: "

send "verydifficult123\r"

expect "GBI_0:"

send "/17.1/bin/cli.exe\r"

expect "USERNAME : "

send "GOOR\r"

expect "PASSWORD : "

send "DIFFICULT123\r"

expect "] "
****************** POINT 1 - Only One Change command shown -- I have multiple commands here to insert-----


expect "(Y/N) :"

send "Y\r"

expect "]"
******************** POINT 2

send "exit\r"

expect "0:~> "

send "exit\r"

expect "logout"

send "exit\r"

expect "closed"

expect eof

If I hard code expect commands b/w Point 1 and 2 then everything works like a charm (I have multiple commands)... How can I call expect command b/w Point 1 and 2 from file ... i.e., I do not want to hard code the commands inside but rather in a separate file so user can update the commands file without touching the script and run it..... passing IP to expect script works great ... I am not sure if I can pass expect command from file as well.

closed as unclear what you're asking by Scott, Timothy Martin, Rui F Ribeiro, Stephen Rauch, Romeo Ninov Aug 15 '17 at 5:50

Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.


TCL can easily include and run commands from some other file; the source(n) command for example allows one to say

#!/usr/bin/env expect

package require Tcl 8.5

set IP [lindex $argv 0]
set include_file [lindex $argv 1]

# ... begin commands before here

catch {source $include_file} result options
if {[dict get $options -code] != 0} {
    puts stderr "could not source $include_file: $result"
    exit 1

# end commands after here ...

with this saved as runner one can then have an include file

$ cat runthese 
puts a
puts b
puts c
$ expect runner runthese

though of course the runthese or whatever file to include can instead have send and expect as necessary.

  • Thank you ....So.. i should add --> catch {source.......until exit 1 } part b/w point 1 and 2 in my second script and save it as a runner ....create another file runthese with send and expect commands..... and run script as "expect runner runthese" ....Is fixed IP ..In my case if i have 100 diff IPs and i would like to ssh to each and then run the runthese commands...Please elaborate on a bit or is that an actual file that has a list of IPs..... Thank you again so much for your time and it has been a great help.....Thanks – aavam Aug 15 '17 at 3:12
  • It worked beautifully.....in my first script i added runthese (like below) and added include file in second script.....and it worked like a charm *********** for device in cat /home/ip.txt;do ./step_3 $device runthese; done – aavam Aug 15 '17 at 13:58

Not the answer you're looking for? Browse other questions tagged or ask your own question.