Open-AudIT
https://www.open-audit.org/phpBB3/

strip the leading zero in IP address in host lists
https://www.open-audit.org/phpBB3/viewtopic.php?f=9&t=3164
Page 1 of 1

Author:  kilgor [ Sat Feb 14, 2009 6:35 am ]
Post subject:  strip the leading zero in IP address in host lists

openaudit for some reason lists IP's with a leading zero. Whatever the reason for this, it doesn't help in any way.

Example:

192.168.143.048

Should be:

192.168.143.48

I often copy paste the lists to use with psexec, I then have to additionally reformat the IP address (sed s/"\.0"/"."/g). At least when used with psexec, .048 is not the same as .48.


Thanks!

Author:  Walter1981 [ Thu Mar 26, 2009 6:24 pm ]
Post subject:  Re: strip the leading zero in IP address in host lists

i agree. It would also be easier to read. Possibly it could become an option?

Author:  shankwc [ Sun Mar 29, 2009 1:41 pm ]
Post subject:  Re: strip the leading zero in IP address in host lists

there is probably a better way to do this or maybe add some recursion, but it's late and i'm tired:

[code]
function format_ip($myip) {
$newip1 = preg_replace('/\.0/', '.', $myip); //first pass
$newip2 = preg_replace('/\.0/', '.', $newip1); //second pass
if (ord($newip2) == 48) { //48 = ascii value for 0
$newip = ltrim ($newip2, "0");
}
if (ord($newip) == 48) { //48 = ascii value for 0
$newip = ltrim ($newip, "0");
}
return $newip;
}
[/code]

Author:  A_Hull [ Mon Mar 30, 2009 12:19 am ]
Post subject:  Re: strip the leading zero in IP address in host lists

Strictly speaking we should store all IP addresses and subnet mask in the same format, and if I am being particularly pedantic they should be stored as ling int, 'cos that is what they are (4 bytes) (http://en.wikipedia.org/wiki/Long_int) this would require a little bit of a re-write, but would also cut down on the size of the database.

Perhaps this should be a consideration for OAv2 (whatever that might be :twisted: )

Author:  jbsclm [ Mon Mar 30, 2009 3:34 am ]
Post subject:  Re: strip the leading zero in IP address in host lists

As you say both subnet and ip address should be in the same format. Both PHP and MYSQL provide functions for converting between IP address strings and integers, but storing them as strings makes it more readable when looking at the tables themselves. The device location add on I wrote stores them as varchar(20), although 15 would be enough, and then uses the MYSQL funtions to manipulate them.

Author:  shankwc [ Mon Mar 30, 2009 3:53 am ]
Post subject:  Re: strip the leading zero in IP address in host lists

i tried using the php ip2long with an IP with leading zeros. Then converted it back using long2ip and it gave me a different IP. (ie: 010.002.002.102). that is why i ended up making that convoluted function. But then I went into the audit.vbs script and commented out the sections where it adds the leading zero's. Now the IP is stored properly. I prefer that method. I realize the zero's were added to make sorting work better, but I find IPs with leading zeros very difficult to read and I have to reformat it for display anyway - which breaks sorting too.

here are my changes to the VBS- starting at line 788:
[code]
' IP Address are padded with zeros so they sort properly
'MyIP = Split(net_ip(0), ".", -1, 1)
'if MyIP(0) <> "169" AND MyIP(1) <> "254" then
' MyIP(0) = right("000" & MyIP(0),3)
' MyIP(1) = right("000" & MyIP(1),3)
' MyIP(2) = right("000" & MyIP(2),3)
' MyIP(3) = right("000" & MyIP(3),3)
' net_ip(0) = MyIP(0) & "." & MyIP(1) & "." & MyIP(2) & "." & MyIP(3)
' The first detected IP address / Subnet mask become the system IP/Mask
if (net_ip(0) <> "0.0.0.0" and net_ip_address = "") then
net_ip_address = net_ip(0)
net_ip_mask = net_ip_subnet(0)
elseif net_ip(0) = "0.0.0.0" then net_ip(0) = "none" end if
'WCS - changed 000s to just 0
'end if
' MyIP = Split(net_ip(1), ".", -1, 1)
' if MyIP(0) <> "169" AND MyIP(1) <> "254" then
' MyIP(0) = right("000" & MyIP(0),3)
' MyIP(1) = right("000" & MyIP(1),3)
' MyIP(2) = right("000" & MyIP(2),3)
' MyIP(3) = right("000" & MyIP(3),3)
' net_ip(1) = MyIP(0) & "." & MyIP(1) & "." & MyIP(2) & "." & MyIP(3)
if net_ip(1) = "0.0.0.0" then net_ip(1) = "none" end if
'WCS - changed 000s to just 0
' end if
' MyIP = Split(net_ip(2), ".", -1, 1)
' if MyIP(0) <> "169" AND MyIP(1) <> "254" then
' MyIP(0) = right("000" & MyIP(0),3)
' MyIP(1) = right("000" & MyIP(1),3)
' MyIP(2) = right("000" & MyIP(2),3)
' MyIP(3) = right("000" & MyIP(3),3)
' net_ip(2) = MyIP(0) & "." & MyIP(1) & "." & MyIP(2) & "." & MyIP(3)
if net_ip(2) = "0.0.0.0" then net_ip(2) = "none" end if
'WCS - changed 000s to just 0
' end if
[/code]

Author:  jbsclm [ Mon Mar 30, 2009 4:39 am ]
Post subject:  Re: strip the leading zero in IP address in host lists

I suspect the php function actually did the correct thing, according to
http://tools.ietf.org/id/draft-main-ipa ... rep-01.txt
the address should not have leading zeros, therefore it is an invalid format.
It looks like several implementations of IP treat any octet with leading zeros as an octal number, so at best the leading zero format is ambiguous, at worst wrong, although it probably depends whch specification you read.
Sorting is easy without leading zeros, MYSQL will do it for you with "ORDER BY inet_aton(<ip_address>)"

Author:  Mark [ Mon Mar 30, 2009 8:42 am ]
Post subject:  Re: strip the leading zero in IP address in host lists

Just an FYI.
OAv2 will store all IPs in the database with leading zero's, and show without leading zero's.
Yes, this is mainly for sorting.
I have a couple of functions (from Open-AudIT) that I use for this - they are loaded and ready to go in CodeIgniter.
The only problem I now have is the javascript table sorting... Javascript sorts the columns fine, but does the sorting based on "non-leading" zero IPAddresses. So it doesn't come out in the order you would expect. Sigh, just another bridge to cross...

Author:  shankwc [ Mon Mar 30, 2009 10:11 am ]
Post subject:  Re: strip the leading zero in IP address in host lists

are you writing OAv2 in CodeIgniter? I have been working on a new frontend for OA that integrates a few other things as well using CI. Maybe we should work together on this?

Author:  Mark [ Mon Mar 30, 2009 1:48 pm ]
Post subject:  Re: strip the leading zero in IP address in host lists

Yep, OAv2 == CodeIgniter.
Am still bedding down the fundamentals, but nearly there.
What I'd give for a week off work to work on it !!!
I reackon I'd have an alhpa release if I had a week... sigh.
Will ask for help when the alpha is done - thanks in advance.

Page 1 of 1 All times are UTC + 10 hours
Powered by phpBB® Forum Software © phpBB Group
https://www.phpbb.com/