Results 1 to 8 of 8
Hi All
I want to expect command with for loop in shell script.
I have list of server IP's in file and i want to auto login to all and ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
- 12-12-2012 #1Just Joined!
- Join Date
- Dec 2012
- Posts
- 5
Expect Command with for loop in Shell script
Hi All
I want to expect command with for loop in shell script.
I have list of server IP's in file and i want to auto login to all and run the scripts one by one on all servers till the lis is finished.
Please suggest the script for the same.
Thanks
Samit
- 12-13-2012 #2Linux Newbie
- Join Date
- Dec 2011
- Posts
- 119
What is:
Expect Command?
Give example please.
- 12-13-2012 #3Just Joined!
- Join Date
- Dec 2012
- Posts
- 5
Miven
Below is the example of expect command
#!/usr/bin/expect
set timeout 20
#set ip [lindex $argv 0]
#set user [lindex $argv 1]
#set password [lindex $argv 2]
set user root
set ip garlic
set password l00nie
spawn ssh "$user\@$ip"
expect "Password:"
send "$password\r";
interact
above is to expect password of single IP
but i need it for multiple IP's
For that i need to use for loop, which i am not able to do.
Please suggest the expect with for loop
- 12-13-2012 #4Linux Newbie
- Join Date
- Jun 2012
- Location
- SF Bay area
- Posts
- 101
I strongly suggest you use "ssh" instead since then you don't need "expect" at all. It's really much simpler and allows you to create easier to understand (and maintain IMO) scripts.
- 12-13-2012 #5Just Joined!
- Join Date
- Dec 2012
- Posts
- 5
Hi cnamejj
thanks for suggestion. please send me some example
thanks
- 12-13-2012 #6Just Joined!
- Join Date
- Jan 2010
- Posts
- 32
Some ssh server implementations don't support this. I had to write similar script to log into Ruckus ZoneDirect 1100 wireless controller.
I don't know the answer but a workaround -- use expect-script from inside of bash-script. Expect-script is able to get cmd-line params, AFAIK.
- 12-13-2012 #7Just Joined!
- Join Date
- Mar 2007
- Location
- Melbourne, Australia
- Posts
- 28
Here's how I'd do it:
Read more about HEREDOC at Wikipedia.Code:#!/bin/bash ips=`cat <<EOF ip1 ip2 ip3 EOF` username="blahblah" password="password" for ip in $ips; do # use HEREDOC to embed your expect script as below /usr/bin/expect <<EOF spawn ssh $ip # implement all your expect related commands # between the EOF markers # reference any variable defined outside the EOF # markers as you usually do in bash - $varname EOF
Hope that makes sense, otherwise ask after you've tried and can't get it to work...
You can even avoid using username and password by configuring your ssh server to accept your public key!
Cheers,
tkb.
- 12-19-2012 #8Just Joined!
- Join Date
- Dec 2012
- Posts
- 5
Thanks All its done with some internal pre designed scripts


Reply With Quote

