Quote:Did I miss something?
Yes. I think you need to learn more about truthiness in Python and about function definitions.
The username is supplied in the function definition as the second argument and if you don't call the function with a
username
, then
None
is assigned to the
username
. This is used for the different extension of
cmd
.
Quote:1. replace username and password with real ones - line 32
The name username comes from the function-call. Look at the function definition:
def get_psinfo(host, username=None, password=""):
If
username
is not supplied as argument or keyword-argument, then
username
is assigned to
None
.
A
bool(None)
returns
False
, which is important in this case, because I use it where you think you have to replace it, but it's not right.
Then the list for the command is extended. If the user has called the function with
username
, then
bool(username)
is True and the first branch is executed. If
username
is
None
, then the second branch (else) is executed.
# username and password is mandatory for remote access
if username:
cmd += ["-d", "-nobanner", "-accepteula", host, "-u", username, "-p", password]
else:
cmd += ["-d", "-nobanner", "-accepteula", host]
Afterwards,
cmd
is used and the elements of
cmd
depends on the function-call. If no username was supplied, the cmd without
"-u", username
is used.
Quote:2. replace DESKTOP-S5MOQUE for my local machine - line 51
3. replace "DESKTOP-S5MOQUE", "Andre" with remote machine - line 52
Yes, this is right.
Just use the function call.
To understand truthiness a bit better, test this code:
def is_set(username=None):
if username:
print("bool(username) is True")
else:
print("bool(username) is False")
# all False
is_set()
is_set(None) # same call as above, but explicit supplying None as first argument
is_set("") # empty str is False
is_set(False) # False
is_set([]) # empty list is False
# all True
is_set(True)
is_set("string is not empty2") # True
is_set(print) # bool(function) is also True