Jump to content

_StringIPIsValid(); _StringIPPTR


SmOke_N
 Share

Recommended Posts

  • Moderators

I got a headache doing the RegExp on this one, it seems so simple looking at it now, but to be honest, I'm not 100% sure on the validity of it, I just know it passed all my own personal tests. Should work on 0-255.

Vaildate just the string type as an IP:

Func _StringIPTypeValid($sIP)
    If StringRegExp($sIP, "(?:(\d|[1-9]\d|1\d{2}|2[0-4]\d|25[0-5])(\.)){3}(?:(25[0-5]$|2[0-4]\d$|1\d{2}$|[1-9]\d$|\d$))") Then Return 1
    Return SetError(1, 0, 0)
EndFuncoÝ÷ Ú)Üç^اzÖ®Þgyç^uV¥Ö­zØ^²Úâr¥ê+­ën®{azêå

Edit:

Just realized I put Ping in the title description, obviously that's not being used... :)

Edited by SmOke_N

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Link to comment
Share on other sites

@SmOke_N

#include <array.au3>
Dim $MyString1
$MyString1 = "junk blah blah blah 192.168.1.1 and other junk 32522344 68.3.1.1 my04 22dd---48s9s 100.260.2.4 300.22.22.1"
$answer5 = StringRegExp($MyString1,'((25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0-9]|[1-9])\.(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0-9]|[1-9])\.(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0-9]|[1-9])\.(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0-9]|[1-9]))',3)
For $i = UBound($answer5) -1 to 0 step - 1
;MsgBox(0,"mod $i",Mod($i,5) & "   :   " & $answer5[$i])
if Mod($i,5) <> 4 Then
_ArrayDelete($answer5,$i)
EndIf
Next
_ArrayInsert($answer5,0,UBound($answer5))
_arraydisplay($answer5,"$answer5")

Here is an other one I grabbed somewhere.

Enjoy !!

ptrex

Link to comment
Share on other sites

  • Moderators

@SmOke_N

#include <array.au3>
Dim $MyString1
$MyString1 = "junk blah blah blah 192.168.1.1 and other junk 32522344 68.3.1.1 my04 22dd---48s9s 100.260.2.4 300.22.22.1"
$answer5 = StringRegExp($MyString1,'((25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0-9]|[1-9])\.(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0-9]|[1-9])\.(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0-9]|[1-9])\.(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0-9]|[1-9]))',3)
For $i = UBound($answer5) -1 to 0 step - 1
;MsgBox(0,"mod $i",Mod($i,5) & "   :   " & $answer5[$i])
if Mod($i,5) <> 4 Then
_ArrayDelete($answer5,$i)
EndIf
Next
_ArrayInsert($answer5,0,UBound($answer5))
_arraydisplay($answer5,"$answer5")oÝ÷ Øw«z+azº'x+i¶ÞvÊ&{^­á'·ß}鶷±þ«¨µí÷Þ­éín*ݲ«xay%¢,)Ê«²Z(Çè®+-®)à'¢b¢wbv}ý¶ØbKhvajÙ¢è¬ë-z+az»Ú')දjXܨ®ö¥Ö­x-ãÓDay0¯z¼+¢×­«pjƵêì¢g­)à)¶¬¶.®¶­sb6æ6ÇVFRfÇC¶'&æS2fwC°¢b33c·57G&ærÒgV÷C¶§Væ²&Æ&Æ&Æ"ãcããæB÷FW"§Væ²3#S##3CBcã2ãã×B#&FBÒÒÓC32ã#cã"ãB3ã#"ã#"ãgV÷C°¢b33c¶Òõ7G&ætvWD'&b33c·57G&ær¥ô'&F7Æb33c¶Âb33²b33² ¤gVæ2õ7G&ætvWD'&b33c·57G&ær Æö6Âb33c¶'&Ò7G&æu&VtWb33c·57G&ærÂgV÷C²#U³ÓUÒb3#²çÃ%³ÓEÒb3#¶Bb3#²çÃb3#¶Bb3#¶Bb3#²çųÓÒb3#¶Bb3#²çųÓÒb3#²â³7Ò#U³ÓU×Ã%³ÓEÒb3#¶GÃb3#¶Bb3#¶GųÓÒb3#¶GųÓÒgV÷C²Ã2 bæ÷B4'&b33c¶'&FVâ&WGW&â6WDW'&÷"ÂÂb33²b33² Æö6Âb33c¶öÆD'& f÷"b33c¶42ÒFòT&÷VæBb33c¶'&Ò7FW0 b33c¶öÆD'&f׳Òb33c¶'&²b33c¶45Òfײ6" æW@ bb33c¶öÆD'&FVâ&WGW&â7G&æu7ÆB7G&æuG&Õ&vBb33c¶öÆD'&ÂÂ6" &WGW&â6WDW'&÷""ÂÂb33²b33²¤VæDgVæ0

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Link to comment
Share on other sites

@SmOke_N: I cannot seem to get this to work. I get the same thing whether it is a good IP or not. Am I missing something? Maybe you could provide an easy to follow sample. Thank you. BTW, you may want to turn turn off smileys on your first post. I don't think you wanted a frowny face in the middle of your code. :-)

#include <GUIConstants.au3>

#Region ### START Koda GUI section ### Form=G:\My Programs\Daylight Saver\frmIPRange.kxf
$frmIPRange = GUICreate("Day Saver", 219, 113, 193, 115)
$inputIPRangeBegin = GUICtrlCreateInput("", 8, 40, 89, 21)
$inputIPRangeEnd = GUICtrlCreateInput("", 120, 40, 89, 21)
GUICtrlCreateLabel("Please enter a valid IP range", 8, 16, 140, 17)
GUICtrlCreateLabel("to", 104, 43, 13, 17)
$btnIPRangeOK = GUICtrlCreateButton("OK", 72, 72, 75, 25, 0)
GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###

While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit
        Case $btnIPRangeOK
            $boolIPOK = 1
            $strIPRangeBegin = GUICtrlRead($inputIPRangeBegin)
            _StringIPTypeValid($strIPRangeBegin)
            If @error = 1 Then 
                $boolIPOK = 0
                MsgBox(16,"Day Saver", "Please input a valid beginning IP address in the form of xxx.xxx.xxx.xxx")
            EndIf
            $strIPRangeEnd = GUICtrlRead($inputIPRangeEnd)
            _StringIPTypeValid($strIPRangeEnd)
            If @error = 1 Then
                $boolIPOK = 0
                MsgBox(16,"Day Saver", "Please input a valid ending IP address in the form of xxx.xxx.xxx.xxx")
            EndIf
            If $boolIPOK = 1 Then
                GUIDelete($frmIPRange)
                ExitLoop
            EndIf
    EndSwitch
WEnd

; IP Validater thanks to SmOke_N on the AutoIT forums
Func _StringIPTypeValid($sIP)
    If StringRegExp($sIP, "(?:(\d|[1-9]\d|1\d{2}|2[0-4]\d|25[0-5])(\.)){3}(?:(25[0-5]$|2[0-4]\d$|1\d{2}$|[1-9]\d$|\d$))") Then Return 1
    Return SetError(1, 0, 0)
EndFunc
Link to comment
Share on other sites

http://www.regular-expressions.info/regexb...atecapture.html

-S

(Yet Another) ExcelCOM UDF"A human being should be able to change a diaper, plan an invasion, butcher a hog, conn a ship, design a building, write a sonnet, balance accounts, build a wall, set a bone, comfort the dying, take orders, give orders, cooperate, act alone, solve equations, analyze a new problem, pitch manure, program a computer, cook a tasty meal, fight efficiently, die gallantly...[indent]...specialization is for insects." - R. A. Heinlein[/indent]
Link to comment
Share on other sites

  • Moderators

Just saw this... I stripped the white spaces from your GUICtrlRead() as you didn't provide the "valid" ip addresses you were doing the range on.

I used 192.168.0.1 To 192.168.0.255 and it worked fine.

#include <GUIConstants.au3>
#Region ### START Koda GUI section ### Form=G:\My Programs\Daylight Saver\frmIPRange.kxf
$frmIPRange = GUICreate("Day Saver", 219, 113, 193, 115)
$inputIPRangeBegin = GUICtrlCreateInput("", 8, 40, 89, 21)
$inputIPRangeEnd = GUICtrlCreateInput("", 120, 40, 89, 21)
GUICtrlCreateLabel("Please enter a valid IP range", 8, 16, 140, 17)
GUICtrlCreateLabel("to", 104, 43, 13, 17)
$btnIPRangeOK = GUICtrlCreateButton("OK", 72, 72, 75, 25, 0)
GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###

While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit
        Case $btnIPRangeOK
            $boolIPOK = 1
            $strIPRangeBegin = StringStripWS(GUICtrlRead($inputIPRangeBegin), 8)
            _StringIPTypeValid($strIPRangeBegin)
            If @error = 1 Then
                $boolIPOK = 0
                MsgBox(16,"Day Saver", "Please input a valid beginning IP address in the form of xxx.xxx.xxx.xxx")
            EndIf
            $strIPRangeEnd = StringStripWS(GUICtrlRead($inputIPRangeEnd), 8)
            _StringIPTypeValid($strIPRangeEnd)
            If @error = 1 Then
                $boolIPOK = 0
                MsgBox(16,"Day Saver", "Please input a valid ending IP address in the form of xxx.xxx.xxx.xxx")
            EndIf
            If $boolIPOK = 1 Then
                MsgBox(64, 'Info', 'Valid IP')
                GUIDelete($frmIPRange)
                ExitLoop
            EndIf
    EndSwitch
WEnd

; IP Validater thanks to SmOke_N on the AutoIT forums
Func _StringIPTypeValid($sIP)
    If StringRegExp($sIP, "(?:(\d|[1-9]\d|1\d{2}|2[0-4]\d|25[0-5])(\.)){3}(?:(25[0-5]$|2[0-4]\d$|1\d{2}$|[1-9]\d$|\d$))") Then Return 1
    Return SetError(1, 0, 0)
EndFunc
Edited by SmOke_N

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Link to comment
Share on other sites

  • 2 weeks later...
  • Moderators

Do I need the beta for this or something? I am using 3.2.0.1. I copied your code and it didn't work. Same thing, It always bounces back as invalid. Sorry about the late reply, I am not getting my e-mail notifications for some reason.

I wrote it using 3.2.2.0, I have no idea if StringRegExp was broken in 3.2.0.1. But, again you've not posted what you are trying to do (An actual IP range you are putting in). I took the spaces out of the GUICtrlRead() in the last example just to make sure you weren't using them. Every time I've ran it, it's been fine. Edited by SmOke_N

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...