Andrew Overholt: znc, ssh, NetworkManager across suspend/resume cycle |
These days I use an IRC bouncer running on a remote machine with the traffic forwarded over an ssh connection. It’s annoying to have to kill my hung ssh connection and restart it when my network connection change (oh, the humanity!). Originally I thought I could use nm-dispatcher but those scripts run as root and ssh keys + su = PITA if even possible.
Based on the NetworkManager python examples and some googling I cobbled together a script to kill and restart my ssh connection when I get a new IPv4 address:
#!/usr/bin/env python # I have my znc bouncer running on "people" # My wireless adapter is known as wlp3s0 to NM from gi.repository import GLib, NetworkManager, NMClient from subprocess import call import psutil main_loop = None def state_changed(obj, arg1, arg2, arg3): # got a DHCP lease if arg1 == 100: print "Connected" for p in psutil.process_iter(): if p.name == "ssh" and len(p.cmdline) == 3 and p.cmdline[2] == "people": p.kill() call(["ssh", "-fN", "people"]) if __name__ == "__main__": c = NMClient.Client.new() dev = c.get_device_by_iface("wlp3s0") dev.connect('state-changed', state_changed) main_loop = GLib.MainLoop() main_loop.run()
I’m sure it could be improved so I’m open to suggestions if you care to provide any.
I’ve been told that there are better ways to do this: autossh and Mosh.
Комментировать | « Пред. запись — К дневнику — След. запись » | Страницы: [1] [Новые] |