Skip to content

Instantly share code, notes, and snippets.

@Phhere
Created June 10, 2013 19:25
Show Gist options
  • Save Phhere/5751472 to your computer and use it in GitHub Desktop.
Save Phhere/5751472 to your computer and use it in GitHub Desktop.
#! /usr/bin/env python2
import sys
import datetime
import subprocess
import os
networks = ['eduroam','HHUD-W']
interface = 'wlp3s0'
logFile = '/var/log/wlan.txt'
wifiLogFile = '/var/run/wifi'
homeIPs = ['192.168.178.']
uniIPs = ['192.168.10.','10.','134.99.']
synergyHost = '192.168.178.21'
def main():
iface = sys.argv[1]
state = sys.argv[2]
currentWifi = subprocess.check_output('iwgetid | awk \'{split($0,array,"\\"")} {print array[2]}\'',shell=True).strip()
currentIP = subprocess.check_output('ip addr | grep "global" | sed -e "s/.*inet //" -e "s/\/.*//" | head -1',shell=True).strip()
checkMirror(currentIP)
checkWifiLog(iface,state,currentWifi)
checkDock(currentIP)
def checkWifiLog(iface, state, currentWifi):
print("[*] Check Wifi-Log:")
if iface == interface:
if state == "up":
f = open(wifiLogFile,'w')
if currentWifi in networks:
f.write(currentWifi)
print(" - SSID speichern")
else:
f.write('')
print(" - SSID wird ignoriert")
f.close()
elif state == "down":
if os.path.isfile(wifiLogFile):
f = open(wifiLogFile,"r")
oldWifi = f.read()
f.close()
if oldWifi in networks:
print(" - SSID : "+oldWifi)
print(" -- Log schreiben")
f = open(logFile,'a')
datestring = datetime.datetime.now().strftime("%d.%m.%Y %H:%M:%S")
f.write(datestring)
f.write("\n")
dmesgstring = subprocess.check_output('dmesg | grep -E "'+iface+'|cfg80211|NetworkManager|dhcp" | tail -n 25',shell=True).strip()
f.write(dmesgstring)
f.write("\n")
f.close()
else:
print(" - Nix zu tun")
else:
print(" - Nix zu tun")
def checkMirror(currentIP):
print("[*] Check Mirror:")
for i in uniIPs:
if currentIP.startswith(i):
subprocess.call(['/usr/bin/ln','-sf','/etc/pacman.d/mirrorlist.uni','/etc/pacman.d/mirrorlist'])
print(" - Uni-Mirror")
return ""
subprocess.call(['/usr/bin/ln','-sf','/etc/pacman.d/mirrorlist.extern','/etc/pacman.d/mirrorlist'])
print(" - Normaler Mirror")
def checkDock(currentIP):
print("[*] Dock Modus:")
for i in homeIPs:
if currentIP.startswith(i):
dockMode = subprocess.check_output('cat /sys/devices/platform/dock.0/docked',shell=True).strip()
print(" - Zuhause")
if dockMode == "1":
# todo: Dockmode setzen
print(" -- Docked")
setDockMode(True)
else:
print(" -- Undocked")
setDockMode(False)
return ""
# todo: Mode reset
print(" - Unterwegs")
print(" - Undocked")
setDockMode(False)
def setDockMode(docked):
if docked == True:
subprocess.call(['synergyc',synergyHost])
else:
try:
subprocess.check_output('killall synergyc', shell=True,stderr=subprocess.STDOUT)
except:
pass
return ""
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment