cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
407
Views
1
Helpful
5
Replies

Any idea to change [if password != password:] in the netmiko script

train7654
Level 1
Level 1

Hi i am new to python and currently learning the basic of netmiko . I built 3 scripts as below;

device_1.py  : contain device list , i do not input username/pw in the script for security purpose

my_tool.py    : prompt username/PW

config.py  : main body

I attached a txt file containing all the scripts.

-----------

Question:

If i entered the pw correctly the script, goes without any problem. But if i try to enter the pw incorrectly, the output does not go

to loop  (Password do not match. Try again.) , instead it go to output like below:

root@NetworkAutomation-1:~# python3 netmiko1.py
Enter Username: admin
Enter Password
##################################################
failed to connect to 10.10.10.101
##################################################

 

i think the problem lies in , my_tool.py script at [ if password != password: ] . How can i change this to make the pw loop appear.

Thank you in advance 

 

 

 

5 Replies 5

Looks like you’re problem in the my_tool.pyscript is that the password variable is not being compared to itself, but rather to a new string 'password'that is being created inside the ifstatement. This means that the if password != password condition will always be true, and the script will not prompt the you/the user for a new password if they enter the incorrect password.

If you modify the get_credentials_1 function in the my_tool.py script to compare the password variable to a new, separate string that is not being used as a variable.

Hope this helps.

Please mark this as helpful or solution accepted to help others
Connect with me https://bigevilbeard.github.io

Thank you for the reply.

 I created another variable named [ confirm_password ], and let it be compared in the if statement , and it worked just fine. But if i do this , user need to fill pw 2 times . If i want the user enter the pw only ONCE , and let the pw be compared to the pw information pull from the network device , what is need to be alter? This is the problem that i want to solve.  

from getpass import getpass

def get_input(prompt=''
try:
line = input(prompt)
except NameError:
line = input(prompt)
return line


def get_credentials_1():
"""Prompt for and return a username and password."""
username = get_input('Enter Username: ')
password = None
while not password:
password = getpass('Enter Password: ')
confirm_password = getpass('Confirm Password: ')
if password != confirm_password :
print('Passwords do not match. Try again.')
password = None
return username, password

 

 

 

No problem happy to help. To enter their password once, you can modify yourget_credentials_1 function

def get_credentials_2():
    username = get_input('Enter Username: ')
    password = None
    while not password:
        password = getpass('Enter Password: ')
        if password:
            confirm_password = getpass('Re-enter Password: ')
            if password != confirm_password :
                print('Passwords do not match.  Try again.')
                password = None
    return username, password
 
Excuse the formatting, using mobile to relpy and there is no code block function. In the updated version, the confirm_password variable is only used if the you have entered a password, which avoids the need to request to enter their password twice.
 
Hope this helps.
Please mark this as helpful or solution accepted to help others
Connect with me https://bigevilbeard.github.io

Yes i aware the function of the [confirm_password] ,but this is not what i want. If i run the code i get below output . First

I need to fill pw in('Enter Password: '), then i need to enter pw again ('Re-enter Password: ') , 2 times pw filling . All i want to make is  :

1) login to the device with just  ('Enter Password: '),  without the need to enter ('Re-enter Password: ')

2) If pw is entered incorrectly at  ('Enter Password: ') , it need to prompt ('Passwords do not match.  Try again.') ,not ('Re-enter Password: ')

To conclude it all, i want pw is checked if right/wrong after i fill the pw at ('Enter Password: ') .

//like login to cisco switch/router, we do not asked to enter 'Re-enter Password: '

I know i need to create some variable and assign it to something in the below (if) statement to let it be compared to [if password], but

i still struggle to find it out . I can create a variable and assign it to the pw i created ,and let it be compared in the (if) statement, and it worked  , but i dont want my pw to be shown in the code for security reason

if password != [xxxxxxxx]:

 

==========================

[The Output]

root@NetworkAutomation-1:~# python3 netmiko1.py
Enter Username: admin
Enter Password:
Re-enter Password:

###sh ip int br output##

None
root@NetworkAutomation-1:~#

===================================

Try, instead of checking if password != password, (which will always be false), first check if password is falsy (i.e., empty or evaluates to False). If the password is empty, you can prompt to enter a non-empty password. If password is not empty, prompt to confirm the password by entering it again.

This code should compares the original password and the confirm_password. If they don't match, it then print an error message indicating that the passwords does not match, and reset password to None, which will force to enter the password again from the beginning of the loop. If the passwords match, it will return the username and password from the function.

 

from getpass import getpass

def get_input(prompt=''):
    try:
        line = input(prompt)
    except NameError:
        line = input(prompt)
    return line

def get_credentials_1():
    username = get_input('Enter Username: ')
    password = None
    while not password:
        password = getpass('Enter Password: ')
        if not password:
            print('Password cannot be empty. Try again.')
        else:
            confirm_password = getpass('Confirm Password: ')
            if password != confirm_password:
                print('Passwords do not match. Try again.')
                password = None
    return username, password

 

 

Please mark this as helpful or solution accepted to help others
Connect with me https://bigevilbeard.github.io
Review Cisco Networking for a $25 gift card