<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Remove Users script using Netmiko in Tools</title>
    <link>https://community.cisco.com/t5/tools/remove-users-script-using-netmiko/m-p/5035503#M3477</link>
    <description>&lt;P&gt;Hi everyone,&lt;/P&gt;
&lt;P&gt;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!&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;
&lt;P&gt;usernames_to_remove = ["username1", "username1"]&lt;/P&gt;
&lt;P&gt;try:&lt;BR /&gt;# Connect to the device&lt;BR /&gt;print(f"Connecting to {device_ip}")&lt;BR /&gt;ssh_client = ConnectHandler(**device)&lt;/P&gt;
&lt;P&gt;# Display current usernames before configuration&lt;BR /&gt;print(f"Current usernames on {device_ip} (before configuration):")&lt;BR /&gt;current_usernames_before = ssh_client.send_command("show running-config | include username")&lt;BR /&gt;print(current_usernames_before)&lt;/P&gt;
&lt;P&gt;# Remove usernames&lt;BR /&gt;for username in usernames_to_remove:&lt;BR /&gt;config_command = f"no username {username}"&lt;BR /&gt;output = ssh_client.send_command_(config_command)&lt;/P&gt;
&lt;P&gt;# Save the configuration&lt;BR /&gt;output += ssh_client.send_command_timing("wr memory")&lt;/P&gt;
&lt;P&gt;print(f"Configuration applied successfully on {device_ip}")&lt;BR /&gt;print(output)&lt;/P&gt;
&lt;P&gt;# Display current usernames after configuration&lt;BR /&gt;print(f"Current usernames on {device_ip} (after configuration):")&lt;BR /&gt;current_usernames_after = ssh_client.send_command("show running-config | include username")&lt;BR /&gt;print(current_usernames_after)&lt;/P&gt;
&lt;P&gt;# Disconnect from the device&lt;BR /&gt;ssh_client.disconnect()&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;
&lt;P&gt;== OUTPUT AFTER RUNNING THE SCRIPT ==&lt;/P&gt;
&lt;P&gt;Connecting to 172.16.222.12&lt;BR /&gt;Current usernames on 172.16.222.12 (before configuration):&lt;BR /&gt;username adminuser privilege 15 secret 9 $9$//hb7V8B9eiLHw$fQxcjZ3WEXVT3ResZBUC/b.Lg3ruiKlz3bMs3BF&lt;BR /&gt;username username1 privilege 15 secret 9 $9$XyNW80Yyvb9EOg$oEdPdvXcibt6aWD3cXPqa1fRd0EbjFrfDc5n9OP&lt;BR /&gt;username username2 privilege 15 secret 9 $9$KNohPABLAwt8ur$EIg02hp5opnHRPDRzImbxoUpVQhP93XzA9XGAX1&lt;BR /&gt;Configuration applied successfully on 172.16.222.12&lt;BR /&gt;n&lt;BR /&gt;OLD-D_GF-ASW1(config)#o username mark&lt;BR /&gt;^&lt;BR /&gt;% Invalid input detected at '^' marker.&lt;BR /&gt;end&lt;BR /&gt;OLD-D_GF-ASW1#Building configuration...&lt;BR /&gt;[OK]&lt;BR /&gt;Current usernames on 172.16.222.12 (after configuration):&lt;BR /&gt;username adminuser privilege 15 secret 9 $9$//hb7V8B9eiLHw$fQxcjZ3WEXVT3ResZBUC/b.Lg3ruiKlz3bMs3BF&lt;BR /&gt;username username1 privilege 15 secret 9 $9$XyNW80Yyvb9EOg$oEdPdvXcibt6aWD3cXPqa1fRd0EbjFrfDc5n9OP&lt;BR /&gt;username username2 privilege 15 secret 9 $9$KNohPABLAwt8ur$EIg02hp5opnHRPDRzImbxoUpVQhP93XzA9XGAX1&lt;BR /&gt;Script execution complete.&lt;/P&gt;</description>
    <pubDate>Thu, 07 Mar 2024 05:17:58 GMT</pubDate>
    <dc:creator>bryg0d</dc:creator>
    <dc:date>2024-03-07T05:17:58Z</dc:date>
    <item>
      <title>Remove Users script using Netmiko</title>
      <link>https://community.cisco.com/t5/tools/remove-users-script-using-netmiko/m-p/5035503#M3477</link>
      <description>&lt;P&gt;Hi everyone,&lt;/P&gt;
&lt;P&gt;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!&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;
&lt;P&gt;usernames_to_remove = ["username1", "username1"]&lt;/P&gt;
&lt;P&gt;try:&lt;BR /&gt;# Connect to the device&lt;BR /&gt;print(f"Connecting to {device_ip}")&lt;BR /&gt;ssh_client = ConnectHandler(**device)&lt;/P&gt;
&lt;P&gt;# Display current usernames before configuration&lt;BR /&gt;print(f"Current usernames on {device_ip} (before configuration):")&lt;BR /&gt;current_usernames_before = ssh_client.send_command("show running-config | include username")&lt;BR /&gt;print(current_usernames_before)&lt;/P&gt;
&lt;P&gt;# Remove usernames&lt;BR /&gt;for username in usernames_to_remove:&lt;BR /&gt;config_command = f"no username {username}"&lt;BR /&gt;output = ssh_client.send_command_(config_command)&lt;/P&gt;
&lt;P&gt;# Save the configuration&lt;BR /&gt;output += ssh_client.send_command_timing("wr memory")&lt;/P&gt;
&lt;P&gt;print(f"Configuration applied successfully on {device_ip}")&lt;BR /&gt;print(output)&lt;/P&gt;
&lt;P&gt;# Display current usernames after configuration&lt;BR /&gt;print(f"Current usernames on {device_ip} (after configuration):")&lt;BR /&gt;current_usernames_after = ssh_client.send_command("show running-config | include username")&lt;BR /&gt;print(current_usernames_after)&lt;/P&gt;
&lt;P&gt;# Disconnect from the device&lt;BR /&gt;ssh_client.disconnect()&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;
&lt;P&gt;== OUTPUT AFTER RUNNING THE SCRIPT ==&lt;/P&gt;
&lt;P&gt;Connecting to 172.16.222.12&lt;BR /&gt;Current usernames on 172.16.222.12 (before configuration):&lt;BR /&gt;username adminuser privilege 15 secret 9 $9$//hb7V8B9eiLHw$fQxcjZ3WEXVT3ResZBUC/b.Lg3ruiKlz3bMs3BF&lt;BR /&gt;username username1 privilege 15 secret 9 $9$XyNW80Yyvb9EOg$oEdPdvXcibt6aWD3cXPqa1fRd0EbjFrfDc5n9OP&lt;BR /&gt;username username2 privilege 15 secret 9 $9$KNohPABLAwt8ur$EIg02hp5opnHRPDRzImbxoUpVQhP93XzA9XGAX1&lt;BR /&gt;Configuration applied successfully on 172.16.222.12&lt;BR /&gt;n&lt;BR /&gt;OLD-D_GF-ASW1(config)#o username mark&lt;BR /&gt;^&lt;BR /&gt;% Invalid input detected at '^' marker.&lt;BR /&gt;end&lt;BR /&gt;OLD-D_GF-ASW1#Building configuration...&lt;BR /&gt;[OK]&lt;BR /&gt;Current usernames on 172.16.222.12 (after configuration):&lt;BR /&gt;username adminuser privilege 15 secret 9 $9$//hb7V8B9eiLHw$fQxcjZ3WEXVT3ResZBUC/b.Lg3ruiKlz3bMs3BF&lt;BR /&gt;username username1 privilege 15 secret 9 $9$XyNW80Yyvb9EOg$oEdPdvXcibt6aWD3cXPqa1fRd0EbjFrfDc5n9OP&lt;BR /&gt;username username2 privilege 15 secret 9 $9$KNohPABLAwt8ur$EIg02hp5opnHRPDRzImbxoUpVQhP93XzA9XGAX1&lt;BR /&gt;Script execution complete.&lt;/P&gt;</description>
      <pubDate>Thu, 07 Mar 2024 05:17:58 GMT</pubDate>
      <guid>https://community.cisco.com/t5/tools/remove-users-script-using-netmiko/m-p/5035503#M3477</guid>
      <dc:creator>bryg0d</dc:creator>
      <dc:date>2024-03-07T05:17:58Z</dc:date>
    </item>
    <item>
      <title>Re: Remove Users script using Netmiko</title>
      <link>https://community.cisco.com/t5/tools/remove-users-script-using-netmiko/m-p/5035534#M3478</link>
      <description>&lt;P&gt;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: &lt;A href="https://pyneng.readthedocs.io/en/latest/book/18_ssh_telnet/netmiko.html" target="_blank" rel="noopener"&gt;https://pyneng.readthedocs.io/en/latest/book/18_ssh_telnet/netmiko.html&lt;/A&gt;&lt;/P&gt;&lt;P&gt;I would rewrite the # Remove usernames block like so:&lt;/P&gt;&lt;PRE&gt;config_list = []&lt;BR /&gt;for username in usernames_to_remove:&lt;BR /&gt; command = f"no username {username}"&lt;BR /&gt; config_list.append(command)&lt;BR /&gt;output = ssh_client.send_config_set(config_list)&lt;BR /&gt;&lt;BR /&gt;# Or the more concise version with list comprehension&lt;BR /&gt;config_list = [ f"no username {username}" for username in usernames_to_remove ]&lt;BR /&gt;output = ssh_client.send_config_set(config_list)&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 07 Mar 2024 06:41:19 GMT</pubDate>
      <guid>https://community.cisco.com/t5/tools/remove-users-script-using-netmiko/m-p/5035534#M3478</guid>
      <dc:creator>Torbjørn</dc:creator>
      <dc:date>2024-03-07T06:41:19Z</dc:date>
    </item>
  </channel>
</rss>

