Getting Notified on "Ethernet cable plugged in" events in linux - python

I am writing a Python Application running on linux. I want to be able to register and be notified by the system if a network cable is plugged in/ out.
I am already using pyUdev (python bindings for libUdev) in order to get notified for USB plug in events. However, because the network eth0 module (for example) always remains loaded (regardless of the cable being plugged in) , I dont get the information I require. It only works for if up/down events.
I have read a lot of posts on Windows WMI providing this functionality but none on linux. Am I looking in the right direction?
A python way of doing this (a python library) would be ideal as I want to integrate this with my program.

There are many ways to get the "UP" status of a network interface via polling, ranging from simply parsing ifconfig output, to reading the status of /proc/net/... pseudo-files.
However, for a way to be notified (i.e. not poll yourself) for such events, your best option on Ubuntu is to use the netplug package. It's not installed by default, you need to:
sudo apt-get install netplug
Some documentation is here: http://manpages.ubuntu.com/manpages/lucid/man8/netplugd.8.html
To set it up you need to tweak /etc/netplug/netplugd.conf and /etc/netplug/netplug scripts. For an example of those config files check out the section "Configuring netplug" at http://natisbad.org/dyn-net/index.html
With /etc/netplug/netplug you can have your own python script called when the network goes down/up, so you can generate your kind of event for your application.

Have you looked into netplugd command, I am not sure if a python wrapper exists but it should not be hard to parse the output

Related

RDP script in python?

I am writing a script in python, and part of it needs to connect to a remote computer using rdp. Is there a script or an api that I could use to create this function? Also, if there is not, is there a way to package a rdp application along side python and then use a python script to run it? Any help would be much appreciated. Thanks in advance, Nate
If you need an interactive window, use the subprocess module to start your rdesktop.exe (or whatever).
If you need to run some command automatically, you're probably better off forgetting about RDP and using ssh (with passwordless, passphraseless authentication via RSA or similar), psexec (note that some antivirus programs may dislike psexec, not because it's bad, but because it's infrequently been used by malware for bad purposes) or WinRM (this is what you use in PowerShell; it's like ssh or psexec, except it serializes objects on the sender, transmits, and deserializes back to an object on the recipient).
Given a choice among the 3, I'd choose ssh. Cygwin ssh works fine, but there are several other implementations available for Windows.
HTH
As per this GitHub comment, you can try to use libfreerdp via ctypes in Python.
See: FreeRDP library at GitHub which is a free remote desktop protocol library and clients.
Home page: www.freerdp.com
Related: Programmatically manipulating active RDP session.

How can I detect using Python the insertion of only USBs and hard drives on Ubuntu/Linux?

I'm building a backup program which involves detecting when media available for backup is inserted. I've looked into detecting the insertion of backup media, and I'm going to use the file system watch service inotify on the /media/username directory.
The problem is that I've looked into this directory and there are folders that don't represent any currently available medium. How can I detect the list of currently available mediums (USBs, HDDs) and watch for any future ones? More technically, what are the characteristics of an actively available USB/HDD folder in the /media/username directory?
In Linux, you could use uevents from kernel and start sniffing for "ACTION"="add"
Please check the following links:
http://lwn.net/Articles/242046/
and Netlink socket:
http://www.kernel.org/doc/man-pages/online/pages/man7/netlink.7.html
Or use DBUS/HAL API bindings for Python http://ubuntuforums.org/archive/index.php/t-904706.html
Check dmesg messages and see exactly what was attached and to what partition was mounted
P.S.: Here is an example (on SO) of how to do it in Python using DBUS binding:
How can I listen for 'usb device inserted' events in Linux, in Python?
UPDATE
How to check if a path is mounted:
https://serverfault.com/questions/143084/how-can-i-check-whether-a-volume-is-mounted-where-it-is-supposed-to-be-using-pyt
I can detect it rather easily through monitoring the /dev/disks/by-label/ directory.

Network status change on Windows, Linux, and how to do it in Python

Hello friends and colleagues,
I am trying to make a function for my program on pyt4 - softphone which should determine the status of the network computer.
If the network has lost connection to the Internet the program should to wait until the network is restored and immediately restart the core of my app which self connects to server
Restarting the core is done by the "reload" method.
But I also would like to do cross-platform
Windows, Linux, Darwin,
I found something similar to this link: http://www.themacaque.com/?p=751
Reload method:
def reload(self):
self.config = ConfigParser.RawConfigParser()
self.config.readfp(getConfig())
self.tray.setIcon(self.inactiveIcon)
self.controller.core.restart_core()
The example you show is trying to use the native implementation because it was written not to depend on Qt4 (I'm the author of the post). If you don't have a problem in using PyQt4 in all platforms you should consider using the networkAccessibleChanged signals that you can find in the QNetworkAccessManager.
If that is not possible you will have to write different implementations of the same interface to be used according to the platform in which you are working, for example:
Windows: Use the code I posted on the blog.
Linux: You can use the DBus signals that NEtworkManager has to notify you of the state of the connection.
Mac OS X: SystemConfiguration framework as a similar set of signals to tell you the state of the connection.
If your software is open source and you don't care/mind that canonical has the copyright of the code you can take a look at the implementations we wrote for Ubuntu One here.

How do I access a remote filesystem using Python on Windows?

I'm writing a Python script to access all computers on the network, log in to them and read some log files. I don't want to use something as low-level as socket, but I can if I must. I realize that my problem is similar to this question, but not the same.
Are there any modules for accessing external Windows machines?
Has anyone done anything like this before?
I'm specifically looking to log into Windows 7 machines, not unix.
Let's also assume that each computer I want to log into has Remote Desktop installed and enabled. I'm also not worried about network security or encryption because these files are not confidential. Windows machines don't have SSH installed on the by default do they?
There has to be something on the other side for you to talk to. This limits you to either setting up a "server" on each machine, installing a real server (i.e. sshd), building a "server" yourself and installing it, or using a built in and active feature of the OS.
Based upon this, what kind of system do you want to set up on these machines? What does it need to do? Just read the contents of a prespecified file list? Will that list change?
One solution is to turn on telnet, and use paramiko or twisted to
talk across it. This isn't very secure of course
Next up, set up a samba share, and access the folder remotely. This
is also insecure, though less so than telnet
You could find a ssh daemon port and run that, if you are so inclined
Psexec from sysinternals might work
Use twisted to build a server app with the features you need
Use ncat to listen on a port and spawn a cmd prompt
Be aware that most of the solutions for accessing windows remotely are... poor. The best solution is probably to roll your own, but that is hard work and you will probably make mistakes.
Also, Windows 7 is not exactly multi-user friendly. Individual processes can run as separate users, but the OS does not support having multiple users logged in at the same time. Someone is going to be the "user" and everyone else is just a process with a different credential set.
This is more an artificial limitation on M$'s part than anything technical. To see this in action, try to log in with RDP while a user is logged in locally. Fun times.
Per your edit, the easiest thing to do is just set up a samba share on the box.
After this share is set up:
with open(r'\\myCompNameOrIP\C\windows\logs\logfile.txt','rb') as logfile:
loglines = logfile.readlines()
Or you can use the gencat sample found here. Just give it r'\\myCompNameOrIP\C\windows\logs\*.txt' as the search path and watch the magic.
From Ubuntu I use samba:
In Bash:
gvfs-mount smb://them/folder
Here I give name, domain and password
Then in python:
folder = '/home/me/.gvfs/folder on them'
using the os module I read folders and files inside.
I am working in a small business environment.
Why not have each of the computers send the log file to the central computer?

Control rs232 windows terminal program from python

I am testing a piece of hardware which hosts an ftp server. I connect to the server in order to configure the hardware in question.
My test environment is written in Python 3.
To start the ftp server, I need to launch a special proprietary terminal application on my pc. I must use this software as far as I know and I have no help files for it. I do however know how to use it to launch the ftp server and that's all I need it for.
When I start this app, I go to the menu and open a dialog where I select the com port/speed the hardware is connected to. I then enter the command to launch the ftp server in a console like window within the application. I am then prompted for the admin code for the hardware, which I enter. When I'm finished configuring the device, I issue a command to restart the hardware's software.
In order for me to fully automate my tests, I need to remove the manual starting of this ftp server for each test.
As far as I know, I have two options:
Windows GUI automation
Save the stream of data sent on the com port when using this application.
I've tried to find an GUI automater but pywinauto isn't supporting Python 3. Any other options here which I should look at?
Any suggestions on how I can monitor the com port in question and save the traffic on it?
Thanks,
Barry
Have you looked at pySerial? It's been a few years since I've used it but it was quite good at handling RS-232 communications and it looks like it's compatible with Python 3.x.
Sikuli might provide the kind of GUI automation you need.
I was also able to solve this using WScript, but pySerial was the preferred solution.

Categories