Probe - Network Scanner

Probe - Network Scanner + Asset

Working on a V2 of this, but I was requested a few times for access to some probe code:) I am always up for some improvements if anyone sees anything they like to add on. V2 should work with some router APIs in the future to pull tables along with current method.

Keep in mind - Do not run this on multi-systems. Only assign to a single device for a client unless they have different locations/probe areas.

What it does and does not

This scripts pings across the network looking for new devices on the network and adds them to your asset list. This will grab anything that will respond back to a ping so this isnt going to work against devices that are hidden or doesnt respond back. This still helps a lot in finding printers, network devices, missing computers, and etc. Once setup on a machine - you may set a weekly or daily scan. If the script can not figure out what the device is… it will display it as a random IP instead of giving the asset a name. Depending on the client - they may have random IP/devices come in and out… I would recommend tracking/or removing them as needed other wise you might end up with a list of random IPs over time. This also takes the location variable of your current Syncro asset and uses it to also set the newly added devices as well. For example, Location can be the state, local name (Building 1, Building 2, etc), or be left blank.

Setup:

Syncro Asset:
Location → Text field

Create Asset Type: Syncro Other
Location → Text field
Last Updated → Text field
Last Seen → Text field
Device Type → Select box

  • Dropdown:
    
    •     Anything you want really, but at least:
      
    •     Printer
      
    •     Router
      
    •     NAS
      
    •     Phone
      
    •     Other
      

mac → Text field
ip → Text field

Script:

Download and upload WNetWatcher.exe from Wireless Network Watcher - Show who is connected to your wireless network
c:\temp\WNetWatcher.exe → WNetWatcher.exe

Variables:

$location_id → platform → {{asset_custom_field_location}}
$customer_id → platform → {{customer_id}}

Powershell → Run As → System

Your API Key needs to support adding and editing of devices

Import-Module $env:SyncroModule


$WNetWatcher = Get-Process WNetWatcher.exe -ErrorAction SilentlyContinue
if ($WNetWatcher) {
  Exit
}
Remove-Variable WNetWatcher

$path = "C:\temp"
If(!(test-path $path)) {
    New-Item -ItemType Directory -Force -Path $path
}

Remove-Item -path "C:\temp\network.txt"
Start-Process -NoNewWindow -FilePath "c:\temp\WNetWatcher.exe" -ArgumentList "/scomma c:\temp\network.txt" -wait

Write-Output $customer_id 
if ( $customer_id ) {
    $url = "" # Your API URL
    $apikey = "" #Your API Key
    $device_list = Import-Csv "C:/temp/network.txt"

    # Gathers all the devices from the website
    $page = 1
    $assets_list = @()
    while ( $TRUE ) {
        $data = Invoke-RestMethod -Uri "$url/customer_assets?api_key=$apikey" -Method Get -Body @{"page"=$page; "customer_id"=$customer_id} -ContentType 'application/json'
        $page++
        if ( -not $data.assets ){
            break
        }
        $assets_list = $assets_list + $data.assets
    }
    # Track how many devices
    $assets_list.assets.Count
    $device_list.Count

#    foreach ($asset in $assets_list) {
#        $asset.name
#        $asset.properties.mac
#        $asset.asset_type
#        Write-Output "--------------------"
#    }

    foreach($new_asset in $device_list) {
        # IP Address, Device Name, MAC Address, Network Adapter Company, Device Information, User Text, First Detected On, Last Detected On, Detection Count, Active
        
        # Make sure it has a name - other wise just skip it very rare this should ever happen, but to make sure it never does
        if ( -not $new_asset."Device Name" ) {
            $new_asset."Device Name" = $new_asset."Network Adapter Company"
        }

        if ( -not $new_asset."Device Name" ) {
            $new_asset."Device Name" = $new_asset."IP Address"
        }

        if ( -not $new_asset."Device Name" ) {
            continue
        }

        $device_type = ''
        $new_asset."Device Name"
        if ( $new_asset."Device Name" -eq 'Grandstream Networks, Inc.' ) {
            $device_type = 'Phone'
        } elseif ( $new_asset."Device Name" -eq 'LEXMARK INTERNATIONAL, INC.' ) {
            $device_type = 'Printer'
        } elseif ( $new_asset."Device Name" -eq 'Panasonic Communications Co., Ltd.' ) {
            $device_type = 'Phone'
        } elseif ( $new_asset."Device Name" -eq 'YEALINK(XIAMEN) NETWORK TECHNOLOGY CO.,LTD.' ) {
            $device_type = 'Phone'
        } elseif ( $new_asset."Device Name" -like 'balance*' ) {
            $device_type = 'Router'
        } elseif ( $new_asset."Device Name" -like 'CANON*' ) {
            $device_type = 'Printer'
        }

        # Loops over current devices to compare if it exist already. If it does, update the device. If the for loops finishes and the new_device triger isnt set - then it will create it as a new device instead.
        $new_device = $TRUE
        foreach ($asset in $assets_list) {
            try {
                if ( $asset.properties.mac -contains $new_asset."MAC Address" -or $asset.properties.ip -contains $new_asset."IP Address" )  {
                    $new_device = $FALSE
                    if ( $asset.asset_type -ne "Syncro Device" ) {
                        if ( !( $device_type ) ) {
                            $device_type = $asset.properties."Device Type"
                        }

                        $last_updated = $asset.properties."Last Updated"
                        if ( $asset.properties.ip.ToString() -ne $new_asset."IP Address" -or $asset.name.ToString() -ne $new_asset."Device Name" -or $device_type -ne $asset.properties."Device Type") {
                            $last_updated = (Get-Date).ToString()
                        }

                        Write-Output "Updating $($asset.name)"
                        $device = @{
                            "name"=$new_asset."Device Name";
                            "properties" = @{
                                'mac' = $new_asset."MAC Address";
                                'ip' = $new_asset."IP Address";
                                'Location' = $location_id;
                                'Device Type' = $device_type;
                                'Last Updated' = $last_updated;
                                'Last Seen' = (Get-Date).ToString()
                            }
                        }
                        Write-Output ( Invoke-RestMethod -Uri ("$url/customer_assets/" + $asset.id.ToString() + "?api_key=$apikey") -Method Put -Body ( ConvertTo-Json $device ) -ContentType 'application/json' )
                    }
                    break
                }
            } catch {}
        }

        if ( $new_device -eq $TRUE ) {
            if ( !( $device_type ) ) {
                $device_type = 'Other'
            }

            $device = @{
                "customer_id" = $customer_id;
                "asset_type_id" = 207961;
                "asset_type_name" = "Syncro Other";
                "name" = $new_asset."Device Name";
                "properties" = @{
                    'ip' = $new_asset."IP Address";
                    'mac' = $new_asset."MAC Address";
                    'Location' = $location_id;
                    'Device Type' = $device_type;
                    'Last Updated' = (Get-Date).ToString();
                    'Last Seen' = (Get-Date).ToString()
                }
            }
            Write-Output "Adding $($device.name)"
            Write-Output ( Invoke-RestMethod -Uri "$url/customer_assets?api_key=$apikey" -Method Post -Body ( ConvertTo-Json $device ) -ContentType 'application/json' )
            #$value = Create-Syncro-Ticket -Subject "New Device Found" -IssueType "Other" -Status "New"
            #Create-Syncro-Ticket-Comment -TicketIdOrNumber $value.ticket.id -Subject "New Device Found" -Body ( ConvertTo-Json $device ) -Hidden "false" -DoNotEmail "true"
        }
    }
}

#try { Remove-Item -path "C:\temp\network.txt" } catch {}
#try { Remove-Item -path "C:\temp\WNetWatcher.cfg" } catch {}

Sorry about the wait on this one… I had it posted before - but issues with the forum resulted in me having to delay the release of this:) Please support me at Remove the edit history button and make deleted post really deleted - #7 by travis as this is one of the reason for the delay:) keeping the forums safe and secure is always a must!

1 Like

This looks really interesting. I’m reading through all that you’ve written. Didn’t know about that particular tool from nirsoft. That’s great. They make so many useful tools. Question, does this just compare against itself each time, or does it compare against Syncro assets that already exist in syncro so as to not add them again too?

Does both, but to be clear, it pulls the current list of your customer and then compares with it as it loops over the local list with your online list.

Thanks for your work @travis , I’ve bookmarked this for later.

1 Like