Search code examples
bashexpect

How to use export variable from bash script in expect script?


I have this script which is a combination of bash and expect script.

#!/bin/bash
#!/usr/bin/expect

export TART="4 5 8 9"

for i in "$@"; do
        echo "Tart #: $i"

        case "$i" in

                1)
                IP=10.171.0.10
                ;;
                2)
                IP=10.171.0.11
                ;;
                3)
                IP=10.171.0.12
                ;;
                4)
                IP=10.171.0.13
                ;;
                5)
                IP=10.171.0.14
                ;;
                8)
                IP=10.171.0.17
                ;;
                9)
                IP=10.171.0.18
                ;;
                *)
                echo "Invalid TARTS '$i'" >&2;
                exit 1
                ;;
        esac

        echo "    ----  Launching Tart $i  ----  "
                sshpass -p "tart123" ssh -Y -X -L 5900:$IP:5901 tarts@$IP <<EOF
                  export DISPLAY=:1
                gnome-terminal -e "bash -c \"pwd; cd /home/tarts; pwd; ./launch_tarts.sh exec bash\""
                exit
EOF
done

/usr/bin/expect << 'EOF'

set tart_num $env(TART)

puts "Tarts to be updated: $tart_num"

for { set index 0 } { $index < [llength $tart_num] } { incr index } {
 puts "In Loop: $index"
 puts "Tart Num: [lindex $tart_num $index]"

switch -- [lindex $tart_num $index]\
4 {
        spawn telnet 10.171.0.13 6187
        set timeout -1
} 5 {
        spawn telnet 10.171.0.14 6187
        set timeout -1
} 8 {
        spawn telnet 10.171.0.17 6187
        set timeout -1
} 9 {
        spawn telnet 10.171.0.18 6187
        set timeout -1
}
        expect {
                "*traffic*" {
                puts "``````````````````````````````````````````````````````````````"
                puts "Registering Group1, Group2"
                send "traffic map rate reg_group1 0\r"
                send "traffic map rate reg_group2 0\r"

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

                puts "wait 3 seconds"
                sleep 3

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

Can someone please guide me how can I use the export variable such that I can use any of the case from the variable. Right now when I run the script it starts working on all the cases like "4 5 8 9" but I want to be able to use any one of the values. Can someone please guide?


Solution

  • Consider this rewrite:

    #!/bin/bash
    #!/usr/bin/expect
    
    ips=(
        [1]=10.171.0.10
        [2]=10.171.0.11
        [3]=10.171.0.12
        [4]=10.171.0.13
        [5]=10.171.0.14
        [8]=10.171.0.17
        [9]=10.171.0.18
    )
    export TART="4 5 8 9"
    
    ip=()
    for i in $TART; do
        if [[ ! -v ips[i] ]]; then
            echo "Invalid TARTS '$i'" >&2;
            exit 1
        fi
        ip+=(${ips[i]})
    
        echo "    ----  Launching Tart $i  ----  "
        sshpass -p "tart123" ssh -Y -X -L "5900:${ips[i]}:5901" tarts@"${ips[i]}" <<EOF1
            export DISPLAY=:1
            gnome-terminal -e 'bash -c "pwd; cd /home/tarts; pwd; ./launch_tarts.sh exec bash"'
            exit
    EOF1
    
    done
    export IP="${ip[*]}"
    
    /usr/bin/expect << 'EOF2'
    
        set tarts [split $env(TART)]
        set ips   [split $env(IP)]
    
        puts "Tarts to be updated: $tarts"
    
        for {set index 0} {$index < [llength $tarts]} {incr index} {
            puts "In Loop: $index"
            set tart [lindex $tarts $index]
            puts "Tart Num: $tart"
            set ip [lindex $ips $index]
            puts "IP: $ip"
    
            set timeout -1
    
            spawn telnet $ip 6187
            expect {
                "*traffic*" {
                    puts "``````````````````````````````````````````````````````````````"
                    puts "Registering Group1, Group2"
                    send "traffic map rate reg_group1 0\r"
                    send "traffic map rate reg_group2 0\r"
    
                    puts "wait 2 seconds .."
                    send "traffic go\r"
                    sleep 2
    
                    puts "wait 3 seconds"
                    sleep 3
    
                    send -- "^]"
                    expect -exact "^\]\rtelnet> "
                    send -- "close\r"
                    expect eof
                }
            }
        }
    EOF2