Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum. |
Notices |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
|
 |
03-03-2014, 11:29 PM
|
#1
|
Member
Registered: Jul 2009
Location: Brisbane
Distribution: RHEL 6.5
Posts: 39
Rep:
|
Assistance with "Expect" - Reading a list of servers and running against that
Hello,
I have written a script for this a looongg time ago, but I have NO idea how I got it working, I have searched both here, and google for the answer, but I have had no luck.
All I want to do is have Expect run a 'wget' command inside fifty servers which are all listed in one file, line by line.
This shouldn't be difficult, but for some reason, I can't get it working.
If it's possible to have the script request the password as input, as a one time only thing..
ie.
echo "Type the password:"
read -s password
Then call it later.
In short.. ssh into each machine listed in the 'servers' file, run a wget on each machine to pull a file down, and move it to a new location, then exit. Like a "for i in `cat servers` ; do ssh user@$i && wget /tmp/ http://google.com ; done
Except, obviously, that doesn't work... THANKS!! You guys rock!
|
|
|
03-04-2014, 10:23 AM
|
#2
|
Member
Registered: Oct 2012
Distribution: OpenSuSE,RHEL,Fedora,OpenBSD
Posts: 982
|
I'd use autoexpect to create the expect script that calls ssh and enters a password.
I'd use shell to run the expect script against all the servers providing stdin of the commands wanted on each server.
Code:
for i in `cat servers`
do
e < w
done
|
|
1 members found this post helpful.
|
03-04-2014, 05:01 PM
|
#3
|
Member
Registered: Jul 2009
Location: Brisbane
Distribution: RHEL 6.5
Posts: 39
Original Poster
Rep:
|
Hi linosaurusroot,
Thanks for the reply.
Although that will work, I'm more after a single file / script to run in order to do this.
Do you know a way to get expect to look through a file by itself? I know it can definitely do it, I just can't recall how.
I did find a method that might be useful, but I'm not able to get it working..
ie.
Code:
#!/usr/bin/expect
set servers [open "vms"]
set servernames [split [read $servers] "\n"]
close $servers
foreach server $servernames {
spawn ssh root@$server
expect "assword:"
send "p@55word\r"
expect "$ "
send "wget -O /tmp/TEST.ONLY http://server_CNAME/file_to_retrieve\r"
send "exit\r"
expect eof
close
}
But from there, I'm not sure where I would go... When I place commands after this section, it logs into the first server I have in the list, it sits there for a bit, then runs the wget, runs the 'exit command, closes the connection to that first server, then the script dies, (without moving to the next server) stating:
Code:
spawn_id: spawn id exp4 not open
while executing
"close"
("foreach" body line 9)
invoked from within
"foreach server $servernames {
spawn ssh root@$server
expect "assword:"
send "p@55word\r"
expect "$ "
send "wget -O /tmp/TEST.ONLY http://serv..."
(file "./new_script" line 7)
Any ideas? Thanks guys!
|
|
|
03-10-2014, 11:11 PM
|
#4
|
Member
Registered: Jul 2009
Location: Brisbane
Distribution: RHEL 6.5
Posts: 39
Original Poster
Rep:
|
Hey,
So, I couldn't find a solution to this, so I just ran it with a for loop, and pasted the password in each time.
Thanks for your assistance anyway!
for i in `cat vms` ; do ssh $i "wget -O /usr/bin/SCRIPT http://satellite/pub/SCRIPT && chmod 766 /usr/bin/SCRIPT && exit" ;done
For those that would like to know....
Essentially, this just ran through a file, with a heap of servers listed line by line, and SSH'd into each of them, then ran each command and exit'd.
Thanks,
Xen.
|
|
|
03-17-2014, 06:23 PM
|
#5
|
Member
Registered: Jul 2009
Location: Brisbane
Distribution: RHEL 6.5
Posts: 39
Original Poster
Rep:
|
I figured another way to do this, just for any future people that need this done also.
I basically run a for loop against the bottom script... Where "server_list" is a line by line list of servers,
Code:
for i in `cat server_list` ; do ./below_script $i ; done
Code:
#!/usr/bin/expect -f
set server [lrange $argv 0 0]
spawn ssh username@$server
expect "*?assword:*"
send -- "P@ssword_HERE\r"
expect "*\r"
expect "\r"
send -- "Command to run here\r"
send -- "\r"
send -- "exit\r"
expect eof
I hope that helps!
Xen.
|
|
|
All times are GMT -5. The time now is 08:40 PM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|