danhoutz

Creating an ESI generator and DB with ChatGPT

Nov 3rd, 2024
44
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.21 KB | None | 0 0
  1. "Hey! I should have had ChatGPT do it!"....
  2. 7:21
  3. So I asked it, Write python code to generate a unique Type 1 EVPN ESI and store it in a sqlite database
  4. 7:21
  5. 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:
  6. 7:21
  7. import sqlite3
  8. import uuid
  9.  
  10. def generate_unique_esi():
  11. # Generate a unique MAC-like address (48 bits)
  12. return ':'.join(format(uuid.uuid4().int >> i & 0xff, '02x') for i in (40, 32, 24, 16, 8, 0))
  13.  
  14. def create_database(db_name):
  15. # Connect to SQLite database
  16. conn = sqlite3.connect(db_name)
  17. cursor = conn.cursor()
  18.  
  19. # Create a table for storing ESIs
  20. cursor.execute('''
  21. CREATE TABLE IF NOT EXISTS evpn_esi (
  22. id INTEGER PRIMARY KEY AUTOINCREMENT,
  23. esi TEXT UNIQUE NOT NULL
  24. )
  25. ''')
  26. conn.commit()
  27. return conn
  28.  
  29. def store_esi(conn, esi):
  30. cursor = conn.cursor()
  31. try:
  32. # Insert the unique ESI into the database
  33. cursor.execute('INSERT INTO evpn_esi (esi) VALUES (?)', (esi,))
  34. conn.commit()
  35. print(f'Successfully stored ESI: {esi}')
  36. except sqlite3.IntegrityError:
  37. print(f'ESI {esi} already exists in the database.')
  38.  
  39. def main():
  40. db_name = 'evpn_esi.db'
  41. conn = create_database(db_name)
  42.  
  43. # Generate and store a unique ESI
  44. esi = generate_unique_esi()
  45. store_esi(conn, esi)
  46.  
  47. # Close the database connection
  48. conn.close()
  49.  
  50. if __name__ == '__main__':
  51. main()
  52.  
  53.  
  54. dhoutz
  55. 7:30 PM
  56. Close but you are generating a MAC addresses, not a 10 tuple ESI. Can you correct the code?
  57. def generate_unique_esi():
  58. # Generate a 10-tuple ESI
  59. # The first 6 bytes (48 bits) can be random values representing the Ethernet Segment ID
  60. esi_parts = [random.randint(0, 255) for _ in range(8)] # 8 bytes
  61. # Create the ESI as a hexadecimal string
  62. esi = ''.join(format(part, '02x') for part in esi_parts)
  63. return esi
  64. It nearly got it but it forgot the damn : for the join. haha
  65. Closer but not inder a colo between every two characters
  66. def generate_unique_esi():
  67. # Generate a 10-tuple ESI (8 bytes + 2 bytes for identifier)
  68. esi_parts = [random.randint(0, 255) for _ in range(10)] # 10 bytes
  69. # Format the ESI as a string with colons between every two characters
  70. esi = ':'.join(format(part, '02x') for part in esi_parts)
  71. return esi
  72. So close even with all my typos in the prompt. BUT a Type 1 ESI should always start with 00......
  73. Almost but a Type 1 ESI should always start with 00: as the first tuple. Can you correct the code?
  74. def generate_unique_esi():
  75. # Generate a 10-tuple ESI, starting with '00:'
  76. esi_parts = [0] # Start with 00
  77. esi_parts += [random.randint(0, 255) for _ in range(9)] # Generate the remaining 9 bytes
  78. # Format the ESI as a string with colons between every two characters
  79. esi = ':'.join(format(part, '02x') for part in esi_parts)
  80. return esi
  81. 7:30
  82. that IS CORRECT!!
  83. 7:31
  84. >>> esi_parts = [0] # Start with 00
  85. >>> esi_parts += [random.randint(0, 255) for _ in range(9)] # Generate the remaining 9 bytes
  86. >>> esi = ':'.join(format(part, '02x') for part in esi_parts)
  87. >>> print(esi)
  88. 00:8f:e8:77:ad:ec:af:a1:3c:e4
Advertisement
Add Comment
Please, Sign In to add comment