Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- "Hey! I should have had ChatGPT do it!"....
- 7:21
- So I asked it, Write python code to generate a unique Type 1 EVPN ESI and store it in a sqlite database
- 7:21
- And it seems to have written something that's almost right EXCEPT it's generating MAC addresses and not ESIs :rolling_on_the_floor_laughing:
- 7:21
- import sqlite3
- import uuid
- def generate_unique_esi():
- # Generate a unique MAC-like address (48 bits)
- return ':'.join(format(uuid.uuid4().int >> i & 0xff, '02x') for i in (40, 32, 24, 16, 8, 0))
- def create_database(db_name):
- # Connect to SQLite database
- conn = sqlite3.connect(db_name)
- cursor = conn.cursor()
- # Create a table for storing ESIs
- cursor.execute('''
- CREATE TABLE IF NOT EXISTS evpn_esi (
- id INTEGER PRIMARY KEY AUTOINCREMENT,
- esi TEXT UNIQUE NOT NULL
- )
- ''')
- conn.commit()
- return conn
- def store_esi(conn, esi):
- cursor = conn.cursor()
- try:
- # Insert the unique ESI into the database
- cursor.execute('INSERT INTO evpn_esi (esi) VALUES (?)', (esi,))
- conn.commit()
- print(f'Successfully stored ESI: {esi}')
- except sqlite3.IntegrityError:
- print(f'ESI {esi} already exists in the database.')
- def main():
- db_name = 'evpn_esi.db'
- conn = create_database(db_name)
- # Generate and store a unique ESI
- esi = generate_unique_esi()
- store_esi(conn, esi)
- # Close the database connection
- conn.close()
- if __name__ == '__main__':
- main()
- dhoutz
- 7:30 PM
- Close but you are generating a MAC addresses, not a 10 tuple ESI. Can you correct the code?
- def generate_unique_esi():
- # Generate a 10-tuple ESI
- # The first 6 bytes (48 bits) can be random values representing the Ethernet Segment ID
- esi_parts = [random.randint(0, 255) for _ in range(8)] # 8 bytes
- # Create the ESI as a hexadecimal string
- esi = ''.join(format(part, '02x') for part in esi_parts)
- return esi
- It nearly got it but it forgot the damn : for the join. haha
- Closer but not inder a colo between every two characters
- def generate_unique_esi():
- # Generate a 10-tuple ESI (8 bytes + 2 bytes for identifier)
- esi_parts = [random.randint(0, 255) for _ in range(10)] # 10 bytes
- # Format the ESI as a string with colons between every two characters
- esi = ':'.join(format(part, '02x') for part in esi_parts)
- return esi
- So close even with all my typos in the prompt. BUT a Type 1 ESI should always start with 00......
- Almost but a Type 1 ESI should always start with 00: as the first tuple. Can you correct the code?
- def generate_unique_esi():
- # Generate a 10-tuple ESI, starting with '00:'
- esi_parts = [0] # Start with 00
- esi_parts += [random.randint(0, 255) for _ in range(9)] # Generate the remaining 9 bytes
- # Format the ESI as a string with colons between every two characters
- esi = ':'.join(format(part, '02x') for part in esi_parts)
- return esi
- 7:30
- that IS CORRECT!!
- 7:31
- >>> esi_parts = [0] # Start with 00
- >>> esi_parts += [random.randint(0, 255) for _ in range(9)] # Generate the remaining 9 bytes
- >>> esi = ':'.join(format(part, '02x') for part in esi_parts)
- >>> print(esi)
- 00:8f:e8:77:ad:ec:af:a1:3c:e4
Advertisement
Add Comment
Please, Sign In to add comment