0

I want to write a Unix command which select a random user from /etc/passwd and check a remote host using SHH to determine if a user with the same UID exists, print out the full line from passwd if the user exists.

Here is the command I wrote so far:

echo `shuf -n 1 /etc/passwd | cut -d ":" -f 3' | ssh 100.64.0.2  sed -n -e '/**xargs**/p' /etc/passwd

Seems like the sed command doesn't take xargs as a argument from local machine, but a text

Any idea would be very appreciated!

Pang
  • 9,564
  • 146
  • 81
  • 122
Haka
  • 1

2 Answers2

0

I think you were trying to write something like this

user=$(shuf -n 1 /etc/passwd | cut -d ":" -f 3)
ssh 100.64.0.2  sed -n -e "/$user/p" /etc/passwd

But you might also want to look into using getent instead.

user=$(shuf -n 1 /etc/passwd | cut -d ":" -f 3)
ssh 100.64.0.2  getent passwd "$user"

As I said in my comment on the OP xargs is a command. It isn't magic that will read a value from standard input and stick it on the command line. (Note that there's an ordering problem with that even as a concept since the command line, by definition, is constructed before any standard input can possibly exist.)

Etan Reisner
  • 77,877
  • 8
  • 106
  • 148
  • ssh 100.64.0.2 $(shuf -n 1 <(getent passwd) | cut -d ":" -f 3) –  Oct 31 '14 at 20:32
  • What you wrote is superfluous for one and furthermore undoes itself. Check your logic, I fixed it for you. =) –  Nov 01 '14 at 01:49
  • @A.Danischewski You are passing the UID of a random system user as the command to the remote system. How is that *at all* relevant to the question the user asked or even a valid thing to do? What about my examples "undoes itself"? And yes, you could certainly avoid the `$user` variable but there is little reason to do so in this case and using the varialbe makes the distinction between local command and remote command much more clear. – Etan Reisner Nov 03 '14 at 17:31
  • When you do shuf -n 1 /etc/passwd you get a line from passwd like mail:x:8:8:mail:/var/mail:/bin/sh after you get the third field the UID in this case 8 - after you do an ssh and pass getent passwd "$user" - first of all you need to write it like ssh 100.64.0.2 $(getent passwd "$user") and second of all if you did that getent passwd "$user" with 8 returns you back the full string from the passwd file that you started with mail:x:8:8:mail:/var/mail:/bin/sh –  Nov 04 '14 at 12:12
  • @A.Danischewski We are trying to run the `getent` and `sed` commands **on the remote host** not locally. We are trying to get the output **from the remote host** for the UID fetched from the local system. And yes, we are trying to get back the `passwd` file entry for that UID **from the remote host**. – Etan Reisner Nov 04 '14 at 13:16
  • 1
    That makes more sense, but in the first ssh the sed will return a match anywhere so you probably want to relegate it to only the 3rd field: eg. ssh 100.64.0.2 sed -e -n "/^[^ ]*:[^ ]*:$user:/p" /etc/passwd –  Nov 04 '14 at 14:13
  • @A.Danischewski Fair point. I just copied the OP's script. Using `sed` for this is wrong in the first place so I'm not particularly inclined to bother fixing it. – Etan Reisner Nov 04 '14 at 14:15
0
for f in `cat hostlist.txt`; do echo "### $f ###";  ssh $f find / -name "tmp" | sed "s/^/'$f  | '/" ;  done 2>/dev/null

i had difficult time to print hostname for each line of a find command, this helps to solve.