FreeBSD smbclient grep
Quick background
This is a follow-up post to the samba and grep article. This post provides and interesting look at how to utilize the application smbclient and grep to help you find whatever you are looking for on a shared network using FreeBSD. Theoretically this can apply to Linux distro's but I have not tested it personally.
The applications
smbclient
is part of the samba suite. It provides your the ability to connect to a windows network share. Here we will not go into too much detail and if you require more in-depth info please consult the samba documentation.
grep
is a very handy tool in the *nix environment. Looking for a needle in a haystack ? use grep =]. Grep searches the file (or stream) and returns the results of your search.
Quick and dirty
I have not timed the result time but want to bet that this search method beats a Network Neighborhood find function =]
Here we go:
smbclient -c 'recurse; ls /' -N //server/share_name | grep --color=auto -iE "\.avi|\.mpg|\.mp4|\.wm[v]|\.r[a]m|mp[e]g"
Explaining it:
This is a very interesting command, so here is the breakdown
smbclient -c 'recurse; ls /' -N //server/share_name
- smbclient - invoke (start) the smbclient
- -c 'recurse; ls /' - execute the smb command to set directory listing to recursive display and search the root (top level) folder and all its sub folders
- -N - don't ask for a password
- //server/share_name - the server and the share to search
| (pipe)
A very handy function used to redirect the output of one application (smbclient) the the input of another application (grep)
grep --color=auto -iE "\.avi|\.mpg|\.mp4|\.wm[v]|\.r[a]m|mp[e]g"
- grep - starting the grep application
- --color=auto - highlight the matched results
- -i - be case insensitive
- E - extended regular expression
- "\.avi|\.mpg|\.mp4|\.wm[v]|\.r[a]m|mp[e]g" - the fun part... what to search for.
Breaking down the search
- The \(backslash) in the regular expression(regex - the funky search on top) is an escaping character.
- The . (period) has a different meaning in regex and therefore must be 'escaped' to be seen as a . (period).
- The | (pipe) in regex means OR. Example: avi|mov = avi OR mov
- The [] (square brackets) in regex is and optional value in the pattern, almost like an 'OR'. Example: r[a]m = ram or rm
If you should read it, it might sounds like this...
"\.avi|\.mpg|\.mp4|\.wm[v]|\.r[a]m|mp[e]g"
dot avi OR dot mpg OR dot mp4 OR dot wm or dit wmv OR dot rm or dot ram OR mpg OR mpeg
Too much to type!
Who is going to argue that.. it is quite a bit to type. Lets make it easier an put it in a simple script. Create a file called smb.share.video.sh
#!/bin/sh
# search a windows share for files
echo "* recursive search a client for video"
echo -n "enter [//server/share] : "
read CLIENT
echo '<search>'
smbclient -c 'recurse; ls /' -N ${CLIENT} | grep --color=auto -iE "\.avi|\.mpg|\.mp4|\.wm[v]|\.r[a]m|mp[e]g"
echo '</search>'To run it:
#sh smb.share.video.sh
End
All done. Any comments and suggestions are welcomed!
Comments
No comments yet.