Skip to content

Instantly share code, notes, and snippets.

@jgmel
Created May 20, 2019 11:16
Show Gist options
  • Save jgmel/5eeb45d178f3c79753d2d1bd0800ef7a to your computer and use it in GitHub Desktop.
Save jgmel/5eeb45d178f3c79753d2d1bd0800ef7a to your computer and use it in GitHub Desktop.
Winbox URL Handler
#!/usr/bin/env python
import encodings
import sys
from urlparse import urlparse
import os
import subprocess
import configparser
DESKTOP_ENTRY = """[Desktop Entry]
Encoding=UTF-8
Version=1.0
Type=Application
Terminal=false
Exec=$HOME/bin/winbox.url %u
Name=Winbox
Comment=WINBOX-URL
Icon=
Categories=Application;Network;
MimeType=x-scheme-handler/winbox;
"""
DESKTOP_ENTRY2 = """[Default Applications]
x-scheme-handler/winbox=winbox.desktop
"""
# Fix in future
def gui_password(text):
result = subprocess.check_output(["zenity", "--title", text, "--password"])
password,n = result.split("\n")
return password
def main():
with open(os.path.expanduser("~/.local/share/applications/winbox.desktop"), "w+") as desktop:
desktop.write(DESKTOP_ENTRY.replace('$HOME', os.path.expanduser("~")))
mimeapps_config = configparser.ConfigParser()
mimeapps_config.read(os.path.expanduser("~/.local/share/applications/mimeapps.list"))
if not 'Default Applications' in mimeapps_config.sections():
mimeapps_config['Default Applications'] = {}
mimeapps_config['Default Applications']['x-scheme-handler/winbox'] = "winbox.desktop"
with open(os.path.expanduser("~/.local/share/applications/mimeapps.list"), 'w+') as mimeapps_configfile:
mimeapps_config.write(mimeapps_configfile)
user_config = configparser.ConfigParser()
user_config.read(os.path.expanduser("~/.winbox.cfg"))
if len(sys.argv) > 1:
url = urlparse(sys.argv[1])
print url
auth = dict({
'hostname': url.hostname or user_config.get('DEFAULT','hostname',fallback='192.168.88.1'),
'port': url.port or user_config.get('DEFAULT','port' ,fallback=8291),
'username': url.username or user_config.get('DEFAULT','username',fallback='admin'),
'password': url.password or user_config.get('DEFAULT','password',fallback=False) or gui_password('Winbox Password:')
})
os.system("wine ~/bin/winbox.exe {hostname}:{port} {username} {password}".format(**auth))
else:
os.system("wine ~/bin/winbox.exe")
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment