Skip to content

Instantly share code, notes, and snippets.

@husio
Created April 2, 2012 16:10
Show Gist options
  • Select an option

  • Save husio/2284696 to your computer and use it in GitHub Desktop.

Select an option

Save husio/2284696 to your computer and use it in GitHub Desktop.
Remote irssi notification (SSH + libnotify)
#!/usr/bin/env python2
import optparse
import re
import select
import sys
import socket
import time
import paramiko
from gi.repository import Notify
MSG_RX = re.compile('(\S+)\s+<\s*(\S+)>(.*)')
def watch_irssi(hostname, username, key_filename):
client = paramiko.SSHClient()
client.load_system_host_keys()
client.connect(hostname, username=username, key_filename=key_filename,
timeout=5)
transport = client.get_transport()
chann = transport.open_session()
chann.exec_command('tail -f ~/.irssi/fnotify')
# read old data and ignore it
readlist = (chann, )
while True:
dataready, _, _ = select.select(readlist, (), (), 0.1)
if not dataready:
break
chann.recv(1024)
while True:
select.select(readlist, (), ())
data = chann.recv(1024).strip()
for line in data.split("\n"):
yield line
def notify(title, message):
Notify.init(sys.argv[0])
m = Notify.Notification.new(title, message, "mail-message-new")
m.set_timeout(5000)
m.show()
def logwatch(options):
logwatch = watch_irssi(options.hostname, options.username,
key_filename=options.keyfile)
for line in logwatch:
rx = MSG_RX.findall(line)
if not rx:
notify("cannot parse line", line)
continue
chan, nick, msg = rx[0]
notify("{} @ {}".format(nick, chan), msg)
def main():
parser = optparse.OptionParser()
parser.add_option('-H', '--host', dest='hostname', help='server host name')
parser.add_option('-u', '--username', dest='username')
parser.add_option('-k', '--key-file', dest='keyfile',
help='Authentication key file')
(options, args) = parser.parse_args()
if not options.hostname or not options.keyfile or not options.username:
parser.print_help()
sys.exit(2)
# in case of broken network connection
while True:
try:
logwatch(options)
except socket.gaierror:
time.sleep(10)
except KeyboardInterrupt:
sys.exit(0)
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment