LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 01-28-2016, 01:26 AM   #1
Mayank_Porwal
LQ Newbie
 
Registered: Jan 2016
Posts: 2

Rep: Reputation: Disabled
For loop not working with Expect script


Hi All,
I have written an expect script to login into another server using ssh. But, when I want to do the same for multiple servers using a for loop, it doesnt work.
Below is the script:

#!/usr/bin/expect

match_max 5000
set expect_out(buffer) {}

for i in `cat node_list.txt`
do

node_ip=`echo $i| awk -F"," '{print $1}'`
node_initial_pwd=`echo $i| awk -F"," '{print $2}'`

spawn ssh root@$node_ip

expect {
"*(yes/no)?" {send "yes\r";exp_continue}
"'s password:" {send "$node_initial_pwd\r";exp_continue}
"*current*" {send "$node_initial_pwd\r";exp_continue}
"Enter*" {send "Jan2016!\r";exp_continue}
"Retype*" {send "Jan2016!\r";exp_continue}
"\\\$" { puts "matched prompt"}
}

done
---------
node_list.txt has IP's and passwords separated by a comma(,).
---------
Error I get is this:

mayankp@mayankp:~/scripts/new$ ./connect-to-server.sh
invalid command name "i"
while executing
"i"
("for" initial command)
invoked from within
"for i in `cat node_list.txt`"
(file "./connect-to-server.sh" line 6)

Can you please help me in achieving this?
 
Old 01-28-2016, 02:37 AM   #2
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
I have not used expect very much, but when looking online I found that an expect for loop should look like:
Code:
for {initialization} {conditions} {incrementation or decrementation} {
...
}

# example

for {set i 1} {$i < $no} {incr i 1} {
 set $total [expr $total * $i ]
}
Whereas, what you have written looks like a bash for loop ... are you sure you can mix and match formats in the script?
 
Old 01-28-2016, 03:00 AM   #3
Mayank_Porwal
LQ Newbie
 
Registered: Jan 2016
Posts: 2

Original Poster
Rep: Reputation: Disabled
Yes, I think the issue is with mixing of bash for loop and expect for loop.
But, can I call expect script inside a bash script? If yes, that might solve the problem.
 
Old 01-28-2016, 03:43 AM   #4
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
I think so, but we will need an expect guru to pop in to make sure

On the bash side, a for loop and awk are all not needed:
Code:
while IFS=, read -r node_ip node_initial_pwd
do
  ...
done<node_list.txt
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
For Loop Containing Bash within an Expect Script devUnix Programming 2 10-01-2014 11:37 PM
Working Expect script failing when placed inside bash Loop. Sigmaflux Linux - Newbie 1 01-27-2014 10:51 PM
Expect script with loop does not work zongbot Programming 5 08-31-2011 01:19 PM
expect script - for argument loop ? nilleso Linux - Software 3 09-16-2009 08:51 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 05:30 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration