Submit Hint Search The Forums LinksStatsPollsHeadlinesRSS
14,000 hints and counting!

View connected file sharing clients' IP numbers UNIX
This command looks for any connections to port 548 (Apple's File Sharing port), and lists the IP address of any connections through filesharing:
 % netstat -f inet -n | grep '.548 ' | awk '{ print $5 }' \
| sed 's/\.[^.]*$//'
I saw this while on the IRC channel (and server) for the MacFora web boards, I believe it was a person by the nickname "ian" that provided the script for the nice result. Someone else there mentioned they were working on an app to show the number of connections (for all types, not just filesharing) for those that don't want to use terminal.

I have only been able to test this for one connection so far.
    •    
  • Currently 3.00 / 5
  You rated: 4 / 5 (4 votes cast)
 
[17,676 views]  

View connected file sharing clients' IP numbers | 8 comments | Create New Account
Click here to return to the 'View connected file sharing clients' IP numbers' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
Analysis
Authored by: Lizard_King on Dec 18, '02 03:01:57PM
Kewl hint! However, I'm a junky for understanding all of the innards of commands and scripts and being clear on exactly what they do. Here's my interpretation of this command (with a bunch of holes that I hope kind folks can fill in for me so I can learn!):

% netstat -f inet -n | grep '.548 ' | awk '{ print $5 }' | sed 's/.[^.]*$//'

netstat -f inet -n
show statistics only for the 'inet' address family and display network addresses as numbers

grep '.548 '
find the instances where connections are made to the apple file sharing port

awk '{ print $5 }'
scan for the fifth element and print it

sed 's/.[^.]*$//
change the output somehow (I'm not sed fluent at all)




[ Reply to This | # ]
Analysis
Authored by: signals on Dec 18, '02 03:46:38PM

> sed 's/.[^.]*$//
> change the output somehow (I'm not sed fluent at all)

The pattern s/{regex}/{string}/ is a search and replace function.

The regex is ".[^.]*$" which matches the part of the line from the last "." to the end of the line (see the manpage for ed(1) for help with regular expressions)

The string to replace it with is nothing ("//").

So this will take everything from the last period on the line to the end of the line and remove it (replaces it with nothing).

This drops the port numbers from the end of the ip numbers netstat returns.

-Signals



[ Reply to This | # ]
Actually that grep is redundant, all you need is sed..
Authored by: cynikal on Dec 18, '02 04:05:24PM

the grep parameter should be '\\.548' instead of just '.548' because the dot is a wildcard for 'match any character' (similar to ? in the shell).

If you don't escape the meaning of (with the backslash) then you will match anything with 548 including 1548, etc.. Granted these other ports are probably not very common this will probably work. But i just wanted to make this command sniplet more effective.

If you want to, you can combine the awk/grep line (because awk can do everything grep and sed do) like this:

netstat -f inet -n | awk '$5 ~ /\\.548$/ {sub(/\\.[^.]+$/, ""); print $5}'

basically, i had awk check that the 5th field (white space seperated) ends in a dot (not any character) followed by 548 followed by the end of the field (the dollar sign is an 'anchor' for the end of the string, the field being the entire string). If this holds true for the current line, i used the sub() awk function (takes place of the seperate sed). i then also only print that 5th field.



[ Reply to This | # ]
oops..
Authored by: cynikal on Dec 18, '02 04:08:26PM

there should NOT be double slashes, only single slashes..

When i previewed my post, it ignored the first slash so i put two (sometimes you need to escape the slash itself :P)



[ Reply to This | # ]
Actually that grep is redundant, all you need is sed..
Authored by: loren_ryter on Dec 20, '02 09:30:45AM

I don't know how quoting works in tch, so i wonder if anyone can give the sytax of you wanted to make that an alias.

thanks.



[ Reply to This | # ]
The correct string is...
Authored by: geppo1982 on Apr 12, '06 11:20:48PM
netstat -f inet -n | awk '$5 ~ /\.548$/ {sub(/\.[^.]+$/, ""); print $5}'
Thanks cynikal for your hint.

[ Reply to This | # ]
netstat usage
Authored by: soulrider2k on Jan 06, '03 08:27:02PM

simple to understand other way.

netstat -na | grep 548

I prefer this because if you are on another machine this is alot easier to remember.



[ Reply to This | # ]
netstat usage
Authored by: LouieNet on Jan 13, '03 11:56:51AM

"easier to remember"?!? Anything worth doing more than a couple times is worth at least an alias. :)

Louie



[ Reply to This | # ]