IP Address Increment

Leeus

Regular
Joined
Dec 23, 2002
Messages
50
Has anyone got a function to add a number to an ip address? I want it to cope with once it reaches 254 to increment the previous octet also?!?
 
This is soooo going to be used for a hack attack :)

Visual Basic:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        Dim strNext As String = "0.0.0.0"
        Dim intIndex As Integer

        For intIndex = 0 To 100000
            Debug.WriteLine(strNext)
            strNext = funcNextIPAddy(strNext)
        Next

    End Sub

    Private Function funcNextIPAddy(ByVal strIPAddy) As String

        Dim intIndex As Integer
        Dim intRev As Integer
        Dim strSplit() As String = Split(strIPAddy, ".")
        Dim bytIP(3) As Byte
        'Just to make sure it works
        ReDim Preserve strSplit(3)
        Dim bAdded As Boolean = False

        For intIndex = 3 To 0 Step -1
            bytIP(intIndex) = funcFixByte(strSplit(intIndex))
            If bAdded = False Then
                If bytIP(intIndex) < 255 Then
                    bytIP(intIndex) += 1
                    bAdded = True
                    For intRev = intIndex + 1 To 3
                        bytIP(intRev) = 0
                    Next
                End If
            End If
        Next

        Return CStr(bytIP(0)) & "." & CStr(bytIP(1)) & "." & CStr(bytIP(2)) & "." & CStr(bytIP(3))

    End Function

    Private Function funcFixByte(ByVal Data As String) As Byte
        Dim intVal As Integer = Val(Data)
        If intVal < 0 Then intVal = 0
        If intVal > 255 Then intVal = 255
        Return CByte(intVal)
    End Function
 
Last edited by a moderator:
you're gonna need to modify to reduce the # of ips produced.
C#:
string[] GenerateIps()
{
	ArrayList ips = new ArrayList();
	string a=null,b=null,c=null,d=null;
	for(int intA = 1; intA < 255; intA++)
	{
		a = intA.ToString(); 
		for(int intB = 0; intB < 255; intB++)
		{
			b = intB.ToString();
			for(int intC = 0; intC < 255; intC++)
			{
				c = intC.ToString();
				for(int intD = 0; intD < 255; intD++)
				{
					d = intD.ToString();
					ips.Add(a + "." + b + "." + c + "." + d);
				}
				ips.Add(a + "." + b + "." + c + "." + d);
			}
			ips.Add(a + "." + b + "." + c + "." + d);
		}
		ips.Add(a + "." + b + "." + c + "." + d);
	}
	return (string[]) ips.ToArray(typeof(string));
}

Grimfort said:
This is soooo going to be used for a hack attack :)

More like, try to use. Anyway, if someone trying to hack and is asking a question like this, I highly doubt s/he will be able to go very far with their project (assuming it's being used for a hack).
 
Last edited by a moderator:
No seriously, its not going to be used for hacking, I work for an isp and we have huge issues with our customers being open for relay, so this app just checks for that! I could have plumbed in our ip ranges but is seems to do the trick!

Thanks guys!
 
Back
Top