LinuxQuestions.org
Help answer threads with 0 replies.
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-31-2007, 08:26 AM   #1
m1ck
LQ Newbie
 
Registered: Jan 2007
Distribution: Ubuntu
Posts: 5

Rep: Reputation: 0
SSH Bash Loop


Hi All,

I'm trying to do a loop that will ssh onto a list of nodes and print to a file the current processes of that node. I currently have the following but for some reason its only doing the first node in my list. Is there something wrong with the ssh line? (I've cut the print statement down so its easier to read)

Any advice appreciated.

Code:
while read uplist

do

	ssh $uplist ps aux | awk '$1 != "root" {print $1}' >> processes

	
done < uplist

exit
 
Old 01-31-2007, 09:27 AM   #2
matthewg42
Senior Member
 
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530

Rep: Reputation: 65
Can you show a section of the uplist file? The "while read" loop will want the input as one input per line of the file.
 
Old 01-31-2007, 09:31 AM   #3
m1ck
LQ Newbie
 
Registered: Jan 2007
Distribution: Ubuntu
Posts: 5

Original Poster
Rep: Reputation: 0
Its a list of IP's one line after the other....


149.x.x.x
149.x.x.x
149.x.x.x
149.x.x.x
149.x.x.x
149.x.x.x
etc....
 
Old 01-31-2007, 09:37 AM   #4
m1ck
LQ Newbie
 
Registered: Jan 2007
Distribution: Ubuntu
Posts: 5

Original Poster
Rep: Reputation: 0
I've worked it out after some googling...

I needed the -n in my ssh line so that ssh didn't read from standard input .

Thanks
 
Old 01-31-2007, 09:44 AM   #5
m1ck
LQ Newbie
 
Registered: Jan 2007
Distribution: Ubuntu
Posts: 5

Original Poster
Rep: Reputation: 0
One more quick questions -

How can I print $1 if its not equal to another username as well i.e.

ssh $uplist ps aux | awk '$1 != "root" && !="usera" {print $1}'

The above doesnt work I was just wondering is there an easy way to do it?
 
Old 01-31-2007, 09:49 AM   #6
matthewg42
Senior Member
 
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530

Rep: Reputation: 65
Do it like this:
Code:
... | awk '{ if ( $1 != "root" && $1 != "usera" ) { print $1; } }'
 
Old 01-31-2007, 09:52 AM   #7
matthewg42
Senior Member
 
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530

Rep: Reputation: 65
Or you could use grep to exclude lines based on a regular expression pattern. It might be cleaner if you have multiple patterns:
Code:
... | grep -v -e ^root -e ^usera -e ^userb ...
You can just add "-e pattern" clauses for each one you want to exclude. ^ just means "beginning of line"
 
Old 01-31-2007, 10:03 AM   #8
m1ck
LQ Newbie
 
Registered: Jan 2007
Distribution: Ubuntu
Posts: 5

Original Poster
Rep: Reputation: 0
Thanks mate both work perfectly
 
  


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
how to loop over text file lines within bash script for loop? johnpaulodonnell Linux - Newbie 9 07-28-2015 03:49 PM
Complex bash loop. hepburnenthorpe Linux - General 2 11-19-2006 09:42 AM
Bash Loop Dialog xlordt Programming 5 09-26-2006 09:40 AM
bash script loop bong.mau Programming 6 09-14-2005 07:38 PM
Bash for loop Genjix Programming 5 12-23-2004 02:56 AM

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

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