LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
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 04-15-2009, 02:02 PM   #1
zirtik
Member
 
Registered: Jul 2005
Posts: 33

Rep: Reputation: 15
Deleting remote directories on multiple hosts with a bash script


Hi,

I have a text file which has a bunch of host names in it. It looks as follows:

Code:
host1.universitydomain.edu
host2.universitydomain.edu
host3.universitydomain.edu
host4.universitydomain.edu
.
.
.
I want to delete some directories from each of these nodes if they exist.
I can ssh to all of the nodes but the file contents change dynamically so I need to write a script that does this job automatically.

My script looks like:


Code:
cat nodes.txt | while read line;
do
    echo "ssh $line rm -rf /tmp/hadoop-0.19.1 &"
    ssh $line rm -rf /tmp/hadoop-0.19.1 &
done

but when I run this script I figure that it doesn't work, for some reason when I manually ssh to the nodes and check /tmp directory, I still see hadoop-0.19.1 under /tmp. I tried to remove the & from the end so it looks like:

Code:
cat nodes.txt | while read line;
do
    echo "ssh $line rm -rf /tmp/hadoop-0.19.1"
    ssh $line rm -rf /tmp/hadoop-0.19.1
done
but in this case my script only removed the directory from the first host in the list and skipped the rest without even printing the echo message.

I'd appreciate any help, thanks in advance.
 
Old 04-15-2009, 02:07 PM   #2
ncsuapex
Member
 
Registered: Dec 2004
Location: Raleigh, NC
Distribution: CentOS 2.6.18-53.1.4.el5
Posts: 770

Rep: Reputation: 44
I have a similar script that I use to do work on remote servers:


Quote:
for i in `cat hostnames`
do
echo "host $i returned the following results:"
ssh root@$i -C rm -rf /tmp/hadoop-0.19.1
done

see if that works for you. I also have SSH keys setup so I dont have to input passwords for each server.
 
Old 04-15-2009, 02:35 PM   #3
zirtik
Member
 
Registered: Jul 2005
Posts: 33

Original Poster
Rep: Reputation: 15
Thanks ncsuapex. That really worked. So what is the difference between iterating over the lines with a for loop and a while loop? I just want to understand why my approach failed and learn something from my mistake.
 
Old 04-15-2009, 02:42 PM   #4
ncsuapex
Member
 
Registered: Dec 2004
Location: Raleigh, NC
Distribution: CentOS 2.6.18-53.1.4.el5
Posts: 770

Rep: Reputation: 44
To be honest.. I dont know... Im not much of a programmer.. I had to get help to figure mine out so I just posted mine because I knew it should do what you wanted. I, too, would be interested in knowing why yours didnt work.
 
Old 04-15-2009, 02:53 PM   #5
zirtik
Member
 
Registered: Jul 2005
Posts: 33

Original Poster
Rep: Reputation: 15
No worries, thanks again ncsuapex. I appreciate you being so responsive.
 
Old 04-15-2009, 07:23 PM   #6
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
Quote:
Originally Posted by zirtik View Post
difference between iterating over the lines with a for loop and a while loop?
there is no differences in your case. cut the useless cat in your while loop
Code:
while read line
do
 ssh "$line" <your command>
done < host.txt
check also how ssh us the "host" portion, eg ssh root@server.com
 
Old 04-16-2009, 09:52 AM   #7
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian, Arch
Posts: 3,780

Rep: Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081
Quote:
Originally Posted by zirtik View Post
Thanks ncsuapex. That really worked. So what is the difference between iterating over the lines with a for loop and a while loop? I just want to understand why my approach failed and learn something from my mistake.
The difference is that when you do cat file | command and command is a compound command like a while statement, every command inside reads from file. In your case there were 2 commands that use input: the read command, and ssh. If you use ssh -n it should work fine.
Code:
     -n      Redirects stdin from /dev/null (actually, prevents reading from
	     stdin)...
Another way would be to open a new file descriptor:
Code:
exec 3< file # open file for reading
while read -u3 line ...
 
  


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
create multiple directories in bash? Count Zero Programming 19 07-06-2008 08:34 AM
FTP script to multiple hosts telamonides Linux - General 6 02-23-2007 05:58 PM
ssh to multiple hosts behind remote nat keex Linux - Networking 3 01-05-2006 10:34 AM
remote ssh commands on multiple hosts evilchild Linux - Software 6 08-12-2004 10:48 PM
BASH scripting browsing multiple directories PokerFace Programming 3 10-02-2002 12:50 PM

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

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