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