UPDATE!! ==> fingscan & fingscaninstall.app now posted on
www.copy.com in shared folder
the apps are shared on this drive
https://copy.com/ak4zXRrPUccVmR4hcode should be ready for general use.
Karl .. ready to go for a swim..
a) copy code and compile locally:
0. install fing from overlooksoft.com
copy applescript code:
1. select file1(fingscaninstall) copy code open applescript and paste code
-save as fingscaninstall on desktop
- export as app on desktop
2, select 2. file(fingscan) copy code open applescript and paste code
-save as fingscan on desktop (must be the same as above)
- export as app on desktop with option stay running after run handler
3. doubleclick on fingscaninstall.app
b) with download from shared drive,
steps to install
0 install fing from overlooksoft.com
1. download the posted 2 files
2. double click on fingscaninstall in downloads
3. click ok ...
=======
result:
- fingscan.app will be copied into ../Indigo 6 or 5 or 4/scripts/attachments/fingscan.app
- it will be launched, you have to enter your password to enable fing.bin to run
- a scheduled action is created : fingscanrelaunch it will run once a day to launch fingscan if not running.
- fingscan will after 10 seconds scan your network and present to you enter nicknames for each device
it will create variables ipDevicesnn with information for each device e.g.:
HP-Printer;2C:76:8A:BF:51:A1;192.168.1.164;HP;up;2013/09/12 02:43:12;2
nickname;mac#;ip#;vendor;timestamp-of-lastchange;#of-up/down
in addition a a timestamp variable ipDevsLastupdate gives the time of last change in network
and ipDevsAlive flips between 1 and -1 every time is checks the network
new devices or devices coming back from sleep will be detected in ~ 5 secs,
if a devices go to sleep it will take ~ 1 minute to update indigo.
you can edit the variables values (ie change the nickname) but MUST NOT change ";mac#;" the 2C:76...
your can setup a trigger to use the info of eg ipDevice15:
if variable ipDevice15 has any change, and variable contains ";down" then do your action eg say " ip device 22 is down"
and attached the files:
1. fingscaninstall
- Code: Select all
-- fingscaninstall into indigo folder (copied and adapted from Nathan Sheldon etrack.app)
-- Karl Wachs
-- v0.2
-- v0.2 formatting changes
--
-- Sept 12 2013
-- required:
-- download fingscanapp and installfingscan.app into ~/Downloads/: just download the files in safari. they should end up there, then doubleclick on fingscaninstall
-- what it does:
-- checks directories which indigo version is installed
-- copies into latest version
-- starts fingscan
-- creates a scheduled action "fingscanreload" to start fingscan,(fingscan checks if already running and quits if second instance)
--
set lf to ASCII character 10
set theApp to "fingscan.app"
set install to true
set userName to short user name of (system info)
-- Verify that the "theAPP" Indigo Attachment file exists in the same folder as this program.
set myPath to (path to me) as text
set tid to AppleScript's text item delimiters
set AppleScript's text item delimiters to ":"
set chunks to text items of myPath
if last item of chunks is "" then set chunks to chopLast(chunks)
-- the above accounts for script applications that are package folders
set myFolderPath to chopLast(chunks) as text
set myFolder to POSIX path of myFolderPath & "/" -- this is where this application is located, fingscan.app must be in teh same folder
set AppleScript's text item delimiters to tid
set theAPPfile to myFolder & theApp -- and the full path with app
set msg to "no"
tell application "Finder" to if exists theAPPfile as POSIX file then set msg to "yes"
if msg = "no" then
tell me
display dialog "FINGSCAN INSTALL" & lf & "The " & theApp & " Indigo attachment file could not be found in the same folder as this setup program. In order to install " & theApp & " into your Indigo system, the " & theApp & " file that was included with this setup program must be in the same folder as this program." buttons {"OK"} default button 1
return false
end tell
end if
-- all surces in right place
-- find indigo folder
set theIndigoFolder to ""
tell application "Finder"
-- find newest of the supported Indigo version.
set ii to 20 -- start with indigo version 20… yes, we are optimistic!!
repeat while ii > 1
set theFolder to "/Library/Application Support/Perceptive Automation/Indigo " & ii & "/IndigoServer.app"
if exists theFolder as POSIX file then
set theIndigoFolder to "/Library/Application Support/Perceptive Automation/Indigo " & ii & "/"
set ii to 0
end if
set ii to ii - 1
end repeat
end tell
set destinationFolder to theIndigoFolder & "Scripts/Attachments/"
tell application "Finder"
set destinationFile to destinationFolder & "fingscan.app"
set install to true
try -- See if the file already exists in the installation location.
set destinationFinderFile to (POSIX file destinationFile) as Unicode text
tell me
set overwriteOrSkip to button returned of (display dialog "FINGSCAN INSTALL" & lf & destinationFolder & " is already installed. Click \"Overwrite\" to re-install the attachment or \"Skip\" to skip the installation of the " & theApp & " attachment." buttons {"Overwrite", "Skip"} default button 2)
end tell
if overwriteOrSkip is "Skip" then
set install to false
end if
on error -- Meaning the file does NOT already exist in the installation location.
set install to true
end try
-- Install the theAPP Indigo Attachment if we'resupposed to.
if install is true then
tell me
set installChoice to button returned of (display dialog " FINGSCAN INSTALL" & lf & " We're now ready to install. " buttons {"Cancel", "OK"} default button 2)
end tell
if installChoice is "OK" then
do shell script "cp -R " & quoted form of theAPPfile & " " & quoted form of destinationFolder & " & " -- copy to indigo folder
try -- just in case its already there
tell application "IndigoServer"
make new time date action with properties {name:"fingscanrelaunch", days of week:"1111111"}
end tell
end try
try -- just in case its already there
tell application "IndigoServer"
set action type of action step 1 of time date action "fingscanrelaunch" to executeScript
set script code of action step 1 of time date action "fingscanrelaunch" to cmd
end tell
end try
tell me
activate
display dialog "FINGSCAN INSTALL" & lf & " is installed, ready to be launched, " & lf & " and scheduled action fingscanrelauch will run once a day to (re)start fingscan " buttons {"OK"} giving up after 60
end tell
do shell script "open '" & destinationFolder & theApp & "' &" -- and start it
set cmd to "do shell script \"open '" & destinationFolder & theApp & "' & \" "
else
tell me
activate
display dialog "FINGSCAN INSTALL" & lf & "has been canceled." buttons {"OK"} default button 1 giving up after 60
return false
end tell
end if
end if
end tell
return true
end
on chopLast(theList)
return reverse of (rest of (reverse of theList))
end chopLast
2. fingscan
- Code: Select all
-- fingscan.app
-- build list of network devices using FING
-- runs in background, iddles for 5 secs then checks if FING has found new configs
-- creates variables with ipdevice info
--
--
-- Karl Wachs
-- Sep 12 2013
-- please use as you see fit
-- V 1.0
-- 0.81 one code fix, if IP number change: wrong quotes for variable
-- 0.82 added comments, clearer code, and reset logfile once a day
-- 0.9 added check logfile for any change, if no chang skip checks, faster reaction, and less code excution, most of the time nothing changes. this will only check size of file "finglog"
-- 0.95 check for change in fingdata file. if time stamp changed get all data and update indigo (1/minute)
-- adding a device results in an update of indigo within 6 secs, detection of disconnecting a debvice takes up to 65 secs. connecting sends traffic, disconnecting in most cases no, have to wait until next ping cycle (1/minute)
-- 0.97 add 4 password options (set passWordOption to 0 or 1 or 2 or 3 see code for details
-- 0.971 OOI variable fix,
-- 0.973 set lastLogFilesize to logFilesize was at the wrong place,
-- bad logic with logMACaddress fixed (was old code from old logic)
-- add heartbeat (-1 / 1) display in indigo everytime it runs
-- change ipDevsLastUpdate to last time variables were fully checked or any new device added etc
-- 0.98 added counter of number of up/down of each device
-- 0.99 added "activate" before display dialog, to make sure pop windows for new devices goes into foreground
-- fixed situation when indigoNoOfChanges is not a number, set to 0 (try .. on error set … to 0)
-- 1.0 added & clarified comments and fixed spelling
-- check if fingscan is already running ==> quit
--
-- how to install:
-- copy this script into applescript editor, save into .../indigo 6/scripts/attachments
-- export into …/indigo 6/scripts/attachments as APP with "stay open after run handler.."
-- then start fingscan using finder (doubleclick on fingscan.app in /indigo 6/scripts/attachments)
--
-- use in indigo as trigger:
-- setup trigger "IPdevice22 down"
-- trigger if any change to ipDevice22
-- condition ipDevice22 must contain ";down" "down" (without ";" ) probably also works, but you might have a device's name up or down or vendor's name may contain string up or down
-- action: whatever you like
--
-- what it does:
-- starts fing if not active
-- creates fing directory in ~/Documents/ for data
-- will ask for approval to use keychain for sudofing command / userid .. see password options
-- will run every 5 seconds and check finglog file size, if changed check fingdata file for new info
-- will create/ update varibales ipDevicenn nn = 01 .. 99
-- will update ipDevsLastUpdate with last date/time of last change
-- will update ipDevsAlive toggle 1/-1 everytime it run as heat beat
-- will ask for each new MAC address to assign nickname
-- ipDevicenn will contain nickname;macaddress;ip#;vendor;up/downinfo;date of last change if any;#of"up/downs"
-- eg:iPHONE4-dad;14:8F:C6:74:87:58;192.168.1.148;Apple;up;2013/09/12 00:28:11;2
-- fing will stay active and will create log file and data file. log file will continuously increase (fing will reset at ~ 350kb). finglog file is checked for changed file size, fingdata file is analzed by this script once it changes
-- regular read seems to conflict with fing, we use cat with pipe into applescript variable.
--
-- needs to be fixed:
-- does not allow non consequtive ipDevice03 ..05 eg 04 missing by eg deleting a variable
-- tried to put fing output files into indigo directory, but fing does not like spaces in directories, so i put the outputfiles into User/yourid/Documents/fing/ fingdata and finglog
--
--
-- added 4 password options
-- 0. set password in code
-- 1. ask for password in dialog box
-- 2. use keychain to get password
-- 3. sudo without password works only o=if fing has been enabled to run without password, see option 3 in code what to do.
-- uses ~ 0.1-2% of one cpu (mostly 0.1%)
on quit -- enable quit
continue quit
end quit
on run -- this is done only once at time of "open"
global debug
global theDir
global userName
global theFile
global theLog
global lastLogFilesize
global logFilesize
global noOfDevices
global FingContents
global noOfIndigoDevices
global IndigoIpDevices
global lastUpdate
global passwordOption
global yourPassword
global alive
set yourPassword to "" -- use this if paaswordOption = 0
set passwordOption to 3 -- 0= set password in code, 1= use keychain to get password, 2 = enabel fing to not require password, 3 = enter password when running
set userName to short user name of (system info)
set theDir to "/Users/" & userName & "/Documents/fing/"
set theFile to "fingdata"
set theLog to "finglog"
set lastLogFilesize to 0
set logFilesize to 0
set newDevice to 0
set lastUpdate to ""
set alive to 1
-- quit if already running
try
set theResponse to do shell script "ps aux | grep 'fingscan' | awk '{print $2}' "
end try
-- if pgmToFind is running then theResponse has 3 lines with PIDs from : ps, fingscan itself and one more --- have not figured out which one..
set ii to count words of theResponse
if ii > 3 then quit -- run just one instance
-- end quit if already running
-- init if needed
setupFing()
-- send to idle
idle
-- finish main pgm from now on on idle takes over
end run
on idle -- every 5 seconds this gets executed
global theDir
global userName
global theFile
global theLog
global lastLogFilesize
global logFilesize
global newDevice
global noOfDevices
global FingContents
global noOfIndigoDevices
global IndigoIpDevices
global lastUpdate
global anyChange
global alive
set anyChange to 0
set noOfLines to 0
set AppleScript's text item delimiters to {";"}
-- update indigo with time stamp
set theTime to text -8 thru -1 of ((current date) as «class isot» as string)
-- check file size of logfile, if its larger than last time, new data
set cmd to "ls -l " & theDir & theLog & "| awk '{print $5}'"
set logFilesize to do shell script cmd
if logFilesize > lastLogFilesize then -- there must be a new entry in finglog
set lastLogFilesize to logFilesize
-- read latest fingdata log file (just last line)
getFingIPlogdata()
if noOfDevices > 0 then -- read not successful .. must be more than 0 devices changed on network
-- yes there is really new data to use:
-- use last line of FINGlog for new information
set nextLine to paragraph 1 of FingContents
set theItems to every text item of nextLine
set theLOGIPNumber to item 3 of theItems as text
set theLOGStatus to item 2 of theItems as text
set theLOGDate to item 1 of theItems as text
set theLOGmacAddress to item 6 of theItems as text
set theLOGVendor to item 7 of theItems as text
-- get all devices stored in indigo into IndigoIpDevices, same format as from FING
getIndigoIPDEvices()
-- compare to indogo info
set newDevice to 0
compareToIndigo(theLOGmacAddress, theLOGIPNumber, theLOGVendor, theLOGStatus, theLOGDate)
end if
end if
-- check if fingdata was updated (once a minute)
set cmd to "ls -l " & theDir & theFile & "| awk '{print $8}'" -- time stamp of last change of file
set thisUpdate to do shell script cmd
if not (thisUpdate = lastUpdate) then -- updated FINGdata file
set lastUpdate to thisUpdate -- remember this time stamp
-- read latest fingdata file
getFingIPData()
if noOfDevices > 1 then -- read not successful .. must be more than 1 devices on network
-- get all devices stored in indigo into IndigoIpDevices, same format as from FING
set newDevice to 0
getIndigoIPDEvices()
-- all data read now compare if anything new
set ii to 0
-- loop through FING data
repeat while ii < noOfDevices
set ii to ii + 1
set nextLine to paragraph (ii) of FingContents
set theItems to every text item of nextLine
set themacAddress to item 6 of theItems as text
set theIPNumber to item 1 of theItems as text
set theStatus to item 3 of theItems as text
set theDate to item 4 of theItems as text
set theVendor to item 7 of theItems as text
-- set theAssignedName to item 5 of theItems as text -- not used
-- set theHostName to item 2 of theItems as text -- not used
compareToIndigo(themacAddress, theIPNumber, theVendor, theStatus, theDate)
end repeat
end if
end if
-- house keeping, update ipDeviceLastUpdate and reset logfile once a day
-- update indigo with time stamp and heat beat
if anyChange > 0 then
try -- just in case indigo is down, dont crash this program
tell application "IndigoServer"
set the value of variable "ipDevsLastUpdate" to theTime
end tell
end try
end if
set alive to -alive -- flip everytime fingscan runs
try -- just in case indigo is down, dont crash this program
tell application "IndigoServer"
set the value of variable "ipDevsAlive" to alive
end tell
end try
return 5 -- idle for 5 seconds
end idle
on setupFing()
global theDir
global theFile
global theLog
global userName
global theIndigoFolder
global passwordOption
global yourPassword
--check if fing is already running, if not start it.
set theApp to "fing.bin"
set theResponse to " "
-- create fing data folder
set cmd to "mkdir '" & theDir & "' >/dev/null 2>&1 & "
do shell script cmd
-- check if fing.bin is running
try
set theResponse to do shell script "ps aux | grep '" & theApp & "' | awk '{print $2}' "
end try
-- if pgmToFind is running then theResponse has 3 lines with PIDs from : ps, fing itself and one more --- have not figured out which one..
set ii to count words of theResponse
if ii < 3 then -- if not running start script, start fing
-- command looks like: " xx sudo xx fing -o table,csv,"Users/yourid/Documents/fing/fingdata" log,csv,"Users/yourid/Documents/fing/finglog" >/dev/null 2>&1 & " with some variations on xx sudo xx regarding password depending on option
-- option 0: set thePassword in code
if passwordOption = 0 then
set cmd to "echo " & yourPassword & " | sudo -S fing -o table,csv,\"" & theDir & theFile & "\" log,csv,\"" & theDir & theLog & "\" >/dev/null 2>&1 &" -- start fing, send to background, dont wait, create 2 output files: one table format file and one logfile
set yourPassword to "" -- dont keep password around
end if
-- option 1: have to use sudo: pipe password from getPW() into sudo
if passwordOption = 1 then
set yourPassword to getPW(userName) -- this has to be looked at, grabs first occurence of userName, if 2 with same username and different passwords this might fail
set cmd to "echo " & yourPassword & " | sudo -S fing -o table,csv,\"" & theDir & theFile & "\" log,csv,\"" & theDir & theLog & "\" >/dev/null 2>&1 &" -- start fing, send to background, dont wait, create 2 output files: one table format file and one logfile
set yourPassword to "" -- dont keep password around
end if
-- option 2: modify system setting so that fing does not need sudo from gcoop76 indido discussions
(*
in terminal do:
sudo visudo
add this line:
spanky localhost = NOPASSWD: /usr/bin/fing
That is, if "spanky" is your username (the one who will be executing the applescript and calling fing).
Note that this edits the file in "vi." Use the arrow keys or "j" or "k" to move up and down. Go to the line you want to add the above text and hit "i" for insert. Type or paste it out, then hit "esc." Then hit ":" then type "wq" for "write, then quit"
That should do it. It is important to use visudo to edit your sudoers file since apparently bad things can happen to the permissions of the file if you edit it directly. I just tested it on my Mac OS X Mountain Lion laptop. Works well. Everything else still needs a password for sudo, but not fing.
*)
if passwordOption = 2 then
set cmd to " sudo fing -o table,csv,\"" & theDir & theFile & "\" log,csv,\"" & theDir & theLog & "\" >/dev/null 2>&1 &" -- start fing, send to background, dont wait, create 2 output files: one table format file and one logfile
set yourPassword to "" -- dont keep password around
end if
--option 3 user will need to enter password
if passwordOption = 3 then
tell application "Finder" -- need to go through finder in case this runs in the background
activate
set yourPassword to the text returned of (display dialog "FINGSCAN: enter your password" default answer "xxyyzz" giving up after 200)
end tell
set cmd to "echo " & yourPassword & " | sudo -S fing -o table,csv,\"" & theDir & theFile & "\" log,csv,\"" & theDir & theLog & "\" >/dev/null 2>&1 &" -- start fing, send to background, dont wait, create 2 output files: one table format file and one logfile
set yourPassword to "" -- dont keep password around
end if
-- execute option 0,1,2,3
do shell script cmd
delay 10 --wait until FING has finished at least once
end if
-- fing is running or has started
try -- just in case indigo is down, dont crash this program
set theTime to text -8 thru -1 of ((current date) as «class isot» as string)
tell application "IndigoServer"
if not ((exists variable "ipDevsLastUpdate")) then
make new variable with properties {name:"ipDevsLastUpdate", value:theTime}
end if
if not ((exists variable "ipDevsAlive")) then -- heat beat flip everytime it runs
make new variable with properties {name:"ipDevsAlive", value:"-1"}
end if
end tell
end try
return
end setupFing
on getIndigoIPDEvices()
global noOfIndigoDevices
global IndigoIpDevices
-- get all devices stored in indigo into IndigoIpDevices, same format as from FING
set AppleScript's text item delimiters to {";"}
set ii to 0
set noOfIndigoIpDevices to "0"
set IndigoIpDevices to {}
set noMore to false
repeat while ii < 999
set ii to ii + 1
set OOii to ii
if ii < 10 then set OOii to "0" & ii as text
set ipDevice to "ipDevice" & OOii
tell application "IndigoServer"
if not ((exists variable ipDevice)) then
set noMore to true
set noOfIndigoDevices to ii - 1 -- this is the number of ipDevices currently in indigo
set ii to 99999 -- exit repeat
end if
end tell
if noMore = false then
try -- just in case indigo is down, dont crash this program
tell application "IndigoServer"
set ipDeviceValue to (get value of variable ipDevice) as text
end tell
end try
set IndigoIpDevices to IndigoIpDevices & ipDeviceValue -- add next ipDevice to end of list
end if
end repeat
return
end getIndigoIPDEvices
on compareToIndigo(themacAddress, theIPNumber, theVendor, theStatus, theDate) -- this is the data from fing to be compared to indigo
global noOfIndigoDevices
global IndigoIpDevices
global newDevice
global anyChange
set anyChange to 1 -- if called there is a change, remember for timestamp variable of last yupdate
-- check if any match in indigo data
set sameMAC to 0
set jj to 0
repeat while jj < noOfIndigoDevices
set jj to jj + 1
set nextLineIndigo to item jj of IndigoIpDevices as text
set theIndigoItems to every text item of nextLineIndigo -- this is all info of ONE indigoIpDevice
set indigomacAddress to trim(item 2 of theIndigoItems as text) -- this is the indigo mac address for the ipDevice
if themacAddress = indigomacAddress then ---- mac number found, already in indigo… exit repeat
set sameMAC to jj -- this is the device that is the same
set jj to 999999 -- force repeat exit
set indigoIPNumber to trim(item 3 of theIndigoItems as text) -- get the indigo info for later use
set indigoStatus to trim(item 5 of theIndigoItems as text)
set indigoVendor to trim(item 4 of theIndigoItems as text)
set indigoNick to trim(item 1 of theIndigoItems as text)
try
set indigoNoOfChanges to 0 -- make sure that this becomes a number, not text, if empty make it 0
set indigoNoOfChanges to trim(item 7 of theIndigoItems as text)
set indigoNoOfChanges to indigoNoOfChanges + 0
on error
set indigoNoOfChanges to 0
end try
end if
end repeat
if sameMAC > 0 then -- found match in MAC number, now check if anything has changed
if (theIPNumber = indigoIPNumber) and (theStatus = indigoStatus) then -- both the same
-- do nothing
else -- ip or status has changed, update indigo variable
set OOii to sameMAC
if sameMAC < 10 then set OOii to "0" & sameMAC as text -- make sequential number 2 digits
set theDeviceNo to "ipDevice" & OOii -- eg ipDevice06
set theDeviceValue to indigoNick & ";" & themacAddress & ";" & theIPNumber & ";" & theVendor & ";" & theStatus & ";" & theDate & ";" & indigoNoOfChanges + 1 -- this will be written back to indigo
try -- just in case indigo is down, dont crash this program
tell application "IndigoServer"
set the value of variable theDeviceNo to theDeviceValue
end tell
end try
end if
else -- new device, set up in indigo
set newDevice to newDevice + 1
set OOii to noOfIndigoDevices + newDevice
if OOii < 10 then set OOii to "0" & OOii as text -- make sequential number 2 digits
set theDeviceName to "ipDevice" & OOii -- eg ipDevice06
set theText to "FINGSCAN network has found new device, please enter nickname for new device : " & theDeviceName & " " & themacAddress & " " & theIPNumber & " " & theVendor & " " & theStatus & " eg:"
tell application "Finder" -- need to go through finder in case this runs in the background
activate
set nickname to the text returned of (display dialog theText default answer "phone-xyz" giving up after 60)
end tell
set theDeviceValue to nickname & ";" & themacAddress & ";" & theIPNumber & ";" & theVendor & ";" & theStatus & ";" & theDate & ";1" -- this will be written back to indigo
try -- just in case indigo is down, dont crash this program
tell application "IndigoServer"
make new variable with properties {name:theDeviceName, value:theDeviceValue}
end tell
end try
end if
end compareToIndigo
on getFingIPData()
global noOfDevices
global FingContents
global theDir
global theFile
set FingContents to {}
-- read latest fingdata file with unix command cat, tryed normal applescript read, but fing did not like that, this one seems to be less intrucive
set cmd to "cat " & theDir & theFile
try
set FingContents to (do shell script cmd)
set noOfDevices to (count paragraphs of FingContents)
end try
return
end getFingIPData
on getFingIPlogdata()
global noOfDevices
global FingContents
global theDir
global theFile
global theLog
set FingContents to {}
set cmd to "tail -1 " & theDir & theLog -- just get the last line ie the new /changed device only
try
set FingContents to (do shell script cmd)
set noOfDevices to (count paragraphs of FingContents)
end try
end getFingIPlogdata
on getPW(keychainItemName) -- get password from keychain for userid: keychainitemname .. if there are 2 userids that are the same in keychain, it might get the password of the wrong id
do shell script "security 2>&1 >/dev/null find-generic-password -gl " & quoted form of keychainItemName & " | awk '{print $2}'"
return (text 2 thru -2 of result) -- return the password part of the string
end getPW
-- trim blanks from string
on trim(someText)
if someText = "" then return someText
if someText = " " then return "" -- drop spaces
repeat until someText does not start with " "
set someText to text 2 thru -1 of someText
end repeat
repeat until someText does not end with " "
set someText to text 1 thru -2 of someText
end repeat
return someText
end trim