0

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 in a single bash script.

I added a debug statement and here is the output

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 IP again:"? no
10.10.10.10

expect: does " 10.10.10.10\r\n" (spawn_id exp4) match glob pattern 
"Enter IP again:"? no
Enter IP again:
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 }
skorada
  • 431
  • 1
  • 4
  • 15
  • You cannot nest single quotes like that. The string `expect -c 'foo'bar'baz'` passes the quoted strings `foo` and `baz` and an unquoted `bar` between them. Similarly `'foo'"bar"'baz'` passes `foo` (in single quotes) followed by `bar` (in double quotes) followed by `baz` (in single quotes) as a single string, but without any quotes by the time the shell is done with it. – tripleee Feb 12 '18 at 09:26
  • I have now removed the nested single quotes and face the same issue? Maybe I am doing something wrong - Edited the question – skorada Feb 12 '18 at 12:01
  • Are you sure you need `expect` here at all? It looks like you could simply pipe the expected strings directly to `add_ip`: `sshpass -p "password" ssh -o StrictHostKeyChecking=no user@100.10.100.100 "source .profile && printf '%s\n' 10.10.10.10 10.10.10.10 | add_ip"` – chepner Feb 12 '18 at 16:03
  • @chepner this is an even better solution. Thanks a lot ! – skorada Feb 13 '18 at 07:35

0 Answers0