How to use wait inside bash script

This is the code snippet that I use in the below bash script:

for user_input in `awk '{print}' testfile_$$.txt` do ipaddress=`echo $user_input | cut -d';' -f 1` command="${config_mode}`echo $user_input | cut -d';' -f 2-`" ping -w 1 $ipaddress 1> /dev/null 2> $ERR_LOG_FILE 1> $LOG_FILE if [ $? -eq 0 ];then ssh " $USERNAME@ $ipaddress" "$command" >> $LOG_FILE fi done 

how can i use to automate ssh login in this script.

I am very new to waiting and started testing this (it failed):

 #!/usr/bin/bash set force_conservative 0 ;# set to 1 to force conservative mode even if ;# script wasn't run conservatively originally if {$force_conservative} { set send_slow {1 .1} proc send {ignore arg} { sleep .1 exp_send -s -- $arg } } # set timeout -1 spawn ssh auto21@10.38.227.229 {uname -a; df -h} match_max 100000 expect "*?assword: " send -- "bar01\r" expect eof 

I need to write a bash script again, waiting for the script, or expect it to be used inside the bash script if this can be done:

more i need to get the variable bash $ command , $ username , $ password , $ ipaddress and use it in the expected part

what solution would you suggest? or can I create the expected script and call it from a bash script only for login, error handling, execution, logfiles

+4
source share
1 answer

you will need to run two separate shell scripts, which causes the script to wait

 #!/usr/bin/bash set force_conservative 0 ; 

change the value above to

 #!/usr/bin/expect set force_conservative 0 ; 

or alternatively in your shell script I'm not sure about the format, but you can send expect -c with a command to execute:

 expect -c "send \"hello\n\"" -c "expect \"#\"" expect -c "send \"hello\n\"; expect \"#\"" 

There is actually another alternative

 #!/bin/bash echo "shell script" /usr/bin/expect<<EOF set force_conservative 0 ;# set to 1 to force conservative mode even if ;# script wasn't run conservatively originally if {$force_conservative} { set send_slow {1 .1} proc send {ignore arg} { sleep .1 exp_send -s -- $arg } } # set timeout -1 spawn ssh auto21@10.38.227.229 {uname -a; df -h} match_max 100000 expect "*?assword: " send -- "bar01\r" expect eof EOF 
+7
source

All Articles