Multiple expect/send statements not working in shell script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Multiple expect/send statements not working in shell script
# 1  
Old 02-12-2018
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

Code:
#!/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

Code:
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

Code:
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


Last edited by skorada; 02-12-2018 at 10:11 AM.. Reason: added debug
# 2  
Old 02-12-2018
I think expect is sending the IP address the 2nd time but then is closing before the echo comes back you could try:

Code:
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:

Code:
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.

Code:
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
'

# 3  
Old 02-13-2018
Thanks Chubler_XL

Code:
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.Smilie

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

Code:
expect -re {[#>$] }

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

To send ID and Password for each command using expect feature in bash script

Dear Tech Guys, I am trying to send some commands on the local server and it always asks for user name and password after each command. To serve the purpose I am using expect function as follows: #!/usr/bin/expect set timeout 20 spawn "./data1.sh" expect "Please Enter UserName: "... (6 Replies)
Discussion started by: Xtreme
6 Replies

2. Shell Programming and Scripting

Expect script error : send: spawn id exp4 not open

Hi, I am executing a expect script in while loop for doing telnet on list of servers stored in file as below : expect script : #!/usr/bin/expect -f set timeout 20 set ip set port if { == 0} { send_user "Usage: scriptname ip port\n" exit 1 } #exp_internal 1 log_user 0 spawn... (3 Replies)
Discussion started by: omkar.jadhav
3 Replies

3. Shell Programming and Scripting

Bash Script: Send files to SFTP using Expect

I have to send few gzipped files from local server to SFTP server. My Server Info Distributor ID: Ubuntu Description: Ubuntu 12.04.4 LTS Release: 12.04 Codename: precise Created a bash script and could able to send files to sftp, but i want to send email if transfer is successful. ... (1 Reply)
Discussion started by: krux_rap
1 Replies

4. Shell Programming and Scripting

expect script inside shell script not working.

Shell Scipt: temp.sh su - <$username> expect pass.exp Expect script: pass.exp #!/usr/bin/expect -f # Login ####################### expect "Password: " send "<$password>\r" it comes up with Password: but doesnt take password passed throguh file. (2 Replies)
Discussion started by: bhavesh.sapra
2 Replies

5. AIX

Send ctrl+C in expect script

Hi, Am trying to transfer file via FTP using expect script from server to client i need to interrupt the file transfer between server and client Please help what should used in expect code.. I used send "ctrl+c\r" expect "Aborted" but that didnt work.. I need what should... (3 Replies)
Discussion started by: Priya Amaresh
3 Replies

6. Shell Programming and Scripting

ssh is not working while calling through expect shell script

Hi, Please share you experience and way out on below error:--> #!/bin/bash -xv FILE=login.txt + FILE=login.txt CONNECT=sshlogin.exp + CONNECT=sshlogin.exp SERVERNAME=$1 + SERVERNAME=192.168.12.1 MyServer="" + MyServer= MyUser="" + MyUser= MyPassword="" + MyPassword= exec 3<&0 +... (6 Replies)
Discussion started by: manish_1678
6 Replies

7. Shell Programming and Scripting

Multiple if statements in script and unexpected end of file

Two things... 1) If I have an if statement that is true I want it to exit without running the rest of the file, but if it is false I want it to continue.. I just did if ; then exit else echo "" fi Can that work? I want it to exit if $1 is 0, and if not to continue running the rest of the... (1 Reply)
Discussion started by: glev2005
1 Replies

8. Shell Programming and Scripting

within shell script send expect and if else

Hi, I have written one shell script , using that i am able to connect to remote machine but i have to #!/usr/bin/expect -f set address set username set password set OOLpath set dbusername set dbpasswd set tnsname set recdbusername set recdbpasswd set rectnsname spawn ssh... (2 Replies)
Discussion started by: mnmonu
2 Replies

9. Shell Programming and Scripting

within shell script send expect and if else

Hi I have written one shell script , using that i am able to connect to remote machine but i have to #!/usr/bin/expect -f set address set username set password set OOLpath set dbusername set dbpasswd set tnsname set recdbusername set recdbpasswd set rectnsname spawn ssh... (1 Reply)
Discussion started by: mnmonu
1 Replies

10. Shell Programming and Scripting

how to send multiple files from the shell script

hi, how to send multiple files from the shell script eg : i have /home/adm/file1 /home/adm/file2 /home/adm/cfg how can i attach these files in the mail ? (1 Reply)
Discussion started by: mail2sant
1 Replies
Login or Register to Ask a Question