bug fixes, preparing for unittesting

This commit is contained in:
fluzzi 2022-06-10 13:24:26 -03:00
parent 3061b54059
commit 2f5b5fcf6b
5 changed files with 914 additions and 868 deletions

View File

@ -1,2 +1,2 @@
__version__ = "2.1.3"
__version__ = "2.1.4"

View File

@ -69,14 +69,18 @@ class configfile:
self.profiles = config["profiles"]
if not os.path.exists(self.key):
self._createkey(self.key)
self.privatekey = RSA.import_key(open(self.key).read())
with open(self.key) as f:
self.privatekey = RSA.import_key(f.read())
f.close()
self.publickey = self.privatekey.publickey()
def _loadconfig(self, conf):
#Loads config file
jsonconf = open(conf)
return json.load(jsonconf)
jsondata = json.load(jsonconf)
jsonconf.close()
return jsondata
def _createconfig(self, conf):
#Create config file
@ -87,7 +91,9 @@ class configfile:
f.close()
os.chmod(conf, 0o600)
jsonconf = open(conf)
return json.load(jsonconf)
jsondata = json.load(jsonconf)
jsonconf.close()
return jsondata
def _saveconfig(self, conf):
#Save config file
@ -106,6 +112,7 @@ class configfile:
f.write(key.export_key('PEM'))
f.close()
os.chmod(keyfile, 0o600)
return key
def _explode_unique(self, unique):
#Divide unique name into folder, subfolder and id

View File

@ -129,9 +129,13 @@ class connapp:
#Function called when connecting or managing nodes.
if not self.case and args.data != None:
args.data = args.data.lower()
if args.action == "version":
actions = {"version": self._version, "connect": self._connect, "debug": self._connect, "add": self._add, "del": self._del, "mod": self._mod, "show": self._show}
return actions.get(args.action)(args)
def _version(self, args):
print(__version__)
if args.action == "connect" or args.action == "debug":
def _connect(self, args):
if args.data == None:
matches = self.nodes
if len(matches) == 0:
@ -156,7 +160,8 @@ class connapp:
node.interact(debug = True)
else:
node.interact()
elif args.action == "del":
def _del(self, args):
if args.data == None:
print("Missing argument node")
exit(3)
@ -169,6 +174,8 @@ class connapp:
exit(2)
question = [inquirer.Confirm("delete", message="Are you sure you want to delete {}?".format(matches[0]))]
confirm = inquirer.prompt(question)
if confirm == None:
exit(7)
if confirm["delete"]:
uniques = self.config._explode_unique(matches[0])
if args.data.startswith("@"):
@ -177,7 +184,8 @@ class connapp:
self.config._connections_del(**uniques)
self.config._saveconfig(self.config.file)
print("{} deleted succesfully".format(matches[0]))
elif args.action == "add":
def _add(self, args):
if args.data == None:
print("Missing argument node")
exit(3)
@ -209,7 +217,6 @@ class connapp:
self.config._folder_add(**uniques)
self.config._saveconfig(self.config.file)
print("{} added succesfully".format(args.data))
if type == "node":
nodefolder = args.data.partition("@")
nodefolder = "@" + nodefolder[2]
@ -230,7 +237,8 @@ class connapp:
self.config._connections_add(**newnode)
self.config._saveconfig(self.config.file)
print("{} added succesfully".format(args.data))
elif args.action == "show":
def _show(self, args):
if args.data == None:
print("Missing argument node")
exit(3)
@ -246,7 +254,8 @@ class connapp:
print(k + ":")
for i in v:
print(" - " + i)
elif args.action == "mod":
def _mod(self, args):
if args.data == None:
print("Missing argument node")
exit(3)
@ -277,7 +286,10 @@ class connapp:
#Function called when managing profiles
if not self.case:
args.data[0] = args.data[0].lower()
if args.action == "del":
actions = {"add": self._profile_add, "del": self._profile_del, "mod": self._profile_mod, "show": self._profile_show}
return actions.get(args.action)(args)
def _profile_del(self, args):
matches = list(filter(lambda k: k == args.data[0], self.profiles))
if len(matches) == 0:
print("{} not found".format(args.data[0]))
@ -296,7 +308,8 @@ class connapp:
self.config._profiles_del(id = matches[0])
self.config._saveconfig(self.config.file)
print("{} deleted succesfully".format(matches[0]))
elif args.action == "show":
def _profile_show(self, args):
matches = list(filter(lambda k: k == args.data[0], self.profiles))
if len(matches) == 0:
print("{} not found".format(args.data[0]))
@ -309,7 +322,8 @@ class connapp:
print(k + ":")
for i in v:
print(" - " + i)
elif args.action == "add":
def _profile_add(self, args):
matches = list(filter(lambda k: k == args.data[0], self.profiles))
if len(matches) > 0:
print("Profile {} Already exist".format(matches[0]))
@ -320,7 +334,8 @@ class connapp:
self.config._profiles_add(**newprofile)
self.config._saveconfig(self.config.file)
print("{} added succesfully".format(args.data[0]))
elif args.action == "mod":
def _profile_mod(self, args):
matches = list(filter(lambda k: k == args.data[0], self.profiles))
if len(matches) == 0:
print("{} not found".format(args.data[0]))
@ -344,9 +359,13 @@ class connapp:
def _func_others(self, args):
#Function called when using other commands
if args.command == "ls":
actions = {"ls": self._ls, "move": self._mvcp, "cp": self._mvcp, "bulk": self._bulk, "completion": self._completion, "case": self._case, "fzf": self._fzf, "idletime": self._idletime}
return actions.get(args.command)(args)
def _ls(self, args):
print(*getattr(self, args.data), sep="\n")
elif args.command == "move" or args.command == "cp":
def _mvcp(self, args):
if not self.case:
args.data[0] = args.data[0].lower()
args.data[1] = args.data[1].lower()
@ -374,11 +393,10 @@ class connapp:
if args.command == "move":
self.config._connections_del(**olduniques)
self.config._saveconfig(self.config.file)
if args.command == "move":
print("{} moved succesfully to {}".format(args.data[0],args.data[1]))
if args.command == "cp":
print("{} copied succesfully to {}".format(args.data[0],args.data[1]))
elif args.command == "bulk":
action = "moved" if args.command == "move" else "copied"
print("{} {} succesfully to {}".format(args.data[0],action, args.data[1]))
def _bulk(self, args):
newnodes = self._questions_bulk()
if newnodes == False:
exit(7)
@ -421,32 +439,44 @@ class connapp:
print("Succesfully added {} nodes".format(count))
else:
print("0 nodes added")
else:
if args.command == "completion":
def _completion(self, args):
if args.data[0] == "bash":
print(self._help("bashcompletion"))
elif args.data[0] == "zsh":
print(self._help("zshcompletion"))
else:
if args.command == "case":
def _case(self, args):
if args.data[0] == "true":
args.data[0] = True
elif args.data[0] == "false":
args.data[0] = False
if args.command == "fzf":
self._change_settings(args.command, args.data[0])
def _fzf(self, args):
if args.data[0] == "true":
args.data[0] = True
elif args.data[0] == "false":
args.data[0] = False
if args.command == "idletime":
self._change_settings(args.command, args.data[0])
def _idletime(self, args):
if args.data[0] < 0:
args.data[0] = 0
self.config.config[args.command] = args.data[0]
self._change_settings(args.command, args.data[0])
def _change_settings(self, name, value):
self.config.config[name] = value
self.config._saveconfig(self.config.file)
print("Config saved")
def _func_run(self, args):
if len(args.data) > 1:
args.action = "noderun"
actions = {"noderun": self._node_run, "generate": self._yaml_generate, "run": self._yaml_run}
return actions.get(args.action)(args)
def _node_run(self, args):
command = " ".join(args.data[1:])
command = command.split("-")
matches = list(filter(lambda k: k == args.data[0], self.nodes))
@ -457,8 +487,8 @@ class connapp:
node = self.node(matches[0],**node, config = self.config)
node.run(command)
print(node.output)
else:
if args.action == "generate":
def _yaml_generate(self, args):
if os.path.exists(args.data[0]):
print("File {} already exists".format(args.data[0]))
exit(14)
@ -468,6 +498,8 @@ class connapp:
file.close()
print("File {} generated succesfully".format(args.data[0]))
exit()
def _yaml_run(self, args):
try:
with open(args.data[0]) as file:
scripts = yaml.load(file, Loader=yaml.FullLoader)
@ -479,15 +511,14 @@ class connapp:
args = {}
try:
action = script["action"]
except:
print("Action is mandatory")
exit(11)
try:
nodelist = script["nodes"]
except:
print("Nodes list is mandatory")
args["commands"] = script["commands"]
output = script["output"]
if action == "test":
args["expected"] = script["expected"]
except KeyError as e:
print("'{}' is mandatory".format(e.args[0]))
exit(11)
# try:
for i in nodelist:
if isinstance(i, dict):
name = list(i.keys())[0]
@ -500,29 +531,6 @@ class connapp:
this = self.config.getitem(i)
nodes[i] = this
nodes = self.connnodes(nodes, config = self.config)
# except:
# print("Failed getting nodes")
# exit(12)
try:
args["commands"] = script["commands"]
except:
print("Commands list is mandatory")
exit(11)
if action == "test":
try:
args["expected"] = script["expected"]
except:
print("Expected is mandatory with action 'test'")
exit(11)
try:
args["vars"] = script["variables"]
except:
pass
try:
output = script["output"]
except:
print("output is mandatory")
exit(11)
stdout = False
if output is None:
pass
@ -530,14 +538,16 @@ class connapp:
stdout = True
elif isinstance(output, str) and action == "run":
args["folder"] = output
try:
args["vars"] = script["variables"]
except:
pass
try:
options = script["options"]
thisoptions = {k: v for k, v in options.items() if k in ["prompt", "parallel", "timeout"]}
args.update(thisoptions)
except:
options = None
if options is not None:
thisoptions = {k: v for k, v in options.items() if k in ["prompt", "parallel", "timeout"]}
print(thisoptions)
args.update(thisoptions)
size = str(os.get_terminal_size())
p = re.search(r'.*columns=([0-9]+)', size)
columns = int(p.group(1))
@ -1014,7 +1024,9 @@ tasks:
'''
if keyfile is None:
keyfile = self.config.key
key = RSA.import_key(open(keyfile).read())
with open(keyfile) as f:
key = RSA.import_key(f.read())
f.close()
publickey = key.publickey()
encryptor = PKCS1_OAEP.new(publickey)
password = encryptor.encrypt(password.encode("utf-8"))

View File

@ -92,13 +92,14 @@ class node:
else:
self.password = [password]
def __passtx(self, passwords, *, keyfile=None):
def _passtx(self, passwords, *, keyfile=None):
# decrypts passwords, used by other methdos.
dpass = []
if keyfile is None:
keyfile = self.key
if keyfile is not None:
key = RSA.import_key(open(keyfile).read())
with open(keyfile) as f:
key = RSA.import_key(f.read())
decryptor = PKCS1_OAEP.new(key)
for passwd in passwords:
if not re.match('^b[\"\'].+[\"\']$', passwd):
@ -147,6 +148,8 @@ class node:
t = ansi_escape.sub('', t)
t = t.lstrip(" \n\r")
t = t.replace("\r","")
t = t.replace("\x0E","")
t = t.replace("\x0F","")
if var == False:
d = open(logfile, "w")
d.write(t)
@ -163,7 +166,7 @@ class node:
def _keepalive(self):
#Send keepalive ctrl+e when idletime passed without new inputs on interact
self.lastinput = time()
t = threading.currentThread()
t = threading.current_thread()
while True:
if time() - self.lastinput >= self.idletime:
self.child.sendcontrol("e")
@ -208,7 +211,7 @@ class node:
print(connect)
exit(1)
def run(self, commands, vars = None,*, folder = '', prompt = r'>$|#$|\$$|>.$|#.$|\$.$', stdout = False, timeout = 10):
def run(self, commands, vars = None,*, folder = '', prompt = r'>$|#$|\$$|>.$|#.$|\$.$', stdout = False, timeout = 20):
'''
Run a command or list of commands on the node and return the output.
@ -241,7 +244,7 @@ class node:
default False.
- timeout (int):Time in seconds for expect to wait for prompt/EOF.
default 10.
default 20.
### Returns:
@ -292,7 +295,7 @@ class node:
f.close()
return connect
def test(self, commands, expected, vars = None,*, prompt = r'>$|#$|\$$|>.$|#.$|\$.$', timeout = 10):
def test(self, commands, expected, vars = None,*, prompt = r'>$|#$|\$$|>.$|#.$|\$.$', timeout = 20):
'''
Run a command or list of commands on the node, then check if expected value appears on the output after the last command.
@ -324,7 +327,7 @@ class node:
need some special symbol.
- timeout (int):Time in seconds for expect to wait for prompt/EOF.
default 10.
default 20.
### Returns:
bool: true if expected value is found after running the commands
@ -390,7 +393,7 @@ class node:
if self.logs != '':
self.logfile = self._logfile()
if self.password[0] != '':
passwords = self.__passtx(self.password)
passwords = self._passtx(self.password)
else:
passwords = []
expects = ['yes/no', 'refused', 'supported', 'cipher', 'sage', 'timeout', 'unavailable', 'closed', '[p|P]assword:|[u|U]sername:', r'>$|#$|\$$|>.$|#.$|\$.$', 'suspend', pexpect.EOF, pexpect.TIMEOUT, "No route to host", "resolve hostname", "no matching host key"]
@ -403,7 +406,7 @@ class node:
if self.logs != '':
self.logfile = self._logfile()
if self.password[0] != '':
passwords = self.__passtx(self.password)
passwords = self._passtx(self.password)
else:
passwords = []
expects = ['[u|U]sername:', 'refused', 'supported', 'cipher', 'sage', 'timeout', 'unavailable', 'closed', '[p|P]assword:', r'>$|#$|\$$|>.$|#.$|\$.$', 'suspend', pexpect.EOF, pexpect.TIMEOUT, "No route to host", "resolve hostname", "no matching host key"]

View File

@ -424,14 +424,18 @@ __pdoc__ = {
self.profiles = config[&#34;profiles&#34;]
if not os.path.exists(self.key):
self._createkey(self.key)
self.privatekey = RSA.import_key(open(self.key).read())
with open(self.key) as f:
self.privatekey = RSA.import_key(f.read())
f.close()
self.publickey = self.privatekey.publickey()
def _loadconfig(self, conf):
#Loads config file
jsonconf = open(conf)
return json.load(jsonconf)
jsondata = json.load(jsonconf)
jsonconf.close()
return jsondata
def _createconfig(self, conf):
#Create config file
@ -442,7 +446,9 @@ __pdoc__ = {
f.close()
os.chmod(conf, 0o600)
jsonconf = open(conf)
return json.load(jsonconf)
jsondata = json.load(jsonconf)
jsonconf.close()
return jsondata
def _saveconfig(self, conf):
#Save config file
@ -461,6 +467,7 @@ __pdoc__ = {
f.write(key.export_key(&#39;PEM&#39;))
f.close()
os.chmod(keyfile, 0o600)
return key
def _explode_unique(self, unique):
#Divide unique name into folder, subfolder and id
@ -789,9 +796,13 @@ __pdoc__ = {
#Function called when connecting or managing nodes.
if not self.case and args.data != None:
args.data = args.data.lower()
if args.action == &#34;version&#34;:
actions = {&#34;version&#34;: self._version, &#34;connect&#34;: self._connect, &#34;debug&#34;: self._connect, &#34;add&#34;: self._add, &#34;del&#34;: self._del, &#34;mod&#34;: self._mod, &#34;show&#34;: self._show}
return actions.get(args.action)(args)
def _version(self, args):
print(__version__)
if args.action == &#34;connect&#34; or args.action == &#34;debug&#34;:
def _connect(self, args):
if args.data == None:
matches = self.nodes
if len(matches) == 0:
@ -816,7 +827,8 @@ __pdoc__ = {
node.interact(debug = True)
else:
node.interact()
elif args.action == &#34;del&#34;:
def _del(self, args):
if args.data == None:
print(&#34;Missing argument node&#34;)
exit(3)
@ -829,6 +841,8 @@ __pdoc__ = {
exit(2)
question = [inquirer.Confirm(&#34;delete&#34;, message=&#34;Are you sure you want to delete {}?&#34;.format(matches[0]))]
confirm = inquirer.prompt(question)
if confirm == None:
exit(7)
if confirm[&#34;delete&#34;]:
uniques = self.config._explode_unique(matches[0])
if args.data.startswith(&#34;@&#34;):
@ -837,7 +851,8 @@ __pdoc__ = {
self.config._connections_del(**uniques)
self.config._saveconfig(self.config.file)
print(&#34;{} deleted succesfully&#34;.format(matches[0]))
elif args.action == &#34;add&#34;:
def _add(self, args):
if args.data == None:
print(&#34;Missing argument node&#34;)
exit(3)
@ -869,7 +884,6 @@ __pdoc__ = {
self.config._folder_add(**uniques)
self.config._saveconfig(self.config.file)
print(&#34;{} added succesfully&#34;.format(args.data))
if type == &#34;node&#34;:
nodefolder = args.data.partition(&#34;@&#34;)
nodefolder = &#34;@&#34; + nodefolder[2]
@ -890,7 +904,8 @@ __pdoc__ = {
self.config._connections_add(**newnode)
self.config._saveconfig(self.config.file)
print(&#34;{} added succesfully&#34;.format(args.data))
elif args.action == &#34;show&#34;:
def _show(self, args):
if args.data == None:
print(&#34;Missing argument node&#34;)
exit(3)
@ -906,7 +921,8 @@ __pdoc__ = {
print(k + &#34;:&#34;)
for i in v:
print(&#34; - &#34; + i)
elif args.action == &#34;mod&#34;:
def _mod(self, args):
if args.data == None:
print(&#34;Missing argument node&#34;)
exit(3)
@ -937,7 +953,10 @@ __pdoc__ = {
#Function called when managing profiles
if not self.case:
args.data[0] = args.data[0].lower()
if args.action == &#34;del&#34;:
actions = {&#34;add&#34;: self._profile_add, &#34;del&#34;: self._profile_del, &#34;mod&#34;: self._profile_mod, &#34;show&#34;: self._profile_show}
return actions.get(args.action)(args)
def _profile_del(self, args):
matches = list(filter(lambda k: k == args.data[0], self.profiles))
if len(matches) == 0:
print(&#34;{} not found&#34;.format(args.data[0]))
@ -956,7 +975,8 @@ __pdoc__ = {
self.config._profiles_del(id = matches[0])
self.config._saveconfig(self.config.file)
print(&#34;{} deleted succesfully&#34;.format(matches[0]))
elif args.action == &#34;show&#34;:
def _profile_show(self, args):
matches = list(filter(lambda k: k == args.data[0], self.profiles))
if len(matches) == 0:
print(&#34;{} not found&#34;.format(args.data[0]))
@ -969,7 +989,8 @@ __pdoc__ = {
print(k + &#34;:&#34;)
for i in v:
print(&#34; - &#34; + i)
elif args.action == &#34;add&#34;:
def _profile_add(self, args):
matches = list(filter(lambda k: k == args.data[0], self.profiles))
if len(matches) &gt; 0:
print(&#34;Profile {} Already exist&#34;.format(matches[0]))
@ -980,7 +1001,8 @@ __pdoc__ = {
self.config._profiles_add(**newprofile)
self.config._saveconfig(self.config.file)
print(&#34;{} added succesfully&#34;.format(args.data[0]))
elif args.action == &#34;mod&#34;:
def _profile_mod(self, args):
matches = list(filter(lambda k: k == args.data[0], self.profiles))
if len(matches) == 0:
print(&#34;{} not found&#34;.format(args.data[0]))
@ -1004,9 +1026,13 @@ __pdoc__ = {
def _func_others(self, args):
#Function called when using other commands
if args.command == &#34;ls&#34;:
actions = {&#34;ls&#34;: self._ls, &#34;move&#34;: self._mvcp, &#34;cp&#34;: self._mvcp, &#34;bulk&#34;: self._bulk, &#34;completion&#34;: self._completion, &#34;case&#34;: self._case, &#34;fzf&#34;: self._fzf, &#34;idletime&#34;: self._idletime}
return actions.get(args.command)(args)
def _ls(self, args):
print(*getattr(self, args.data), sep=&#34;\n&#34;)
elif args.command == &#34;move&#34; or args.command == &#34;cp&#34;:
def _mvcp(self, args):
if not self.case:
args.data[0] = args.data[0].lower()
args.data[1] = args.data[1].lower()
@ -1034,11 +1060,10 @@ __pdoc__ = {
if args.command == &#34;move&#34;:
self.config._connections_del(**olduniques)
self.config._saveconfig(self.config.file)
if args.command == &#34;move&#34;:
print(&#34;{} moved succesfully to {}&#34;.format(args.data[0],args.data[1]))
if args.command == &#34;cp&#34;:
print(&#34;{} copied succesfully to {}&#34;.format(args.data[0],args.data[1]))
elif args.command == &#34;bulk&#34;:
action = &#34;moved&#34; if args.command == &#34;move&#34; else &#34;copied&#34;
print(&#34;{} {} succesfully to {}&#34;.format(args.data[0],action, args.data[1]))
def _bulk(self, args):
newnodes = self._questions_bulk()
if newnodes == False:
exit(7)
@ -1081,32 +1106,44 @@ __pdoc__ = {
print(&#34;Succesfully added {} nodes&#34;.format(count))
else:
print(&#34;0 nodes added&#34;)
else:
if args.command == &#34;completion&#34;:
def _completion(self, args):
if args.data[0] == &#34;bash&#34;:
print(self._help(&#34;bashcompletion&#34;))
elif args.data[0] == &#34;zsh&#34;:
print(self._help(&#34;zshcompletion&#34;))
else:
if args.command == &#34;case&#34;:
def _case(self, args):
if args.data[0] == &#34;true&#34;:
args.data[0] = True
elif args.data[0] == &#34;false&#34;:
args.data[0] = False
if args.command == &#34;fzf&#34;:
self._change_settings(args.command, args.data[0])
def _fzf(self, args):
if args.data[0] == &#34;true&#34;:
args.data[0] = True
elif args.data[0] == &#34;false&#34;:
args.data[0] = False
if args.command == &#34;idletime&#34;:
self._change_settings(args.command, args.data[0])
def _idletime(self, args):
if args.data[0] &lt; 0:
args.data[0] = 0
self.config.config[args.command] = args.data[0]
self._change_settings(args.command, args.data[0])
def _change_settings(self, name, value):
self.config.config[name] = value
self.config._saveconfig(self.config.file)
print(&#34;Config saved&#34;)
def _func_run(self, args):
if len(args.data) &gt; 1:
args.action = &#34;noderun&#34;
actions = {&#34;noderun&#34;: self._node_run, &#34;generate&#34;: self._yaml_generate, &#34;run&#34;: self._yaml_run}
return actions.get(args.action)(args)
def _node_run(self, args):
command = &#34; &#34;.join(args.data[1:])
command = command.split(&#34;-&#34;)
matches = list(filter(lambda k: k == args.data[0], self.nodes))
@ -1117,8 +1154,8 @@ __pdoc__ = {
node = self.node(matches[0],**node, config = self.config)
node.run(command)
print(node.output)
else:
if args.action == &#34;generate&#34;:
def _yaml_generate(self, args):
if os.path.exists(args.data[0]):
print(&#34;File {} already exists&#34;.format(args.data[0]))
exit(14)
@ -1128,6 +1165,8 @@ __pdoc__ = {
file.close()
print(&#34;File {} generated succesfully&#34;.format(args.data[0]))
exit()
def _yaml_run(self, args):
try:
with open(args.data[0]) as file:
scripts = yaml.load(file, Loader=yaml.FullLoader)
@ -1139,15 +1178,14 @@ __pdoc__ = {
args = {}
try:
action = script[&#34;action&#34;]
except:
print(&#34;Action is mandatory&#34;)
exit(11)
try:
nodelist = script[&#34;nodes&#34;]
except:
print(&#34;Nodes list is mandatory&#34;)
args[&#34;commands&#34;] = script[&#34;commands&#34;]
output = script[&#34;output&#34;]
if action == &#34;test&#34;:
args[&#34;expected&#34;] = script[&#34;expected&#34;]
except KeyError as e:
print(&#34;&#39;{}&#39; is mandatory&#34;.format(e.args[0]))
exit(11)
# try:
for i in nodelist:
if isinstance(i, dict):
name = list(i.keys())[0]
@ -1160,29 +1198,6 @@ __pdoc__ = {
this = self.config.getitem(i)
nodes[i] = this
nodes = self.connnodes(nodes, config = self.config)
# except:
# print(&#34;Failed getting nodes&#34;)
# exit(12)
try:
args[&#34;commands&#34;] = script[&#34;commands&#34;]
except:
print(&#34;Commands list is mandatory&#34;)
exit(11)
if action == &#34;test&#34;:
try:
args[&#34;expected&#34;] = script[&#34;expected&#34;]
except:
print(&#34;Expected is mandatory with action &#39;test&#39;&#34;)
exit(11)
try:
args[&#34;vars&#34;] = script[&#34;variables&#34;]
except:
pass
try:
output = script[&#34;output&#34;]
except:
print(&#34;output is mandatory&#34;)
exit(11)
stdout = False
if output is None:
pass
@ -1190,14 +1205,16 @@ __pdoc__ = {
stdout = True
elif isinstance(output, str) and action == &#34;run&#34;:
args[&#34;folder&#34;] = output
try:
args[&#34;vars&#34;] = script[&#34;variables&#34;]
except:
pass
try:
options = script[&#34;options&#34;]
thisoptions = {k: v for k, v in options.items() if k in [&#34;prompt&#34;, &#34;parallel&#34;, &#34;timeout&#34;]}
args.update(thisoptions)
except:
options = None
if options is not None:
thisoptions = {k: v for k, v in options.items() if k in [&#34;prompt&#34;, &#34;parallel&#34;, &#34;timeout&#34;]}
print(thisoptions)
args.update(thisoptions)
size = str(os.get_terminal_size())
p = re.search(r&#39;.*columns=([0-9]+)&#39;, size)
columns = int(p.group(1))
@ -1674,7 +1691,9 @@ tasks:
&#39;&#39;&#39;
if keyfile is None:
keyfile = self.config.key
key = RSA.import_key(open(keyfile).read())
with open(keyfile) as f:
key = RSA.import_key(f.read())
f.close()
publickey = key.publickey()
encryptor = PKCS1_OAEP.new(publickey)
password = encryptor.encrypt(password.encode(&#34;utf-8&#34;))
@ -1720,7 +1739,9 @@ tasks:
&#39;&#39;&#39;
if keyfile is None:
keyfile = self.config.key
key = RSA.import_key(open(keyfile).read())
with open(keyfile) as f:
key = RSA.import_key(f.read())
f.close()
publickey = key.publickey()
encryptor = PKCS1_OAEP.new(publickey)
password = encryptor.encrypt(password.encode(&#34;utf-8&#34;))
@ -1937,13 +1958,14 @@ tasks:
else:
self.password = [password]
def __passtx(self, passwords, *, keyfile=None):
def _passtx(self, passwords, *, keyfile=None):
# decrypts passwords, used by other methdos.
dpass = []
if keyfile is None:
keyfile = self.key
if keyfile is not None:
key = RSA.import_key(open(keyfile).read())
with open(keyfile) as f:
key = RSA.import_key(f.read())
decryptor = PKCS1_OAEP.new(key)
for passwd in passwords:
if not re.match(&#39;^b[\&#34;\&#39;].+[\&#34;\&#39;]$&#39;, passwd):
@ -1992,6 +2014,8 @@ tasks:
t = ansi_escape.sub(&#39;&#39;, t)
t = t.lstrip(&#34; \n\r&#34;)
t = t.replace(&#34;\r&#34;,&#34;&#34;)
t = t.replace(&#34;\x0E&#34;,&#34;&#34;)
t = t.replace(&#34;\x0F&#34;,&#34;&#34;)
if var == False:
d = open(logfile, &#34;w&#34;)
d.write(t)
@ -2008,7 +2032,7 @@ tasks:
def _keepalive(self):
#Send keepalive ctrl+e when idletime passed without new inputs on interact
self.lastinput = time()
t = threading.currentThread()
t = threading.current_thread()
while True:
if time() - self.lastinput &gt;= self.idletime:
self.child.sendcontrol(&#34;e&#34;)
@ -2053,7 +2077,7 @@ tasks:
print(connect)
exit(1)
def run(self, commands, vars = None,*, folder = &#39;&#39;, prompt = r&#39;&gt;$|#$|\$$|&gt;.$|#.$|\$.$&#39;, stdout = False, timeout = 10):
def run(self, commands, vars = None,*, folder = &#39;&#39;, prompt = r&#39;&gt;$|#$|\$$|&gt;.$|#.$|\$.$&#39;, stdout = False, timeout = 20):
&#39;&#39;&#39;
Run a command or list of commands on the node and return the output.
@ -2086,7 +2110,7 @@ tasks:
default False.
- timeout (int):Time in seconds for expect to wait for prompt/EOF.
default 10.
default 20.
### Returns:
@ -2137,7 +2161,7 @@ tasks:
f.close()
return connect
def test(self, commands, expected, vars = None,*, prompt = r&#39;&gt;$|#$|\$$|&gt;.$|#.$|\$.$&#39;, timeout = 10):
def test(self, commands, expected, vars = None,*, prompt = r&#39;&gt;$|#$|\$$|&gt;.$|#.$|\$.$&#39;, timeout = 20):
&#39;&#39;&#39;
Run a command or list of commands on the node, then check if expected value appears on the output after the last command.
@ -2169,7 +2193,7 @@ tasks:
need some special symbol.
- timeout (int):Time in seconds for expect to wait for prompt/EOF.
default 10.
default 20.
### Returns:
bool: true if expected value is found after running the commands
@ -2235,7 +2259,7 @@ tasks:
if self.logs != &#39;&#39;:
self.logfile = self._logfile()
if self.password[0] != &#39;&#39;:
passwords = self.__passtx(self.password)
passwords = self._passtx(self.password)
else:
passwords = []
expects = [&#39;yes/no&#39;, &#39;refused&#39;, &#39;supported&#39;, &#39;cipher&#39;, &#39;sage&#39;, &#39;timeout&#39;, &#39;unavailable&#39;, &#39;closed&#39;, &#39;[p|P]assword:|[u|U]sername:&#39;, r&#39;&gt;$|#$|\$$|&gt;.$|#.$|\$.$&#39;, &#39;suspend&#39;, pexpect.EOF, pexpect.TIMEOUT, &#34;No route to host&#34;, &#34;resolve hostname&#34;, &#34;no matching host key&#34;]
@ -2248,7 +2272,7 @@ tasks:
if self.logs != &#39;&#39;:
self.logfile = self._logfile()
if self.password[0] != &#39;&#39;:
passwords = self.__passtx(self.password)
passwords = self._passtx(self.password)
else:
passwords = []
expects = [&#39;[u|U]sername:&#39;, &#39;refused&#39;, &#39;supported&#39;, &#39;cipher&#39;, &#39;sage&#39;, &#39;timeout&#39;, &#39;unavailable&#39;, &#39;closed&#39;, &#39;[p|P]assword:&#39;, r&#39;&gt;$|#$|\$$|&gt;.$|#.$|\$.$&#39;, &#39;suspend&#39;, pexpect.EOF, pexpect.TIMEOUT, &#34;No route to host&#34;, &#34;resolve hostname&#34;, &#34;no matching host key&#34;]
@ -2352,7 +2376,7 @@ tasks:
</details>
</dd>
<dt id="connpy.node.run"><code class="name flex">
<span>def <span class="ident">run</span></span>(<span>self, commands, vars=None, *, folder='', prompt=&#x27;&gt;$|#$|\\$$|&gt;.$|#.$|\\$.$&#x27;, stdout=False, timeout=10)</span>
<span>def <span class="ident">run</span></span>(<span>self, commands, vars=None, *, folder='', prompt=&#x27;&gt;$|#$|\\$$|&gt;.$|#.$|\\$.$&#x27;, stdout=False, timeout=20)</span>
</code></dt>
<dd>
<div class="desc"><p>Run a command or list of commands on the node and return the output.</p>
@ -2382,7 +2406,7 @@ tasks:
default False.
- timeout (int):Time in seconds for expect to wait for prompt/EOF.
default 10.
default 20.
</code></pre>
<h3 id="returns">Returns:</h3>
<pre><code>str: Output of the commands you ran on the node.
@ -2391,7 +2415,7 @@ tasks:
<summary>
<span>Expand source code</span>
</summary>
<pre><code class="python">def run(self, commands, vars = None,*, folder = &#39;&#39;, prompt = r&#39;&gt;$|#$|\$$|&gt;.$|#.$|\$.$&#39;, stdout = False, timeout = 10):
<pre><code class="python">def run(self, commands, vars = None,*, folder = &#39;&#39;, prompt = r&#39;&gt;$|#$|\$$|&gt;.$|#.$|\$.$&#39;, stdout = False, timeout = 20):
&#39;&#39;&#39;
Run a command or list of commands on the node and return the output.
@ -2424,7 +2448,7 @@ tasks:
default False.
- timeout (int):Time in seconds for expect to wait for prompt/EOF.
default 10.
default 20.
### Returns:
@ -2477,7 +2501,7 @@ tasks:
</details>
</dd>
<dt id="connpy.node.test"><code class="name flex">
<span>def <span class="ident">test</span></span>(<span>self, commands, expected, vars=None, *, prompt=&#x27;&gt;$|#$|\\$$|&gt;.$|#.$|\\$.$&#x27;, timeout=10)</span>
<span>def <span class="ident">test</span></span>(<span>self, commands, expected, vars=None, *, prompt=&#x27;&gt;$|#$|\\$$|&gt;.$|#.$|\\$.$&#x27;, timeout=20)</span>
</code></dt>
<dd>
<div class="desc"><p>Run a command or list of commands on the node, then check if expected value appears on the output after the last command.</p>
@ -2506,7 +2530,7 @@ tasks:
need some special symbol.
- timeout (int):Time in seconds for expect to wait for prompt/EOF.
default 10.
default 20.
</code></pre>
<h3 id="returns">Returns:</h3>
<pre><code>bool: true if expected value is found after running the commands
@ -2516,7 +2540,7 @@ tasks:
<summary>
<span>Expand source code</span>
</summary>
<pre><code class="python">def test(self, commands, expected, vars = None,*, prompt = r&#39;&gt;$|#$|\$$|&gt;.$|#.$|\$.$&#39;, timeout = 10):
<pre><code class="python">def test(self, commands, expected, vars = None,*, prompt = r&#39;&gt;$|#$|\$$|&gt;.$|#.$|\$.$&#39;, timeout = 20):
&#39;&#39;&#39;
Run a command or list of commands on the node, then check if expected value appears on the output after the last command.
@ -2548,7 +2572,7 @@ tasks:
need some special symbol.
- timeout (int):Time in seconds for expect to wait for prompt/EOF.
default 10.
default 20.
### Returns:
bool: true if expected value is found after running the commands