LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 02-26-2014, 02:39 PM   #1
Ekamjotsingh
LQ Newbie
 
Registered: Feb 2014
Posts: 6

Rep: Reputation: Disabled
To get total number of users and their groups in remote unix machine


Hi All

I am trying to do ssh to different server and on the remote server for each user trying to get groups of that user but i am not getting the required
result.

ssh acncde@10.129.192.137 "for i in $( cat /etc/passwd| cut -d: -f1);do groups $i done;exit" >>abc.txt

only names are getting shown but not there usernames.
 
Old 02-26-2014, 02:42 PM   #2
suicidaleggroll
LQ Guru
 
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 5,258

Rep: Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947
Use single quotes around your command, otherwise the cat|cut command and the $i will expand in your local shell before getting to the remote machine.
 
Old 02-26-2014, 02:48 PM   #3
Ekamjotsingh
LQ Newbie
 
Registered: Feb 2014
Posts: 6

Original Poster
Rep: Reputation: Disabled
you mean to say like this

ssh username@ipaddress 'for i in $( cat /etc/passwd| cut -d: -f1);do groups $i done;exit' >>abc.txt
 
Old 02-26-2014, 02:50 PM   #4
suicidaleggroll
LQ Guru
 
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 5,258

Rep: Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947
Yes

You don't need the ";exit" either, it will exit by itself when the command is done.
 
Old 02-26-2014, 02:53 PM   #5
Ekamjotsingh
LQ Newbie
 
Registered: Feb 2014
Posts: 6

Original Poster
Rep: Reputation: Disabled
I tried the below command

ssh username@ipaddress 'for i in $( cat /etc/passwd| cut -d: -f1);do groups $i done' >>abc.txt


It gave me the below error

bash: -c: line 1: syntax error: unexpected end of file
 
Old 02-26-2014, 03:03 PM   #6
suicidaleggroll
LQ Guru
 
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 5,258

Rep: Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947
You missed a semicolon between the "$i" and "done" (I didn't catch that in the original post).

Code:
ssh user@ip 'for i in $(cat /etc/passwd|cut -d: -f1); do groups $i; done' >> abc.txt
 
Old 02-26-2014, 03:23 PM   #7
Ekamjotsingh
LQ Newbie
 
Registered: Feb 2014
Posts: 6

Original Poster
Rep: Reputation: Disabled
Thanks a lot it worked.

But now i jst want to fetch the last few lines of /etc/sudoers file in this at the end for the same remote server.

How can i get that in the same server. i am trying to use from the below command

ssh user@ip 'for i in $(cat /etc/passwd|cut -d: -f1); do groups $i; done;cat /etc/sudoers' >> abc.txt


ALso one more problem is that /etc/sudoers can only be viewed by root for which i only have the sudo access. Any way i can achieve that.
 
Old 02-26-2014, 03:28 PM   #8
suicidaleggroll
LQ Guru
 
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 5,258

Rep: Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947
Not that I know of, sudo requires a tty in order to run (since it needs to prompt you for a password).
 
Old 02-26-2014, 03:30 PM   #9
Ekamjotsingh
LQ Newbie
 
Registered: Feb 2014
Posts: 6

Original Poster
Rep: Reputation: Disabled
Thanks a lot of your help.

In the meanwhile i will try see if we can do sudo through any automated way.
 
Old 02-26-2014, 03:33 PM   #10
suicidaleggroll
LQ Guru
 
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 5,258

Rep: Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947
You may be able to get away with some fancy redirection and an expect script, but that will require putting your sudo password in plain-text in the script, which is not a good idea for security reasons.
 
Old 02-26-2014, 03:37 PM   #11
Ekamjotsingh
LQ Newbie
 
Registered: Feb 2014
Posts: 6

Original Poster
Rep: Reputation: Disabled
sorry if i am asking too much questions.

Can you please assist me in that i want to try that.It is very interesting to use these features .
 
  


Reply


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
[SOLVED] cannot add nis users to groups on client machine 2handband Linux - Networking 7 01-14-2010 05:47 PM
What is the maximum number of groups and users allowed in Linux? dave247 Linux - General 2 09-23-2008 02:20 AM
Invoking a programme on a remote machine, bypassing the unix authentication process.` sk_brahma2008 Linux - Newbie 4 07-01-2008 07:18 AM
moving home directories of users onto remote machine shishirkotkar Linux - Newbie 3 04-27-2008 03:20 PM
A problem with UNIX users groups pedrosan Linux - Newbie 4 06-09-2004 10:00 PM


All times are GMT -5. The time now is 10:50 AM.

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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration