Python Authlog Log File analysis - python

I recently started to play around with Python 3 which I enjoy a lot. I'm a sysadmin in apprenticeship so I have nearly no programming experience. I want to make a small program which is going to tell me everytime someone logs into my system via ssh. I'm going to use the espeak-python bindings. What is the best way to analys the log file in real time?
So everytime someone logs into the system via SSH I can hear it over my speakers :-).
I don't want a complete Solution just a few points so I know where to start...

Should the program run in a terminal or as a service? If run as a service you should consider looking at a library like python-daemon. If you only want to run it in a terminal a usual endless loop (which will then be aborted by a ctrl+c) should do fine.
For playing sound to the speakers you could look at PyAudio. I used it for my sound-related projects. Its website (with good examples) can be found here
To parse the file you can open it (maybe after opening do a read() to discard any log entries that was made before starting the program). Then you can do a readline() in every run of the loop. The retrieved line may be empty. If that's the case no login was attempt. If you got a line you only need to check if there's the word 'sshd' in the line and if yes someone logged in via ssh.

Related

How can I receive the output from a remote (ssh) tail -F command on my local machine with Python?

I am looking for a good way to have a python script that establishes an ssh connection, does a tail -F command on a log file, and receives each line as it is appended to the file in real time so I can further process it and check for certain things. I have tried using tail -1 in an infinite loop, but often times many lines are added almost instantaneously so this is not practical. This is for an automation framework which currently uses Exscript for executing commands over ssh, but I haven't found anything in the docs about this, so I am open to other libraries as well.
I have looked far and wide for many hours for a solution, and nothing is up to date or exactly what I'm looking for.

How to install and run python script on Microsoft server

Guys and ladies. I am new to programming. I have written some script.It just checks whether some data is correct or not. I want that script to run 24*7 on Microsoft server at job (not on my PC). Please let me know how to do that.
thanks in advance
Aside from general server set-up, you will just need to download Python like you would on any server.
As for the running, something like python yourScript.py would work fine. In order to run it 24/7, you need to put your entire script in a while(True): loop so that it never stops running. Note that you should also include some DECENT error handling in the event on an issue so that it doesn't just crash.

Running long Python script on AWS, nohup and screen don't prevent breaks

I'm running a Python script that's parsing through several million xml files and dumping content into a csv. The entire process will take somewhere close to a week to complete, so I'm trying to run it entirely on an AWS server. The problem is that I tried using screen and nohup to get the process to run even when I disconnect from ssh, but often I come back and find that the process has broken either because of a server timeout or a broken pipe error. I thought that nohup and screen were designed to prevent these things from happening and just let the process run until it's complete. Am I missing something or doing something wrong? I can paste my code if it'd be helpful. Thank you for any advice you might have!

Is it possible to use python to establish a putty ssh session and send some input?

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

How can I monitor a python scrypt and restart it in the event of a crash? (Windows)

I have a simple python script to send data from a Windows 7 box to a remote computer via SFTP. The script is set to continuously send a single file every 5 minutes. This all works fine but I'm worried about the off chance that the process stops or fails and the customer doesn't notice the data files have stopped coming in. I've found several ways to monitor python processes in a ubuntu/unix environment but nothing for Windows.
If there are no other mitigating factors in your design or requirements, my suggestion would be to simplify the script so that it doesn't do the polling; it simply sends the file when invoked, and use Windows Scheduler to invoke the script on whatever schedule you need. By relying on a core Windows service, you can factor that complexity out of your script.
You can check out restartme the following link shows how you can use it
http://www.howtogeek.com/130665/quickly-and-automatically-restart-a-windows-program-when-it-crashes/

Categories

Resources