Expect script idles for a long time

the following code works sometimes. other times, it behaves mysteriously. when the script sshs to a box, it is suppose to automatically begin running the command it is told to run. but in this case, after this script logs into a host, it just sits there at the prompt and does not run the command.

i've changed the timeout to "set timeout -1" but that didn't help. any ideas?

                                        export nserver=$(echo ${line})
                                        export username="${USERSNAME}"
                                        export usepas="$NUPSSWD"
                                        export remotecom="${script}"
                                        export specialcomm=">> /tmp/loggs.txt"
                                        #spawn ssh $env(nserver) -l $env(username)

                                        /usr/bin/expect -c '

                                        set timeout 65

                                        spawn ssh $env(username)@$env(nserver)

                                        expect {
                                        "*password*" {
                                                send "$env(usepas)\r"
                                        }
                                        "*yes/no*" {
                                                send "yes\r" ; exp_continue
                                        }
                                        "*password:*" {
                                                send "$env(usepas)\r"
                                        } }

                                        set prompt "(%|#|$|\\]\\$|\\$|\\$|%]) $"

                                        expect -re $prompt
                                                send -- "sudo -l \r"
                                        expect -re $prompt
                                                send -- "$env(remotecom) ; exit\r"
                                        expect -re $prompt
                                                send -- "exit \r"

                                        expect {
                                        "*Permanently added*" {
                                                exp_continue
                                        }
                                        "assword:" {
                                                send "$env(usepas)\r"
                                        } } '

                                        /usr/bin/expect -c '

                                        set timeout 65

                                        spawn ssh $env(username)@$env(nserver) $env(specialcomm)

                                        expect {

                                        "*password*" {
                                                send "$env(usepas)\r"
                                        }
                                        "*yes/no*" {
                                                send "yes\r" ; exp_continue
                                        }
                                        "*password:*" {
                                                send "$env(usepas)\r"
                                        } }'

Is the problem reproducible? Meaning: serverQ always hangs?

What network? An internal LAN or a WAN or the internet? For example, on our comapany-wide network we have points of outage that cause ssh packets to get re-routed, which then causes A man-in-the-middle-attack prompt.

You need to investigate every hang and then code for all possibilities.

1 Like