[113] | 1 | #!/usr/bin/python |
---|
| 2 | import random |
---|
[865] | 3 | from invirt import database |
---|
[113] | 4 | import sys |
---|
| 5 | |
---|
| 6 | # stolen directly from xend/server/netif.py |
---|
| 7 | def randomMAC(): |
---|
| 8 | """Generate a random MAC address. |
---|
| 9 | |
---|
| 10 | Uses OUI (Organizationally Unique Identifier) 00-16-3E, allocated to |
---|
| 11 | Xensource, Inc. The OUI list is available at |
---|
| 12 | http://standards.ieee.org/regauth/oui/oui.txt. |
---|
| 13 | |
---|
| 14 | The remaining 3 fields are random, with the first bit of the first |
---|
| 15 | random field set 0. |
---|
| 16 | |
---|
| 17 | @return: MAC address string |
---|
| 18 | """ |
---|
| 19 | mac = [ 0x00, 0x16, 0x3e, |
---|
| 20 | random.randint(0x00, 0x7f), |
---|
| 21 | random.randint(0x00, 0xff), |
---|
| 22 | random.randint(0x00, 0xff) ] |
---|
| 23 | return ':'.join(map(lambda x: "%02x" % x, mac)) |
---|
| 24 | |
---|
| 25 | # ... and stolen from xend/uuid.py |
---|
| 26 | def randomUUID(): |
---|
| 27 | """Generate a random UUID.""" |
---|
| 28 | |
---|
| 29 | return [ random.randint(0, 255) for _ in range(0, 16) ] |
---|
| 30 | |
---|
| 31 | def uuidToString(u): |
---|
| 32 | return "-".join(["%02x" * 4, "%02x" * 2, "%02x" * 2, "%02x" * 2, |
---|
| 33 | "%02x" * 6]) % tuple(u) |
---|
| 34 | |
---|
| 35 | |
---|
| 36 | def usage(): |
---|
| 37 | print >> sys.stderr, "USAGE: " + sys.argv[0] + " <ip>" |
---|
| 38 | |
---|
| 39 | def addip(ip): |
---|
[1156] | 40 | n = database.NIC(machine=None, mac_addr=randomMAC(), ip=ip, hostname=None) |
---|
| 41 | database.session.save(n) |
---|
| 42 | database.session.flush() |
---|
[113] | 43 | |
---|
| 44 | |
---|
| 45 | if __name__ == '__main__': |
---|
| 46 | if len(sys.argv) == 2: |
---|
| 47 | ip = sys.argv[1] |
---|
| 48 | else: |
---|
| 49 | usage() |
---|
| 50 | sys.exit(1) |
---|
[865] | 51 | database.connect() |
---|
[113] | 52 | addip(ip) |
---|