LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
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


Reply
  Search this Thread
Old 03-03-2014, 11:29 PM   #1
xenner
Member
 
Registered: Jul 2009
Location: Brisbane
Distribution: RHEL 6.5
Posts: 39

Rep: Reputation: 0
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!
 
Old 03-04-2014, 10:23 AM   #2
linosaurusroot
Member
 
Registered: Oct 2012
Distribution: OpenSuSE,RHEL,Fedora,OpenBSD
Posts: 982
Blog Entries: 2

Rep: Reputation: 244Reputation: 244Reputation: 244
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.
Old 03-04-2014, 05:01 PM   #3
xenner
Member
 
Registered: Jul 2009
Location: Brisbane
Distribution: RHEL 6.5
Posts: 39

Original Poster
Rep: Reputation: 0
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!
 
Old 03-10-2014, 11:11 PM   #4
xenner
Member
 
Registered: Jul 2009
Location: Brisbane
Distribution: RHEL 6.5
Posts: 39

Original Poster
Rep: Reputation: 0
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.
 
Old 03-17-2014, 06:23 PM   #5
xenner
Member
 
Registered: Jul 2009
Location: Brisbane
Distribution: RHEL 6.5
Posts: 39

Original Poster
Rep: Reputation: 0
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.
 
  


Reply

Tags
expect, scripting


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
Shell script using expect to login to couple of remote servers and read "crontab -l" jaipsharma Linux - Networking 3 03-16-2013 04:59 PM
"Multicolumn" or "tiles", or even "list" icon view on desktop, in any DE? the dsc Linux - Desktop 3 02-20-2010 09:25 AM
LXer: Displaying "MyComputer", "Trash", "Network Servers" Icons On A GNOME Desktop LXer Syndicated Linux News 0 04-02-2007 08:31 AM
Is it possible to "mirror" package list between servers. tommyj27 Ubuntu 2 09-24-2006 07:12 AM
User "list" running process "python" TroelsSmit Linux - Newbie 2 02-22-2005 04:55 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 08:40 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