source: package_branches/invirt-web/hvirt/code/xen-ips @ 3015

Last change on this file since 3015 was 2322, checked in by price, 16 years ago

xen-ips: silently ignore already-present IPs

Also from the first hours of April. You all can imagine
how happy I was to be running this code. =)

  • Property svn:executable set to *
File size: 1.4 KB
Line 
1#!/usr/bin/python
2import random
3from invirt import database
4import sqlalchemy.exceptions
5import sys
6
7# stolen directly from xend/server/netif.py
8def 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
27def randomUUID():
28    """Generate a random UUID."""
29
30    return [ random.randint(0, 255) for _ in range(0, 16) ]
31
32def uuidToString(u):
33    return "-".join(["%02x" * 4, "%02x" * 2, "%02x" * 2, "%02x" * 2,
34                     "%02x" * 6]) % tuple(u)
35
36
37def usage():
38    print >> sys.stderr, "USAGE: " + sys.argv[0] + " <ip>"
39
40def addip(ip):
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
47
48
49if __name__ == '__main__':
50    if len(sys.argv) == 2:
51        ip = sys.argv[1]
52    else:
53        usage()
54        sys.exit(1)
55    database.connect()
56    addip(ip)
Note: See TracBrowser for help on using the repository browser.