May-01-2023, 11:46 PM
(This post was last modified: May-01-2023, 11:46 PM by billykid999.)
So I wrote some code and it works fine but I am having issues passing in classes/functions from the command line.
Here's the code:
Hardware(ConnectHandler,ip)
Hardware is another class which reads in ip from the above code and performs some operations.
Everything is working fine.
Now I want to try to call Hardware from the cli, as I will be having up to 5-10 seperate classes that will be performing operations on ConnectHandler and ip, i.e class_n(ConnectHandler, ip)
and I want to pick and choose classes from the cli.
And so I did this:
notice how I imported Hardware above. I am ok with that, that isn't the problem, as i will be importing multiple classes whether here or in the main.py file, and I want to define which classes to use in a list in the command line. Hardware is a class. It work fine when I executed it in the Connect class.
here's me trying to execute it from the CLI:
py Connect_Handler.py inlist = [Hardware]
notice i passed in inlist as sysargv, and I want it to operate like kwargs in connect_parser.
The output gives me 'str' object is not callable
so then i change the name == __main__ section to:
and so i tried this, and printed out the 'type' of my functions to have a look at them, and realised that Connector.connect_parser() resolves to a NoneType:
py Connect_Handler.py [Hardware]
and it gives me
"NoneType has no attribute Connect"
Can some one guide me either to what to do or where to experiment to try and resolve this issue? In the end all I need to do is call Hardware on the ConnectHandler from the CLI
Hardware(ConnectHandler)
by passing it to connect_parser
connect_parser(Hardware)
as I plan on passing a list of imported functions from the CLI when i run this.
Here's the code:
import sys
import netmiko
import json
from Hardware_info import HardwareMaintenance
Hardware = HardwareMaintenance.hardware_handler
class Connect:
def __init__(self, **kwargs):
# self.protocol = netmiko.ssh_autodetect
# pass
intake_file = open('ip_list.txt', 'r')
self.json_data = [json.loads(line) for line in intake_file]
self.kwargs = [Hardware]
def connect_parser(self, kwargs=None):
for data in self.json_data:
ip = data["ip"]
# port = data["port"]
username = data["username"] if data["username"] else ""
password = data["password"] if data["password"] else ""
secret = data["secret"] if "secret" in data else False
device_type = data["device_type"] if data["device_type"] else ""
if data["header"] == "Netmiko":
print("The variables being passed: " + ip, username, password, device_type)
ConnectHandler = netmiko.ConnectHandler(
device_type=device_type,
host=ip,
username=username,
password=password,
port=22,
secret=data["secret"] if "secret" in data else False
)
ConnectHandler.enable()when I add this line next under ConnectHandler.enable() it works:Hardware(ConnectHandler,ip)
Hardware is another class which reads in ip from the above code and performs some operations.
Everything is working fine.
Now I want to try to call Hardware from the cli, as I will be having up to 5-10 seperate classes that will be performing operations on ConnectHandler and ip, i.e class_n(ConnectHandler, ip)
and I want to pick and choose classes from the cli.
And so I did this:
if __name__ == "__main__":
from Connect_Handler import Hardware
from Connect_Handler import Connect
Connector = Connect()
inlist = sys.argv[0]
Connector.connect_parser(inlist)I also added this into the main script under ConnectHandler.enable() for i in kwargs:
i(ConnectHandler)where kwargs are variables to be passed into connect_parsernotice how I imported Hardware above. I am ok with that, that isn't the problem, as i will be importing multiple classes whether here or in the main.py file, and I want to define which classes to use in a list in the command line. Hardware is a class. It work fine when I executed it in the Connect class.
here's me trying to execute it from the CLI:
py Connect_Handler.py inlist = [Hardware]
notice i passed in inlist as sysargv, and I want it to operate like kwargs in connect_parser.
The output gives me 'str' object is not callable
so then i change the name == __main__ section to:
if __name__ == "__main__":
from Connect_Handler import Hardware
from Connect_Handler import Connect
Connector = Connect()
inlist = sys.argv[0]
for i in inlist:
i(Connector.connect_parser())again it gives me 'str' object is not callableand so i tried this, and printed out the 'type' of my functions to have a look at them, and realised that Connector.connect_parser() resolves to a NoneType:
if __name__ == "__main__":
from Connect_Handler import Hardware
from Connect_Handler import Connect
Connector = Connect().connect_parser()
print(Connector)
inlist = sys.argv[0]
Connector.Connect(inlist)CLI:py Connect_Handler.py [Hardware]
and it gives me
"NoneType has no attribute Connect"
Can some one guide me either to what to do or where to experiment to try and resolve this issue? In the end all I need to do is call Hardware on the ConnectHandler from the CLI
Hardware(ConnectHandler)
by passing it to connect_parser
connect_parser(Hardware)
as I plan on passing a list of imported functions from the CLI when i run this.
