Need Help with Python code

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-18-2025 09:55 PM
Good day everyone.
My name is Johannes Robbetze and I am new to the automation side of things. I try to write a Python script that will log in to multiple Cisco switches and change the local username and password as well as the enable secret password. When I run the script I get the below output and it does not change the config. Could I please ask for some help in pointing me in the correct direction to fix the code.
SSHSCRIP.py
- Labels:
-
Code and Deployment
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-19-2025 03:43 AM
From your script I'm not sure why is not working. I would assume the commands you have on config_file.txt works, however assuming is not good.
So my advice is:
- First make sure this works manually.
- Then try to run your script with only one device and one command.
- Remove the loops so you remove complexity that is not required at this point.
- Print the command you are actually sending before remote.send( commands) so you are sure what you are sending.
Another piece of advice is to use context managers (with) rather than opening and closing files, this makes the code easier to read and you will be sure your close your reading.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-19-2025 03:59 AM
Hi,
It looks like you're just sending the text without a newline/enter.
I haven't worked with paramiko, but other examples I've found included a newline character in the .send command.
So instead of "remote.send('term len 0')" you should probably be doing "remote.send('term len 0\n')"
(Adding the " \n " at the end)
That being said, there are two other observations I would like to point out:
#1
While I'm not that familiar with paramiko, in the past I've been using netmiko, which is very useful for this sort of thing, as it handles some of the basics so you don't need to send individual commands such as the term length and enable.
I've found it easy to use if I'm using python to do any mass-changes.
#2
I'm not sure if the output you posted is from a LAB environment or not, but your password you use can be seen in the screenshot.
If by any chance this is something you use you should now consider the password to be compromised.
Please mark helpful answers & solutions
---
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-19-2025 05:30 AM
Hi Jonathan
Thank you. The password that you see is just a test and not the real password that will be used. Let me check with the suggested changes if I can get it to work. Thank you
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-19-2025 10:51 PM
Thank you to everyone who gave some input. I have changed the script and now it is working as expected.
