I'm wondering about the best way to use ssh to work with routers interactively in python version 2.7.
Scenario:
Access 20 devices at the same time.
run 10 commands on each device.
parse specific words from the commands output.
run another command based on step 3.
exit the devices.
Take a look at the Netmiko package ktbyers made, here.
The package allows you create ssh connections and send commands/configurations/etc.
Related
I'm trying to find a way to execute commands with python over ssh. I did find this answer, however I'm working on an embedded platform and can't install new packages. In other words I can't install paramiko. Is there a way to do what paramiko does but with standard packages?
Is it a Unix based system with an ssh client? If so, could you use subprocess, https://docs.python.org/3/library/subprocess.html, to spawn another process and run ssh commands?
If not, have you tried to see if you can package paramiko itself with your code and deploy everything together to the target system? I'm not sure if this works, but it may be worth a shot.
Fist of all, due to Company Policy, Paramiko, or installing anything that requires administrative access to local machine it right out; otherwise I would have just done that.
All I have to work with is python with standard libraries & putty.
I am attempting to automate some tedious work that involves logging into a network device (usually Cisco, occasionally Alcatel-Lucent, or Juniper), running some show commands, and saving the data. (I am planning on using some other scripts to pull data from this file, parse it, and do other things, but that should be irrelevant to the task of retrieving the data.) I know this can be done with telnet, however I need to do this via ssh.
My thought is to use putty's logging ability to record output from a session to a file. I would like to use Python to establish a putty session, send scripted log-in and show commands, and then close the session. Before I set out on this crusade, does anyone know of any way to do this? The closest answers I have found to this all suggest to use Paramiko, or other python ssh library; I am looking for a way to do this given the constraints I am under.
The end-result would ideal be able to be used as a function, so that I can iterate through hundreds of devices from a list of ip addresses.
Thank you for your time and consideration.
If you can't use paramiko, and Putty is all you get so the correct tool is actually not Putty - it's his little brother Plink - you can download it here
Plink is the command line tool for Putty and you can your python script to call it using os.system("plink.exe [options] username#server.com [command])
See MAN Page here
Hope it will help,
Liron
I have numerous test servers. These test servers get re-imaged frequently, and they have the same user account and password after being re-imaged. I want to write a python script that runs a command remotely over ssh on one of these servers, without prompting user for a password, and gathers the output of the command. In some circumstance I want to run one command, get output, analyze the output. In other situation, I want to run several commands at a time (possibly run a script file). I read many postings about running commands remotely, using third party packages (e.g. paramiko). Is there a recommended way to achieve this task without using additional packages ? The server from which my script will be run might not have the package installed.
Or should I used pexpect ?
Ideally I would like to use subprocess and capture the output (providing password as an argument). Of course, my script has to handle the case when the client is logging for first time, and prompted to add ssh key to .ssh/knownhosts file.
Thank you,
Ahmed.
If host key security is not an issue (you are on a trusted network etc), you can bypass the host checking. And if you use key-based authentication there is no need for a password prompt:
ssh -oUserKnownHostsFile=/dev/null -oStrictHostKeyChecking=no -oPasswordAuthentication=no \
-n doctor#tardis some_cmd
This way you can just use subprocess as if you executed some_cmd locally.
I am writing a test application in python and to test some particular scenario, I need to launch my python child process in windows SYSTEM account.
I can do this by creating exe from my python script and then use that while creating windows service. But this option is not good for me because in future if I change anything in my python script then I have to regenerate exe every-time.
If anybody have any better idea about how to do this then please let me know.
Bishnu
Create a service that runs permanently.
Arrange for the service to have an IPC communications channel.
From your desktop python code, send messages to the service down that IPC channel. These messages specify the action to be taken by the service.
The service receives the message and performs the action. That is, executes the python code that the sender requests.
This allows you to decouple the service from the python code that it executes and so allows you to avoid repeatedly re-installing a service.
If you don't want to run in a service then you can use CreateProcessAsUser or similar APIs.
You could also use Windows Task Scheduler, it can run a script under SYSTEM account and its interface is easy (if you do not test too often :-) )
To run a file with system account privileges, you can use psexec. Download this :
Sysinternals
Then you may use :
os.system
or
subprocess.call
And execute:
PSEXEC -i -s -d CMD "path\to\yourfile"
Just came across this one - I know, a bit late, but anyway. I encountered a similar situation and I solved it with NSSM (Non_Sucking Service Manager). Basically, this program enables you to start any executable as a service, which I did with my Python executable and gave it the Python script I was testing on as a parameter.
So I could run the service and edit the script however I wanted. I just had to restart the service when I made any changes to the script.
One point for productive environments: Try not to rely on third party software like NSSM. You could also achieve this with the standard SC command (see this answer) or PowerShell (see this MS doc).
I'd like to write a python script to perform some very simple "agentless" monitoring of remote processes running on linux servers.
It would perform the following tasks, in psuedocode:
for each remoteIPAddress in listOfIPAddresses:
log into server#remoteIPAddress via ssh
execute the equivalent of a 'ps -ef' command
grep the result to make sure a particular process (by name) is still running
One way to do this is to have python call shell scripts in a subprocess and parse their output.
That seems pretty inefficient. Is there a better way to do this via python libraries?
All I could find via research here and elsewhere was:
psutil - looks like it doesn't do remote monitoring, so I'd have to run agents on the remote machines to report stats back via RPC.
pymeter - I would have to write my own plugin for monitoring a specific remote service.
stackoverflow #4546492 - Some helpful links but the poster was looking for a different solution.
Thanks, and please go easy on me, it's my first question :-)
The Fabric library may be of interest to you.
Check out paramiko. You can use it to ssh into the server and run commands. You can then parse the results and do what you'd like with them.
Taking cues from the answers above, I investigated Fabric and found the following presentation particularly interesting/helpful. It is an overview of three libraries -- Fabric, Cuisine, and Watchdog -- for server monitoring and administration.
For posterity:
Using Fabric, Cuisine, and Watchdog for server administration in Python
It might be heavier than what you're looking for, but Zenoss supports agentless monitoring.
paramiko and Fabric, suggested in the other answers, are great options too.
Why don't you use a dedicated monitoring tool like Nagios ?
Nagios has agent and agent less monitoring through NRPE plugins and SSH plugins etc.
Try it out.