diff --git a/tools/pdu_automation.py b/tools/pdu_automation.py index 741a3ec3e..9fdd90799 100644 --- a/tools/pdu_automation.py +++ b/tools/pdu_automation.py @@ -1,64 +1,89 @@ import os import pprint import time +import argparse from typing import Sequence from typing import Optional -import argparse try: import dlipower except: print('Please wait we are installing DLI Power') os.system('pip install dlipower') -power_switch = dlipower.PowerSwitch(hostname='192.168.200.49', userid='admin', password='Lanforge12345!') -class switch_on: - def __init__(self, num=None): - self.num = num - if self.num != None: - power_switch[int(self.num)-1].state = "ON" +class setup: + def __init__(self, hostname, user, password): + self.hostname = hostname + self.user = user + self.password = password + self.power_switch = dlipower.PowerSwitch(hostname=self.hostname, userid=self.user, password=self.password) + + +class switch_on(setup): + def __init__(self, hostname, user, password, port=None): + super().__init__(hostname, user, password) + self.port = port + # print(self.power_switch) + if self.port != None: + # String Manupulation + self.power_switch[int(self.port)-1].state = "ON" else: - for outlet in power_switch: + for outlet in self.power_switch: outlet.state = 'ON' -class switch_off: - def __init__(self, num=None): - self.num = num - if self.num != None: - power_switch[int(self.num)-1].state = "OFF" +class switch_off(setup): + def __init__(self, hostname, user, password, port=None): + super().__init__(hostname, user, password) + self.port = port + if self.port != None: + self.power_switch[int(self.port)-1].state = "OFF" else: - for outlet in power_switch: + for outlet in self.power_switch: outlet.state = 'OFF' -def main(argv:Optional[Sequence[str]]=None): + +def main(argv: Optional[Sequence[str]]=None): parser = argparse.ArgumentParser() - parser = argparse.ArgumentParser() - parser.add_argument('--ON_ALL', help='Switches all Outlets in ON Mode') - parser.add_argument('--OFF_ALL', help='Switches all Outlets in OFF Mode') - parser.add_argument('--ON', help='Switches the target Outlets in ON Mode eg: --ON 4') - parser.add_argument('--OFF', help='Switches the target Outlets in OFF Mode --OFF 6') - parser.add_argument('--CYCLE_ALL', help='Cycle all Outlets in OFF Mode and then ON Mode eg: --CYCLE_ALL True') - parser.add_argument('--CYCLE', help='Cycle selected target Outlets in OFF Mode and then ON Mode eg: --CYCLE 6') + parser.add_argument('--host', help='Please provide host name eg: 192.168.200.65') + parser.add_argument('--username', help='Please provide username eg: admin') + parser.add_argument('--password', help='Please provide password eg: 1234') + parser.add_argument('--action', help='Switches all Outlets in ON Mode eg: --on_all\n' + 'Switches all Outlets in OFF Mode eg: --off_all\n' + 'Cycle all Outlets in OFF Mode and then ON Mode eg: --cycle_all\n' + 'Switches the target Outlets in ON Mode eg: --on\n' + 'Switches the target Outlets in OFF Mode --off\n') + parser.add_argument('--port', help='Please provide port name eg: --port lanforge') args = parser.parse_args(argv) dic = vars(args) # print(dic) - if dic['ON_ALL'] == 'True': - on = switch_on() - elif dic['OFF_ALL'] == 'True': - off = switch_off() - elif dic['CYCLE_ALL'] is not None: - off = switch_off() - on = switch_on() - elif dic['CYCLE'] is not None: - on = switch_off(dic['CYCLE']) - off = switch_on(dic['CYCLE']) - elif dic['ON'] is not None: - on = switch_on(dic['ON']) - elif dic['OFF'] is not None: - on = switch_off(dic['OFF']) + if dic['action'] == 'on_all': + set = setup(dic['host'], dic['username'], dic['password']) + on = switch_on(dic['host'], dic['username'], dic['password']) + elif dic['action'] == 'off_all': + set = setup(dic['host'], dic['username'], dic['password']) + off = switch_off(dic['host'], dic['username'], dic['password']) + elif dic['action'] == 'on': + set = setup(dic['host'], dic['username'], dic['password']) + on = switch_on(dic['host'], dic['username'], dic['password'], dic['port']) + # off = switch_on(dic['action']) + elif dic['action'] == 'off': + set = setup(dic['host'], dic['username'], dic['password']) + on = switch_off(dic['host'], dic['username'], dic['password'], dic['port']) + # off = switch_on(dic['action']) + elif dic['action'] == 'cycle_all': + set = setup(dic['host'], dic['username'], dic['password']) + off = switch_off(dic['host'], dic['username'], dic['password']) + on = switch_on(dic['host'], dic['username'], dic['password']) + elif dic['action'] == 'cycle': + set = setup(dic['host'], dic['username'], dic['password']) + on = switch_off(dic['host'], dic['username'], dic['password'], dic['port']) + off = switch_on(dic['host'], dic['username'], dic['password'], dic['port']) + else: + print('Command not found') if __name__ == '__main__': main() +