Create a gist now

Instantly share code, notes, and snippets.

@badp /
Created Nov 11, 2010

What would you like to do?
Send a message to all users on a system. Depends on the `libnotify-bin` package.
# -*- coding: utf-8 -*-
# Licensed under MIT license:
import os, subprocess, sys
# thanks: Martin-Éric Racine, Roger Pate
message = " ".join(sys.argv[1:])
command = "notify-send -u critical -i dialog-warning Warning!".split() + [message]
if not message:
sys.exit("Usage: spam Your message to all users goes here.")
if not os.geteuid() == 0:
sys.exit("Sorry, you need to be root to spam all users on this system.")
command = "notify-send -u critical -i dialog-warning Warning!".split() + [message]
def readfile(fname):
with open(fname, "r") as fobj:
def get_enviro_for(pid, interesting_fields = None):
environment = {}
for line in readfile("/proc/%s/environ" % pid).split("\x00")[:-1]:
param, _, value = line.partition("=")
if (interesting_fields) and (param in interesting_fields):
environment[param] = value
return environment
#warning: reading world-readable files such as /etc/passwd
# will make people go apeshit -- especially if you're skype.
def __get_uname_for(uid):
users = (line.split(":") for line in open("/etc/passwd"))
for line in users:
if line[2] == uid:
return line[0]
_usernames = {}
def get_uname_for(uid):
return _usernames[uid]
except KeyError:
uname = _usernames[uid] = __get_uname_for(uid)
return uname
def get_basic_info_about(pid):
status = readfile("/proc/%s/status" % pid).split("\n")
for line in status:
if line.startswith("Name"):
_, _, proc_name = line.partition(":\t")
if line.startswith("Uid"):
_, _, uids = line.partition(":\t")
uid = uids.split()[0]
return proc_name, get_uname_for(uid)
for pid in os.listdir("/proc"):
if not pid.isdigit():
proc_name, user_name = get_basic_info_about(pid)
if "gnome-session" != proc_name:
# get display and auth
enviro = get_enviro_for(pid, ["DISPLAY","XAUTHORITY"])
print "Notifying %s..." % user_name,
error_code = subprocess.check_call(command, env=enviro)
if error_code == 0:
print "success."
print "failed (error %d)." % error_code
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment