03-31-2024 12:07 PM
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
03-31-2024 01:17 PM - edited 03-31-2024 01:18 PM
Looks like you’re problem in the my_tool.py
script is that the password
variable is not being compared to itself, but rather to a new string 'password'
that is being created inside the if
statement. 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.
03-31-2024 10:42 PM
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
04-01-2024 04:49 AM
No problem happy to help. To enter their password once, you can modify yourget_credentials_1
function
confirm_password
variable is only used if the you have entered a password, which avoids the need to request to enter their password twice.04-01-2024 10:04 AM
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:~#
===================================
04-02-2024 03:20 AM
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
Discover and save your favorite ideas. Come back to expert answers, step-by-step guides, recent topics, and more.
New here? Get started with these tips. How to use Community New member guide