Expect - bash and variables


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Expect - bash and variables
# 1  
Old 12-22-2014
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

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

Last edited by vbe; 12-22-2014 at 03:56 PM.. Reason: code tags please...
# 2  
Old 12-22-2014
Its pretty easy to do. This is how I set the current time in my backup script.

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

You just need something like:

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

Then the results of one script will be assigned to a variable in another script.
# 3  
Old 12-22-2014
is this using the expect interpreter?
# 4  
Old 12-22-2014
Try using:

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

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

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

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

The above prints the error message on the terminal and sets $result to "FAILED"
# 5  
Old 12-22-2014
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.

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

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

Last edited by ylafont; 12-22-2014 at 08:45 PM..
# 6  
Old 12-22-2014
Yes looks like send_user is needed try this:

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

This User Gave Thanks to Chubler_XL For This Post:
# 7  
Old 12-22-2014
You are the MAN! Cant tell you how many ways i tried PUTS to no avail! thanks!
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Advanced & Expert Users

Bash script + expect

im very happy to back for this forum I have servers with alias of double dns extentions: sample: servera.test.com servera.test1.com serverb.test.com serverb.test1.com I need to login to that severs and executing the set of commands if test.com failed then try to login via... (0 Replies)
Discussion started by: prakash0106
0 Replies

2. Shell Programming and Scripting

Bash script with expect

Dear all Hi I want use expect in bash so that we can not use these with each other /bin/bash. With. /usr/bin/expect How can use these with on script or how can call a script from other script #!/bin/bash clear echo "================================== " echo "Enter your Esxi IP"... (3 Replies)
Discussion started by: Baber
3 Replies

3. Shell Programming and Scripting

Passing awk variables to bash variables

Trying to do so echo "111:222:333" |awk -F: '{system("export TESTO=" $2)}'But it doesn't work (2 Replies)
Discussion started by: urello
2 Replies

4. Shell Programming and Scripting

BASH arrays and variables of variables in C++

Sometimes it is handy to protect long scripts in C++. The following syntax works fine for simple commands: #define SHELLSCRIPT1 "\ #/bin/bash \n\ echo \"hello\" \n\ " int main () { cout <<system(SHELLSCRIPT1); return 0; } Unfortunately for there are problems for: 1d arrays:... (10 Replies)
Discussion started by: frad
10 Replies

5. Shell Programming and Scripting

Expect in bash to get the return value

cat test.sh #!/bin/sh expect <<- EOF set timeout 5 spawn ssh -o StrictHostKeyChecking=no lyang0@128.224.178.245 -C mkdir -p /tmp expect { "Password:" {send "root\r"} } spawn scp -o StrictHostKeyChecking=no /tmp/1 lyang0@128.224.178.245:/tmp/ ... (1 Reply)
Discussion started by: yanglei_fage
1 Replies

6. Shell Programming and Scripting

Expect and variables

I'm trying to make an expect function that will pass through a variable. /usr/bin/expect<<EOD spawn su - expect "Password: " send "$psswd\r" expect "#" send "/etc/init.d/network restart >>$log\r" expect "#" send "exit\r" EOD The $log passes through but my $psswd fails I know... (1 Reply)
Discussion started by: Lotheovian
1 Replies

7. Shell Programming and Scripting

expect in bash script

Hi, I'm writing a shell script that calls a few commands that prompt the user for two simple yes/no questions. if the answers are consistent (the first is a yes, the second is a no), what would my expect script look like? Google is only giving me answers for scripts where I telnet or ssh. right now... (3 Replies)
Discussion started by: js741
3 Replies

8. UNIX for Dummies Questions & Answers

How to pass variables in Expect command?

Hi All, I need to frame a unix script to logon to a unix box. The credentials needs to be obtained from a property file on the same location. I am trying to use 'expect' and 'spawn' command to meet this req. When I am passing values, these commands are working fine. but when I am trying to... (3 Replies)
Discussion started by: mailkarthik
3 Replies

9. Shell Programming and Scripting

Need expect to read variables from a list while logged into the same device

Hi, I'm primarily a Cisco/Juniper networking guy, so you'll have to forgive my ignorance when it comes to scripting (although I do write simple backup scripts and things of that nature on a regular basis and I run Linux at home, so I am vaguely familiar with it). What I need to do should be... (2 Replies)
Discussion started by: wolverene13
2 Replies

10. Shell Programming and Scripting

Problems with expect and set variables

I'm writing a script that'll send a time-stamp to my backup server. I create a file with the name of the current date, send it to my server with scp and rm the file from the local computer. Individually these commands work fine and with a set name the expect scripts also work fine. The problem... (0 Replies)
Discussion started by: Ktesh564
0 Replies
Login or Register to Ask a Question