0
#!/bin/expect --

set vm_num [lindex $argv 0]

puts "``````````````````````````````````````````````````````````````"
puts "``````````````````````````````````````````````````````````````"
puts "Registering Stations on VM"
puts "Tarts to be Registered: $vm_num"
puts "``````````````````````````````````````````````````````````````"
puts "``````````````````````````````````````````````````````````````"

for { set index 0 } { $index < [llength $vm_num] } { incr index } {

puts "Registering Tart Num: [lindex $vm_num $index]"

switch -- [lindex $vm_num $index]\
1 {
        spawn telnet 10.171.0.10 6187
        set timeout -1
        expect {
                puts "``````````````````````````````````````````````````````````````"
                "*traffic*" { puts "Registering Group1, Group2"
                send "traffic map rate reg_group1 30000\r"
                send "traffic map rate reg_group2 30000\r"

                puts "wait 100 seconds .."
                send "traffic go\r"
                sleep 100

                puts "Registering Group3, Group4"
                send "traffic map rate reg_group3 30000\r"
                send "traffic map rate reg_group4 30000\r"
                puts "wait 100 seconds .."
                send "traffic go\r"
                sleep 100

                puts "Registering Group5, Group6"
                send "traffic map rate reg_group5 30000\r"
                send "traffic map rate reg_group6 30000\r"
                puts "wait 100 seconds .."
                send "traffic go\r"
                sleep 100

                puts "Registering Group7, Group8"
                send "traffic map rate reg_group7 30000\r"
                send "traffic map rate reg_group8 30000\r"
                puts "wait 100 seconds .."
                send "traffic go\r"
                sleep 100

                }


                puts "wait 3 seconds"
                sleep 3

                send -- "^]"
                expect -exact "^\]\rtelnet> "
                send -- "close\r"
                expect eof
                wait
        }

} 2 {
        spawn telnet 10.171.0.11 6187
        set timeout -1
        expect {
                "*traffic*" {
                puts "``````````````````````````````````````````````````````````````"
                puts "Registering Group1, Group2"
                send "traffic map rate reg_group1 30000\r"
                send "traffic map rate reg_group2 30000\r"

                puts "wait 100 seconds .."
                send "traffic go\r"
                sleep 100

                puts "Registering Group3, Group4"
                send "traffic map rate reg_group3 30000\r"
                send "traffic map rate reg_group4 30000\r"
                puts "wait 100 seconds .."
                send "traffic go\r"
                sleep 100

                puts "Registering Group5, Group6"
                send "traffic map rate reg_group5 30000\r"
                send "traffic map rate reg_group6 30000\r"
                puts "wait 100 seconds .."
                send "traffic go\r"
                sleep 100

                puts "Registering Group7, Group8"
                send "traffic map rate reg_group7 30000\r"
                send "traffic map rate reg_group8 30000\r"
                puts "wait 100 seconds .."
                send "traffic go\r"
                sleep 100

                }

                puts "wait 3 seconds"
                sleep 3


                send -- "^]"
                expect -exact "^\]\rtelnet> "
                send -- "close\r"
                expect eof
                wait
        }
} 3 {
        spawn telnet 10.171.0.12 6187
        set timeout -1
        expect {
                "*traffic*" {
                puts "``````````````````````````````````````````````````````````````"
                puts "Registering Group1, Group2"
                send "traffic map rate reg_group1 50000\r"
                puts "wait 220 seconds .."
                send "traffic go\r"
                sleep 220

                puts "Registering Group2"
                send "traffic map rate reg_group2 50000\r"
                puts "wait 220 seconds .."
                send "traffic go\r"
                sleep 220

                }

                puts "wait 3 seconds"
                sleep 3

                send -- "^]"
                expect -exact "^\]\rtelnet> "
                send -- "close\r"
                expect eof
                wait
        }
}
default {
puts "$vm_num is not a valid tart"
}
}

Can someone please guide me I have the above expect script which is executed when we run the script for instance ./vmSetup.ex "1 2 3" but this is executed sequentially. I had created this expect script but now I want to run the cases in the script i.e. 1, 2 and 3 in parallel. Can someone also tell me how to run for instance 8 cases in parallel? Futhermore I am also using similar cases from 1 to 3 the only difference is the IPs. I have 25 such cases. Can someone also tell me how can I use just one case for all the 25 VMs of them?

1

1 Answer 1

3

The easiest way would be to rewrite your except script so that it connects to only one machine, getting the machine name or IP address from the command line.

Then run that script with either GNU Parallel or with xargs -P. xargs is easier to use for simpler parallelisation tasks, parallel is more flexible and better suited to more complex parallelisation requirements.

e.g.

echo 10.171.0.{10..17} | xargs -P 8 -n 1 /path/to/your/expect/script

This will run up to 8 jobs at once (-P 8), each job given exactly one (-n 1) of the IP addresses.

If your expect scripts produce copious output, I recommend writing the script so that they don't print to stdout but to their own individual log file (e.g. use the IP address expect-10.171.0.10.log), otherwise the output from all jobs will be intermingled.

At the very least, each output line should identify which machine it was coming from, e.g. by prefixing each line with the IP address and a colon.


Extra comments:

  1. If it is at all possible, you should use ssh instead of telnet, and configure it to use key based authentication rather than password auth, then:

    • you wouldn't need expect
    • you could use Parallel Distributed Shell pdsh to connect to the remote machines (pdsh connnects in parallel by default)

    On second thoughts, after looking more closely at your expect script, ssh probably isn't appropriate.

  2. Alternatively, it looks like you're maybe trying to gather data from some routers?

    In that case, SNMP may be more appropriate - and there are already many existing tools to collect, store, and graph data collected via SNMP (and other sources). For example, cacti, cricket, munin and other tools that use RRDtool.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .