[1436] | 1 | #!/usr/bin/python
|
---|
| 2 |
|
---|
| 3 | import getopt, sys, os
|
---|
| 4 | import xml.etree.ElementTree as ET
|
---|
| 5 | from xml.etree.ElementTree import Element
|
---|
| 6 |
|
---|
| 7 | def cleanILOConf (tree):
|
---|
| 8 | for userInfo in tree.findall(".//USER_INFO"):
|
---|
| 9 | for user in userInfo.getiterator ("ADD_USER"):
|
---|
| 10 | userInfo.remove (user)
|
---|
| 11 | login = tree.find (".//LOGIN")
|
---|
| 12 | sso = login.find ("./SSO_INFO")
|
---|
| 13 | if not sso is None:
|
---|
| 14 | login.remove (sso)
|
---|
| 15 | return tree
|
---|
| 16 |
|
---|
| 17 | def createUserInfo (tree):
|
---|
| 18 | login = tree.find (".//LOGIN")
|
---|
| 19 | return ET.SubElement (login, "USER_INFO", {"MODE": "write"})
|
---|
| 20 |
|
---|
| 21 | def modAdminUser (tree, passwd):
|
---|
| 22 | userInfo = tree.find (".//USER_INFO")
|
---|
| 23 | if userInfo is None:
|
---|
| 24 | userInfo = createUserInfo (tree)
|
---|
| 25 | modUser = ET.SubElement (userInfo, "MOD_USER", { "USER_LOGIN": "Administrator"})
|
---|
| 26 | ET.SubElement (modUser, "PASSWORD", { "value": passwd })
|
---|
| 27 | ET.SubElement (modUser, "ADMIN_PRIV", { "value": "Yes" })
|
---|
| 28 | ET.SubElement (modUser, "REMOTE_CONS_PRIV", { "value": "Yes" })
|
---|
| 29 | ET.SubElement (modUser, "RESET_SERVER_PRIV", { "value": "Yes" })
|
---|
| 30 | ET.SubElement (modUser, "VIRTUAL_MEDIA_PRIV", { "value": "Yes" })
|
---|
| 31 | ET.SubElement (modUser, "CONFIG_ILO_PRIV", { "value": "Yes" })
|
---|
| 32 | return tree
|
---|
| 33 |
|
---|
| 34 | def modIP (tree, ip):
|
---|
| 35 | ipAddr = tree.find (".//MOD_NETWORK_SETTINGS/IP_ADDRESS")
|
---|
| 36 | ipAddr.set ("VALUE", ip)
|
---|
| 37 | return tree
|
---|
| 38 |
|
---|
| 39 | def modNetMask (tree, mask):
|
---|
| 40 | netMask = tree.find (".//MOD_NETWORK_SETTINGS/SUBNET_MASK")
|
---|
| 41 | netMask.set ("VALUE", mask)
|
---|
| 42 | return tree
|
---|
| 43 |
|
---|
| 44 | def modGateway (tree, gw):
|
---|
| 45 | gwIP = tree.find (".//MOD_NETWORK_SETTINGS/GATEWAY_IP_ADDRESS")
|
---|
| 46 | gwIP.set ("VALUE", gw)
|
---|
| 47 | return tree
|
---|
| 48 |
|
---|
| 49 | def addLicense (tree, key):
|
---|
| 50 | ribInfo = tree.find (".//RIB_INFO")
|
---|
| 51 | license = ET.SubElement (ribInfo, "LICENSE")
|
---|
| 52 | ET.SubElement (license, "ACTIVATE", {"KEY": key})
|
---|
| 53 | return tree
|
---|
| 54 |
|
---|
| 55 | def forceReset (tree):
|
---|
| 56 | login = tree.find ("./LOGIN")
|
---|
| 57 | ribInfo = ET.SubElement (login, "RIB_INFO", {"MODE": "write"})
|
---|
| 58 | ET.SubElement (ribInfo, "RESET_RIB")
|
---|
| 59 | return tree
|
---|
| 60 |
|
---|
| 61 | def main ():
|
---|
| 62 | try:
|
---|
| 63 | opts, args = getopt.getopt (sys.argv [1:], "p:l:", ["password=", "license=", "ip=", "netmask=", "gw="])
|
---|
| 64 | except getopt.GetoptError, err:
|
---|
| 65 | print str (err)
|
---|
| 66 | sys.exit (2)
|
---|
| 67 | password = key = ip = netmask = gw = None
|
---|
| 68 | for o, a in opts:
|
---|
| 69 | if o in ("-p", "--password"):
|
---|
| 70 | password = a
|
---|
| 71 | elif o in ("-l", "--license"):
|
---|
| 72 | key = a
|
---|
| 73 | elif o == "--ip":
|
---|
| 74 | ip = a
|
---|
| 75 | elif o == "--netmask":
|
---|
| 76 | netmask = a
|
---|
| 77 | elif o == "--gw":
|
---|
| 78 | gw = a
|
---|
| 79 | assert os.path.isfile (args [0])
|
---|
| 80 | tree = ET.parse (args [0])
|
---|
| 81 | cleanILOConf (tree)
|
---|
| 82 | if password:
|
---|
| 83 | modAdminUser (tree, password)
|
---|
| 84 | if key:
|
---|
| 85 | addLicense (tree, key)
|
---|
| 86 | if ip:
|
---|
| 87 | modIP (tree, ip)
|
---|
| 88 | if netmask:
|
---|
| 89 | modNetMask (tree, netmask)
|
---|
| 90 | if gw:
|
---|
| 91 | modGateway (tree, gw)
|
---|
| 92 |
|
---|
| 93 | # Force the iLO to reset
|
---|
| 94 | forceReset (tree)
|
---|
| 95 |
|
---|
| 96 | print ET.tostring (tree.getroot ())
|
---|
| 97 |
|
---|
| 98 |
|
---|
| 99 | if __name__ == "__main__":
|
---|
| 100 | main ()
|
---|