When using is instead of == in the first comparison statement
if host.lower() is 'localhost':
The host argument has a string called 'localhost' passed by a calling method so though the two strings are same,the control flow is running the else part of the code making the two values are not same.
If I use == instead of is than the program throws following error message.Below
To solve the issue I also defined the command variable before the if statement starts but also its not solving the problem. what exactly am I missing ?
def makeCommand(self, user, host, filepath, filteruser, sudo):
"""
Two types of commands with or without ssh depends on hostname
Adding sudo or not depends on sudo passed
"""
if host.lower() is 'localhost':
if sudo.lower() == 'yes':
command = ["sudo", "cat",
"{filepath}".format(filepath=filepath)]
if sudo.lower == "no":
command = ["cat", "{filepath}".format(filepath=filepath)]
else:
if sudo.lower() == 'yes':
command = ["ssh", "{user}@{host}".format(
user=user, host=host), "sudo", "cat", "{filepath}".format(filepath=filepath)]
else:
command = ["ssh", "{user}@{host}".format(
user=user, host=host), "cat", "{filepath}".format(filepath=filepath)]
userfilter = ["egrep", "-v",
"\"{filteruser}\"".format(filteruser=filteruser)]
return command, userfilter
Error:
Traceback (most recent call last):
File "/home/tara/taraproject/checkaccessEnv/project2/plugins/plugin_etc_passwd/plugin_etc_passwd.py", line 68, in run
self.user, self.host, self.fileloc, self.filter, self.sudo)
File "/home/tara/taraproject/checkaccessEnv/project2/lib/sshplugin/sshplugin.py", line 34, in makeCommand
return command, userfilter
UnboundLocalError: local variable 'command' referenced before assignment