ArtsAutosBooksBusinessEducationEntertainmentFamilyFashionFoodGamesGenderHealthHolidaysHomeHubPagesPersonal FinancePetsPoliticsReligionSportsTechnologyTravel

samba network scanner

Updated on October 08, 2012

Scripting scanner

Linux and scripting == Lots of fun

I wrote this script as a very simple tool to get all the open shares on a network with their contents.


How it works...

  • Does a smbtree to get all the available network shares
  • Writes all the details to a standard text file
  • Reads text file and tries to get a directory contents
  • Saves directory listing to text file

From here you have a local text file you can use to grep and search for files as required, much quicker that scanning network.

You need the basic samba tools installed,

  • smbtree
  • smbclient


Dir structure

You need to create the "scanned" dir for your scanned files you can grep

smbscanner/
|-- scanned              - create this dir
|   `-- 2012-10-05       - will auto create files in YYYY-MM-DD with scanned contents
|-- smbscanner.bash      - the scanner file
|-- smbtree.scan.txt     - will auto create
`-- smbtree.txt          - will auto create



Quick setup

$ mkdir /path/to/smbscanner

$ cd /path/to/smbscanner

$ mkdir scanned

$ <your-favourite-editor> smbscanner.bash

--- insert into file

#!/bin/bash
if [ ! -e smbtree.txt ]; 
then
    echo "Start smbtree scanning"
    smbtree -N > smbtree.txt
fi
grep -e '\\\\.*\\.*  ' smbtree.txt | awk '{ FS = " "; print $1}' | grep -v '\$$' | sed -e s/\\\\/\\//g > smbtree.scan.txt
echo "Starting directory fetching..."
> scanned/$(date +%Y-%m-%d)
for smbscan in `cat smbtree.scan.txt`; 
do 
    echo "${smbscan}" >> scanned/$(date +%Y-%m-%d)
    smbclient -N ${smbscan} -c 'recurse;ls' >> scanned/$(date +%Y-%m-%d)
done


--- end

Run the scanner

$ bash smbscanner.bash


Reading the scanned file

The file layout is very simple, shared name on top, starting with // and contents of the shares following.

---

//NAMED-PC/music
  .                                   D        0  Mon Nov 15 08:00:53 2010
  ..                                  D        0  Fri Oct  5 09:12:39 2012
  The Carpenters                      D        0  Tue Jun 29 11:39:34 2010
  Space cat                           D        0  Tue Jun 29 10:37:16 2010
  Everclear                           D        0  Tue Aug 10 10:28:48 2010
  Wedding Singer Soundtrack           D        0  Tue Jun 29 10:56:18 2010
  BEATLES                             D        0  Tue Jun 29 09:51:06 2010
  Gladiator                           D        0  Tue Jun 29 10:05:28 2010

---

Feel free to use and modify as required


Comments

    0 of 8192 characters used
    Post Comment

    No comments yet.

    Click to Rate This Article