python - Getting the first three bytes of an IP address -
I want to use an IP address string, that is: 1920.168.1.23 but only the first three bytes of the IP address And then add 0-255 I want to convert that IP address to a range of IP addresses. 'I can pass NMP to scan a sweep.
The easiest solution to the curriculum is to touch the last two letters of the string, but certainly this will not work if the IP is 192.168.1.1 or 192.168.1.123.
Here is the solution that came with me:
lhost = "192.168 1.23" # Split everything up. "So first (lip): if (i < Len (lip) - 1): For the radius = = I, the first three parts lips = self.lhost.split ('.') Trange = "" Collect, Val + "." Add "0-255" to the end, Now we have the target category = "XX.XX.XX.0-255" == "0-255" This works fine but it's not ugly and efficient for me What is a better way to do this?
C object can use the refund function.
>> <> Lhost [: lhost .rfind (".")] + ".0-255" '192.168.1.0-255' RFID functions are similarly found (), but end.
rfind (...) S.rfind (sub [, start [, end]] -> will return the highest index in int sri where substrings are found, such as within sub s [Start: end]. Optional arguments being interpreted as starting and ending piece signaling They are. Return to -1 on the failure.
Another complex solution can use regular expressions:
& gt; & Gt; & Gt; Import re & gt; & Gt; & Gt; Re.sub ("\ d {1,3} $", "0-255", lhost) '192.168.1.0-255' Hope this is useful!
Comments
Post a Comment