cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
164
Views
0
Helpful
1
Replies

Remove Users script using Netmiko

bryg0d
Level 1
Level 1

Hi everyone,

I am trying to create a script to remove unwanted users/usernames in our network equipments. I am currently using netmiko 4.2.0 and python 3.7.9. I am already connected via SSH but can't seem to push the commands through. Here is my code below and the output after running the script. If you guys have any suggestion please big thanks!

usernames_to_remove = ["username1", "username1"]

try:
# Connect to the device
print(f"Connecting to {device_ip}")
ssh_client = ConnectHandler(**device)

# Display current usernames before configuration
print(f"Current usernames on {device_ip} (before configuration):")
current_usernames_before = ssh_client.send_command("show running-config | include username")
print(current_usernames_before)

# Remove usernames
for username in usernames_to_remove:
config_command = f"no username {username}"
output = ssh_client.send_command_(config_command)

# Save the configuration
output += ssh_client.send_command_timing("wr memory")

print(f"Configuration applied successfully on {device_ip}")
print(output)

# Display current usernames after configuration
print(f"Current usernames on {device_ip} (after configuration):")
current_usernames_after = ssh_client.send_command("show running-config | include username")
print(current_usernames_after)

# Disconnect from the device
ssh_client.disconnect()


== OUTPUT AFTER RUNNING THE SCRIPT ==

Connecting to 172.16.222.12
Current usernames on 172.16.222.12 (before configuration):
username adminuser privilege 15 secret 9 $9$//hb7V8B9eiLHw$fQxcjZ3WEXVT3ResZBUC/b.Lg3ruiKlz3bMs3BF
username username1 privilege 15 secret 9 $9$XyNW80Yyvb9EOg$oEdPdvXcibt6aWD3cXPqa1fRd0EbjFrfDc5n9OP
username username2 privilege 15 secret 9 $9$KNohPABLAwt8ur$EIg02hp5opnHRPDRzImbxoUpVQhP93XzA9XGAX1
Configuration applied successfully on 172.16.222.12
n
OLD-D_GF-ASW1(config)#o username mark
^
% Invalid input detected at '^' marker.
end
OLD-D_GF-ASW1#Building configuration...
[OK]
Current usernames on 172.16.222.12 (after configuration):
username adminuser privilege 15 secret 9 $9$//hb7V8B9eiLHw$fQxcjZ3WEXVT3ResZBUC/b.Lg3ruiKlz3bMs3BF
username username1 privilege 15 secret 9 $9$XyNW80Yyvb9EOg$oEdPdvXcibt6aWD3cXPqa1fRd0EbjFrfDc5n9OP
username username2 privilege 15 secret 9 $9$KNohPABLAwt8ur$EIg02hp5opnHRPDRzImbxoUpVQhP93XzA9XGAX1
Script execution complete.

1 Reply 1

Torbjørn
Spotlight
Spotlight

I am not quite sure how your script produces that output, but there seems to be an extra "_" symbol in "ssh_client.send_command_(config_command)" that could be causing an issue. I would recommend using "send_config_set" instead of "send_command" to alter device configuration with Netmiko. You can read more about this here: https://pyneng.readthedocs.io/en/latest/book/18_ssh_telnet/netmiko.html

I would rewrite the # Remove usernames block like so:

config_list = []
for username in usernames_to_remove:
command = f"no username {username}"
config_list.append(command)
output = ssh_client.send_config_set(config_list)

# Or the more concise version with list comprehension
config_list = [ f"no username {username}" for username in usernames_to_remove ]
output = ssh_client.send_config_set(config_list)

 

Happy to help! Please mark as helpful/solution if applicable.
Get in touch: https://torbjorn.dev