10-25-2022 07:15 AM
I am running a script with Netmiko to erase a username on around 2000 switches, It is working but when the user was already erasing it, netmiko shows an error because is expecting a confirmation to delete, how can I condition expect to have two possible outcomes
When the user is in the configuration there is not problem, but is not there, netmiko is expecting a string 'confirm'
output=net_connect.send_command('no username XXXXXXXX',
expect_string = r'confirm')
output+=net_connect.send_command('\n', expect_string=r'#') #Confirm command
Solved! Go to Solution.
10-25-2022 11:44 AM
in that case, first, i would get a validation username make a CSV file, use that CSV file as input - user condition to get device IP and username as condition start deleting the username.
10-25-2022 07:19 AM
are you removing the username, which was used to login used in netmiko ?
10-25-2022 11:04 AM
10-25-2022 11:44 AM
in that case, first, i would get a validation username make a CSV file, use that CSV file as input - user condition to get device IP and username as condition start deleting the username.
10-27-2022 06:17 AM
That was the option, thanks for the response, I did create a file with user from the switch and validate the username
01-25-2023 04:30 AM
Thanks, I managed to solve like this
output1= net_connect.send_command('sh run | in username xxxxx')
net_connect.config_mode()
net_connect.find_prompt()
#Send commands on config mode
with open('users.txt','w+') as d:
user_list = d.write(output1)
with open('users.txt','r') as d: #reading file with users
username = d.read().split()
if os.path.getsize('users.txt') != 0: #if file is not empty
if username[1] in 'xxxxxxxl': #if user is xxxxxx
output=net_connect.send_command('no username g.otero.local',)
expect_string = r'confirm')
output+=net_connect.send_command('\n', expect_string=r'#') #Confirm command
else: print('xxxxxx no found')
10-25-2022 01:28 PM
Hello,
I am setting up a test lab to verify this, but I think you need something like the below to skip the prompt:
output=net_connect.send_command('no username XXXXXXXX',
expect_string = r'confirm')
output+=net_connect.send_command('\n', expect_string=r'#' #Confirm command
strip_prompt=False,
strip_command=False
)
01-23-2023 08:22 PM
Hi. I had the same problem and for the last 2 days I've fighting against this issue. I know it might be kind of late but I managed to solve it by using this code:
net_connect.config_mode()
for cmd in config_commands:
net_connect.send_command_timing(cmd) #this runs "no username X" for each user stored in config_commands
connection.exit_config_mode()
connection.send_command("wr")
it takes a while to execute (around 30 seconds per device) but at least you won't have to delete users manually, which is always prone to errors. I hope you find this answer useful
01-23-2023 08:25 PM
net_connect or connection was the variable used in
connection = ConnectHandler(**device)
By mistake my first line says "net_connect". Just keep the same variable. Apologies
01-24-2023 12:46 PM
what is the issue here?
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