Pages

Thursday, January 16, 2014

TCL procs

Ip address increment :
proc generate_ips {start_ip number_of_ips} {
    regexp {([0-9]+)\.([0-9]+)\.([0-9]+)\.([0-9]+)} $start_ip - oct1 oct2 oct3 oct4
    set ip_list ""
    while {[llength $ip_list] < $number_of_ips} {
        for {} {$oct4 <= 254} {incr oct4} {
            lappend ip_list "$oct1.$oct2.$oct3.$oct4"
            if {[llength $ip_list] == $number_of_ips} {
                break
            }
        }
        if {$oct4 == 255} {
            set oct4 0
            incr oct3
        }
        if {$oct3 == 256} {
            set oct3 0
            incr oct2
        }
        if {$oct2 == 256} {
            set oct2 0
            incr oct1
        }
        if {$oct1 == 256} {
            break
        }
    }
    return $ip_list
}
set start_ip "103.0.0.2"
set ip_list [generate_ips $start_ip 5]
puts $ip_list
puts [llength $ip_list]

In python:
Here's a Python program that prints 1000 IPv4 addresses in incremental fashion:

```python
def print_ipv4_addresses(start_ip="192.168.0.1", count=1000):
    # Split the start IP address into octets
    parts = start_ip.split('.')
    octets = [int(part) for part in parts]

    # Loop through the count of IPv4 addresses
    for _ in range(count):
        # Print the current IP address
        print('.'.join(map(str, octets)))

        # Increment the last octet
        octets[3] += 1

        # Adjust other octets if necessary
        for i in range(3, 0, -1):
            if octets[i] > 255:
                octets[i] = 0
                octets[i - 1] += 1
            else:
                break

# Call the function to print IPv4 addresses
print_ipv4_addresses()
```

This program defines a function `print_ipv4_addresses` that takes two optional parameters: `start_ip` (the starting IPv4 address, default is "192.168.0.1") and `count` (the number of IPv4 addresses to print, default is 1000).

The function splits the starting IP address into octets and increments them in a loop while printing the resulting IPv4 addresses. If an octet exceeds 255, it rolls over to 0 and the next octet is incremented accordingly. This process continues until the desired number of IPv4 addresses are printed.


mac address increment :

proc generate_mac {start_mac number_of_mac} {
    regexp {([0-9a-fA-F]+)([0-9a-fA-F]+)\:([0-9a-fA-F]+)([0-9a-fA-F]+)\:([0-9a-fA-F]+)([0-9a-fA-F]+)\:([0-9a-fA-F]+)([0-9a-fA-F]+)\:([0-9a-fA-F]+)([0-9a-fA-F]+)\:([0-9a-fA-F]+)([0-9a-fA-F]+)} $start_mac - hex1 hex2 hex3 hex4 hex5 hex6 hex7 hex8 hex9 hex10 hex11 hex12
    set mac_list ""
    array set hex_list {0 0 1 1 2 2 3 3 4 4 5 5 6 6 7 7 8 8 9 9 10 A 11 B 12 C 13 D 14 E 15 F 16 16}
    array set hex_list {0 0 1 1 2 2 3 3 4 4 5 5 6 6 7 7 8 8 9 9 10 a 11 b 12 c 13 d 14 e 15 f 16 16}
    array set dec_list {0 0 1 1 2 2 3 3 4 4 5 5 6 6 7 7 8 8 9 9 A 10 B 11 C 12 D 13 E 14 F 15} 
    array set dec_list {0 0 1 1 2 2 3 3 4 4 5 5 6 6 7 7 8 8 9 9 a 10 b 11 c 12 d 13 e 14 f 15}
    set hex12 $dec_list($hex12)
    while {[llength $mac_list] < $number_of_mac} {
        for {} {$hex12 <= 15} {incr hex12} {
            set hex12_new $hex_list($hex12)
            lappend mac_list "$hex1$hex2:$hex3$hex4:$hex5$hex6:$hex7$hex8:$hex9$hex10:$hex11$hex12_new"
            if {[llength $mac_list] == $number_of_mac} {
                break
            }
        }
        if {$hex12 == 16} {
            set hex12 0
            set hex11 $dec_list($hex11)
            incr hex11
            set hex11 $hex_list($hex11)
        }
        if {$hex11 == 16} {
            set hex11 0
            set hex10 $dec_list($hex10)
            incr hex10
            set hex10 $hex_list($hex10)
        }
        if {$hex10 == 16} {
            set hex10 0
            set hex9 $dec_list($hex9)
            incr hex9
            set hex9 $hex_list($hex9)
        }
        if {$hex9 == 16} {
            set hex9 0
            set hex8 $dec_list($hex8)
            incr hex8
            set hex8 $hex_list($hex8)
        }
        if {$hex8 == 16} {
            set hex8 0
            set hex7 $dec_list($hex7)
            incr hex7
            set hex7 $hex_list($hex7)
        }
        if {$hex7 == 16} {
            set hex7 0
            set hex6 $dec_list($hex6)
            incr hex6
            set hex6 $hex_list($hex6)
        }
        if {$hex6 == 16} {
            set hex6 0
            set hex5 $dec_list($hex5)
            incr hex5
            set hex5 $hex_list($hex5)
        }
        if {$hex5 == 16} {
            set hex5 0
            set hex4 $dec_list($hex4)
            incr hex4
            set hex4 $hex_list($hex4)
        }
        if {$hex4 == 16} {
            set hex4 0
            set hex3 $dec_list($hex3)
            incr hex3
            set hex3 $hex_list($hex3)
        }
        if {$hex3 == 16} {
            set hex3 0
            set hex2 $dec_list($hex2)
            incr hex2
            set hex2 $hex_list($hex2)
        }
        if {$hex2 == 16} {
            set hex2 0
            set hex1 $dec_list($hex1)
            incr hex1
            set hex1 $hex_list($hex1)
        }
        if {$hex1 == 16} {
            break
        }
    }
    return $mac_list
}
set start_mac "10:ef:09:14:a0:7e"
set mac_list [generate_mac $start_mac 100]
puts $mac_list
puts [llength $mac_list]


In Python:

Here's a Python program that increments a MAC address:

```python
def increment_mac_address(mac_address):
    # Split MAC address into octets
    octets = mac_address.split(':')

    # Convert each octet to an integer
    for i in range(len(octets)):
        octets[i] = int(octets[i], 16)

    # Increment the last octet
    octets[-1] += 1

    # Handle carry-over if necessary
    for i in range(len(octets) - 1, 0, -1):
        if octets[i] > 255:
            octets[i] = octets[i] % 256
            octets[i - 1] += 1
        else:
            break

    # Convert octets back to hexadecimal string format
    incremented_mac = ':'.join([format(octet, '02x') for octet in octets])

    return incremented_mac

# Example usage
original_mac = '00:11:22:33:44:ff'
incremented_mac = increment_mac_address(original_mac)
print("Original MAC address:", original_mac)
print("Incremented MAC address:", incremented_mac)
```

This program defines a function `increment_mac_address` that takes a MAC address as input and returns the incremented MAC address. The function splits the MAC address into octets, increments the last octet, and handles carry-over if necessary. Finally, it converts the octets back to hexadecimal string format and returns the incremented MAC address.

You can call this function with a MAC address as input to increment it.