Grep and mail and piping

Hi Folks,

I wanted to be able to do the following, from the command line:

% *cat /etc/passwd | grep admin > mail -s "/etc/passwd file" admin@example.com*

However, this doesn't work, and instead sends a blank email.

Any ideas on how to make it function?

Thanks in advance.

Doug

Posted on Jul 20, 2008 9:22 AM

Reply
7 replies

Jul 20, 2008 9:40 AM in response to Doug Niven1

However, this doesn't work, and instead sends a blank email.


Actually, it's surprising you get any mail at all.

For a start, you're not piping the output of grep into mail, you're redirecting the output to a file called 'mail'. I'd expect this to throw an error, so maybe it's a transcription issue and you didn't mean to '... > mail', but you meant '... | mail...'

Once that issue is resolved, you'll find you still get blank emails. Try the command without the pipe to mail and you'll see why - there IS no admin account in /etc/passwd. Therefore the grep doesn't output any result, so there's nothing to mail.

User accounts are not stored in /etc/passwd for this very reason - it's too easy for someone to grab the passwd file and find the encrypted passwords. From there it's a short jump to cracking the password and compromising the machine. If that's your goal here, move on.

Jul 20, 2008 9:44 AM in response to Camelot

Folks,

There is no wish to hack or do anything devious here. I'm just trying to string together from the command line how to pipe the results of grep into an email.

Substitute any other text file you like. I'm just trying to figure out how to do this, not debate the merits and/or ethics of doing so.

Let's say I wanted to do this to determine the user's default shell.

% cat /etc/passwd | grep nobody | mail -s "/etc/passwd file" admin@example.com

The above does not work, either.

Jul 20, 2008 3:12 PM in response to Doug Niven1

% cat /etc/passwd | grep nobody | mail -s "/etc/passwd file" admin@example.com


That works fine for me here

Of course, the machine you're running this on actually has a mail server running, right?
By default there's no mail server running in Mac OS X, so the message will set added to the queue but not actually get sent until the mail server is started.

The specifics of starting the mail server varies by Mac OS X version.

This thread has been closed by the system or the community team. You may vote for any posts you find helpful, or search the Community for additional answers.

Grep and mail and piping

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.