Expect - bash and variables

I was wondering if anyone could provide some assistance. I trying to run an expect script within bash and get the results of a variable called RESULT. I Have tried a few things but none of them have worked. I know that the child process (the expect script) in this instance cannot set a variable for the parent. (bash) and the value must be return by the expect script, that is my problem.

my script

#!/bin/bash
#script for email validation
emaillist=emaillist1.txt
#emaillist=client.csv
SMTPport=25

clear
echo "---------------------------------------------------------------------"
echo "Now performing email validations.......... Please be patient."
echo "---------------------------------------------------------------------"
echo ""
echo ""

header="%-20s %-20s %-10s %-20s %10s\n"
body="%-20s %-20s %-10s %-20s %10s\n"


printf "$header" "E-mail" "Domain" "F-Name" "L-Name" "Status"
printf "$header" "_______________" "____________________" "__________" "____________________" "____________________"


while read line; do
	userinfo=$(echo $line | sed 's/@/,/')
	IFS=',' read -a address <<< "$userinfo"
	userid="${address[0]}"
	domain="${address[1]}"
	fname="${address[2]}"
	lname="${address[3]}"
	#status=vvv
	SMTPlist=$(host $domain | grep "handled by" | awk '{print $7}' | sed 's/\.$//' )

	/usr/bin/expect << EOF
	set timeout 1
	#set echo on
	#log_user 0
	
	spawn telnet ${address[4]} $SMTPport
	expect "220"

	send "helo $domain\r"
	expect "service"

	send "mail from: <mickeymouse@disney.com>\r"
	expect "250"

	send "rcpt to: <$userid@$domain>\r"
	expect {
		"250" 	{
			set result "SUCCESSFUL"
			set result $expect_out(buffer)
			puts "\n$userid@$domain validation $result\n"
				}
		"230" {send_user "ACCESS denied validation UNSUCCESSFUL\n"}
		"550" {send_user "$userid@$domain validation UNSUCCESSFUL\n"}		
		"553" {send_user "Requested action nottaken validation UNSUCCESSFUL\n"}		
	}
	
	send "quit"
	expect "closed"
EOF	

	printf "$body" "$userid" "$domain" "$fname" "$lname" "$status"
 done < $emaillist 

any assistance is appreciated. thank you.

Its pretty easy to do. This is how I set the current time in my backup script.

export CURR_TIME=`/bin/date +%Y%m%d_%H%M%S`

You just need something like:

export MY_RESULTS=`./myscript.sh param1 param2...`

Then the results of one script will be assigned to a variable in another script.

is this using the expect interpreter?

Try using:

   result=$(/usr/bin/expect << EOF
	set timeout 1
	#set echo on
	#log_user 0
	
	spawn telnet ${address[4]} $SMTPport

        ...

EOF
)
    printf "$body" "$userid" "$domain" "$fname" "$lname" "$status"

Now, within the expect script use puts -nonewline to print the value for $result in the parent script like this:

set result $expect_out(buffer)
puts -nonewline "$result"

To output messages or errors to the terminal use puts stderr like this:

puts stderr "ACCESS denied validation UNSUCCESSFUL"
puts -nonewline "FAILED"

The above prints the error message on the terminal and sets $result to "FAILED"

Chubler_XL, thank you for the guidance.

I have previously tried something like that, and was still unable to set the variable. I am not sure where you place following.

puts stderr "ACCESS denied validation UNSUCCESSFUL"
puts -nonewline "FAILED"

was this on the parent bash script or the child expect script?

I have had similar result. I can print variable with the child, however the parent variable is blank.

here is what i have so far.

	#!/bin/bash
#script for email validation
emaillist=emaillist1.txt
#emaillist=client.csv
SMTPport=25

clear
echo "---------------------------------------------------------------------"
echo "Now performing email validations.......... Please be patient."
echo "---------------------------------------------------------------------"
echo ""
echo ""

header="%-20s %-20s %-10s %-20s %10s\n"
body="%-20s %-20s %-10s %-20s %10s\n"


printf "$header" "E-mail" "Domain" "F-Name" "L-Name" "Status"
printf "$header" "_______________" "____________________" "__________" "____________________" "____________________"


while read line; do
	userinfo=$(echo $line | sed 's/@/,/')
	IFS=',' read -a address <<< "$userinfo"
	userid="${address[0]}"
	domain="${address[1]}"
	fname="${address[2]}"
	lname="${address[3]}"
	#status=vvv
	SMTPlist=$(host $domain | grep "handled by" | awk '{print $7}' | sed 's/\.$//' )
	address=("${address[@]}" $SMTPlist)
	
	status=$(/usr/bin/expect << EOF
	#exp_internal 1
	set timeout 1
	#set echo on
	log_user 0
	
	spawn telnet ${address[4]} $SMTPport
	expect "220"

	send "helo $domain\r"
	expect "service"

	send "mail from: <mickeymouse@disney.com>\r"
	expect "250"

	send "rcpt to: <$userid@$domain>\r"
	expect {
		"250" 	{
				set status $expect_out(buffer)
				puts -nonewline "$status"
				puts stderr "validation SUCCESSFUL"
				puts -nonewline "SUCCESS"
				}
		"230" {send_user "ACCESS denied validation UNSUCCESSFUL\n"}
		"550" {send_user "$userid@$domain validation UNSUCCESSFUL\n"}		
		"553" {send_user "Requested action nottaken validation UNSUCCESSFUL\n"}		
	}
	
	send "quit"
	expect "closed"
EOF
)

echo "status is ------ $status"
printf "$body" "$userid" "$domain" "$fname" "$lname" "$status"
 done < $emaillist

However the Parent variable is still blank.

again. thanks for the assistance.

Yes looks like send_user is needed try this:

#!/bin/bash
#script for email validation
emaillist=emaillist1.txt
#emaillist=client.csv
SMTPport=25

clear
echo "---------------------------------------------------------------------"
echo "Now performing email validations.......... Please be patient."
echo "---------------------------------------------------------------------"
echo ""
echo ""

header="%-20s %-20s %-10s %-20s %10s\n"
body="%-20s %-20s %-10s %-20s %10s\n"


printf "$header" "E-mail" "Domain" "F-Name" "L-Name" "Status"
printf "$header" "_______________" "____________________" "__________" "____________________" "____________________"


while read line; do
	userinfo=$(echo $line | sed 's/@/,/')
	IFS=',' read -a address <<< "$userinfo"
	userid="${address[0]}"
	domain="${address[1]}"
	fname="${address[2]}"
	lname="${address[3]}"
	#status=vvv
	SMTPlist=$(host $domain | grep "handled by" | awk '{print $7}' | sed 's/\.$//' )
	address=("${address[@]}" $SMTPlist)
	
	status=$(/usr/bin/expect << EOF
	#exp_internal 1
	set timeout 1
	#set echo on
	log_user 0
	
	spawn telnet ${address[4]} $SMTPport
	expect "220"

	send "helo $domain\r"
	expect "service"

	send "mail from: <mickeymouse@disney.com>\r"
	expect "250"

	send "rcpt to: <$userid@$domain>\r"
	expect {
		"250" 	{
			set status $expect_out(buffer)
			send_user "$status"
			puts stderr "validation SUCCESSFUL"
			send_user "SUCCESS"
		}
		"230" {puts stderr "ACCESS denied validation UNSUCCESSFUL"}
		"550" {puts stderr "$userid@$domain validation UNSUCCESSFUL"}
		"553" {puts stderr "Requested action nottaken validation UNSUCCESSFUL"}
	}
	
	send "quit"
	expect "closed"
EOF
)

echo "status is ------ $status"
printf "$body" "$userid" "$domain" "$fname" "$lname" "$status"
 done < $emaillist
1 Like

You are the MAN! Cant tell you how many ways i tried PUTS to no avail! thanks!