Multiple expect/send statements not working in shell script

Hi I am trying the following in my bash script which logs into my machine and runs a command. Trying to solve this using expect.

The first expect statement is hit and it enters the address "10.10.0.10" but when the second expect statement is hit it exits

#!/bin/bash

expect -c '
spawn sshpass -p "password" ssh -o StrictHostKeyChecking=no user@100.10.100.100 "source .profile && add_ip"

expect "IP Address:"
send "10.10.10.10\r"
expect "Enter IP again:"
send "10.10.10.10\r"
'

Output: ./test.sh

spawn sshpass -p password ssh -o StrictHostKeyChecking=no user@100.10.100.100 source .profile && add_ip
IP Address: 10.10.10.10
Enter IP again:  ====> EXITS here without entering the IP

This doesnt enter the IP again like expected. Any ideas here?
I would like to do this within my bash script and not write a 2nd expect script.Would appreciate any inputs

I added debug to check

expect version 5.45
argv[0] = /usr/bin/expect  argv[1] = -d
set argc 0
set argv0 "/usr/bin/expect"
set argv ""
executing commands from command file
spawn sshpass -p password ssh -o StrictHostKeyChecking=no user@100.10.100.100 source .profile && add_ip
parent: waiting for sync byte
parent: telling child to go ahead
parent: now unsynchronized from child
spawn: returns {27493}

expect: does "" (spawn_id exp4) match glob pattern "IP Address:"? no
IP Address:
expect: does "IP Address: " (spawn_id exp4) match glob pattern "IP Address:"? yes
expect: set expect_out(0,string) "IP Address:"
expect: set expect_out(spawn_id) "exp4"
expect: set expect_out(buffer) "IP Address:"
send: sending "10.10.10.10\r" to { exp4 }

expect: does " " (spawn_id exp4) match glob pattern "Enter address again to confirm:"? no
10.10.10.10

expect: does " 10.10.10.10\r\n" (spawn_id exp4) match glob pattern "Enter IP again:"? no
Enter address again to confirm:
expect: does " 10.10.10.10\r\nEnter IP again: " (spawn_id exp4) match glob pattern "Enter IP again:"? yes
expect: set expect_out(0,string) "Enter IP again:"
expect: set expect_out(spawn_id) "exp4"
expect: set expect_out(buffer) " 10.10.10.10\r\nEnter IP again:"
send: sending "10.10.10.10\r" to { exp4 } . ===> Seems to be sending it here

I think expect is sending the IP address the 2nd time but then is closing before the echo comes back you could try:

expect "10.10.10.10"

at the end of your script.

However the best solution is to use public key authentication. This avoids having your password sitting in plain text within scripts and having to update your script every time you change your password. Your script would then simply be:

printf "%s\n" 10.10.10.10 10.10.10.10 | ssh user@100.10.100.100 "source .profile && add_ip"

If you insist in using expect and sshpass please use the SSHPASS environment variable as this stops others on your box spotting the password in the ps listings etc.

expect -c '
set ::env(SSHPASS) "password"
spawn sshpass -e ssh -o StrictHostKeyChecking=no user@100.10.100.100 "source .profile && add_ip"

expect "IP Address:"
send "10.10.10.10\r"
expect "Enter IP again:"
send "10.10.10.10\r"
expect "10.10.10.10
'

Thanks Chubler_XL

printf "%s\n" 10.10.10.10 10.10.10.10 | ssh user@100.10.100.100 "source .profile && add_ip"

The one suggested by you was perfect.:slight_smile:

Also I later figured what the issue was with my original script. I had to the following to my expect code and it worked

expect -re {[#>$] }