i’ve got a script i’m working on that needs the user to input two ip addresses (for DNS servers). here is part of the code:
global myDNS1
global myDNS2
on getDns()
set firstQ to "You will need to enter two valid ip addresses to your DNS servers for this to work." & return & "Please enter the first DNS server's ip address: "
set secondQ to "Please enter the second DNS server's ip address: "
set myDNS1 to text returned of (display dialog firstQ default answer "")
set myDNS2 to text returned of (display dialog secondQ default answer "")
end getDns
on checkDns(a)
--not sure how to do this
end checkDns
getDns()
i’d like to call my getDns() and get the user input (done), and then call checkDns(a) for each entry and check to make sure it in a valid ip address form, then give an error if it’s not and recall getDns().
sound simple? i’m not really sure how to do this. i did search, using terms like, “user input” and “‘user input’ validate” but i’m still scratching my head on this one.
global myDNS1
global myDNS2
on getDns()
set firstQ to "You will need to enter two valid ip addresses to your DNS servers for this to work." & return & "Please enter the first DNS server's ip address: "
set secondQ to "Please enter the second DNS server's ip address: "
set myDNS1 to text returned of (display dialog firstQ default answer "")
set myDNS2 to text returned of (display dialog secondQ default answer "")
checkDNS(myDNS1)
checkDNS(myDNS2)
end getDns
on checkDNS(a)
set badAns to a & " is not a valid DNS entry. Please try again."
set ASTD to AppleScript's text item delimiters
set AppleScript's text item delimiters to "."
set howMany to (count text items of a)
set x to 1
repeat while x ≤ howMany
display dialog text item x of a
if text item x of a > 0 and text item x of a < 256 then
set x to (x + 1)
else
display dialog badAns
getDns()
end if
end repeat
end checkDNS
getDns()
however, if the number between the dot’s is less than 3 digits it fails. for example:
123.123.123.123 – works fine!
123.12.312.312 – fails on the ‘12’ → DOAH! 312 is not a valid number. see below.
do i need to pad the numbers with 0’s or is there a better way to do this?
on checkDNS(a)
set badAns to a & " is not a valid DNS entry. Please try again."
set ASTD to AppleScript's text item delimiters
set AppleScript's text item delimiters to "."
set howMany to count (text items of a)
if howMany < 4 then
display dialog "Whoops"
return
end if
set TI to text items of a
set AppleScript's text item delimiters to ASTD
set x to 1
repeat while x ≤ howMany
--display dialog text item x of a
tell (text item x of TI) as number
if it ≥ 0 and it < 256 then
set x to (x + 1)
set msg to "Yay"
else
set msg to badAns
exit repeat
--getDns()
end if
end tell
end repeat
display dialog msg
end checkDNS
checkDNS("123.12.0.123")
ok, here is the final answer for this component of the program:
global myDNS1
global myDNS2
on getDns()
set firstQ to "You will need to enter two valid ip addresses to your DNS servers for this to work." & return & "Please enter the first DNS server's ip address: "
set secondQ to "Please enter the second DNS server's ip address: "
set myDNS1 to text returned of (display dialog firstQ default answer "")
set myDNS2 to text returned of (display dialog secondQ default answer "")
checkDNS(myDNS1)
checkDNS(myDNS2)
end getDns
on checkDNS(a)
set badAns to a & " is not a valid DNS entry. Please try again."
set ASTD to AppleScript's text item delimiters
set AppleScript's text item delimiters to "."
set howMany to (count text items of a)
if howMany is not 4 then
display dialog badAns
getDns()
return
end if
set TI to text items of a
set AppleScript's text item delimiters to ASTD
set x to 1
repeat while x ≤ howMany
tell (text item x of TI) as number
if it ≥ 0 and it < 256 then
set x to (x + 1)
else
display dialog badAns
getDns()
exit repeat
end if
end tell
end repeat
end checkDNS
getDns()
i’ll work in getting some DNS numbers from /etc/resolv.conf or from scutil also–that’s a really good idea.
on checkDNS(a)
set badAns to a & " is not a valid DNS entry."
set ASTD to AppleScript's text item delimiters
set AppleScript's text item delimiters to "."
set blocks to text items of a
set howMany to count blocks
if howMany < 4 then
display dialog a & " is not valid." & return & return & "IP addresses must have four groups"
return
end if
set AppleScript's text item delimiters to ASTD
repeat with k from 1 to 4
tell item k of blocks as number to if it ≥ 0 and it < 256 then
set msg to "Yay"
else
set msg to badAns & return & return & "Block #" & k & " is out of range" & return & return & "Using numbers between 0 and 255, try again"
exit repeat
end if
end repeat
display dialog msg
end checkDNS
set IPs to (choose from list {"123.233.4.1", "43.33.99", "27.49.259.0"}) as string
checkDNS(IPs)
Might want to reset the delimiters before you return.
Edit:
on checkValidIPAddress(address)
local address, octets, i
set ASTID to AppleScript's text item delimiters
set AppleScript's text item delimiters to {"."}
set octets to text items of address
set AppleScript's text item delimiters to ASTID
if (count octets) is not 4 then error "An IP address must have four groups." number 5 -- or return false
repeat with i from 1 to 4
tell (item i of octets as integer) to if not (it ≥ 0 and it ≤ 255) then
error "Group " & i & " is out of range. Each group must be between 0 and 255." number i -- or return false
exit repeat
end if
end repeat
--return true
end checkValidIPAddress
try
choose from list {"123.233.4.1", "43.33.99", "27.49.259.0"}
checkValidIPAddress("" & result)
on error errMsg number errNum
display dialog "Error " & errNum & ":" & return & return & errMsg buttons {"Ok"} default button 1 with icon note
end try