Skip to content

Instantly share code, notes, and snippets.

@hawkw
Last active August 29, 2015 13:58
Show Gist options
  • Save hawkw/10170204 to your computer and use it in GitHub Desktop.
Save hawkw/10170204 to your computer and use it in GitHub Desktop.
Roboworker IP address - it's like a crappy version of a VPN
141.195.91.37
#!/usr/bin/env python3.3
###################################################
# THIS IS NOT FOR YOU (unless you're a cron job)
# do not download this script, do not execute this
# script, this script never happened.
# thank you for your compliance.
##################################################
from os.path import expanduser
from requests.auth import HTTPBasicAuth
import subprocess, requests, re
GIST_ID = ''
TOKEN = ''
LOGIN = ''
log = lambda event: subprocess.call(['logger', '[ip_update] {e}'.format(e=event)])
def get_current_ip():
ifconfig = str(subprocess.check_output(['ifconfig','-a']))
match = re.search(r"addr:([0123456789]{1,3}\.[0123456789]{1,3}\.[0123456789]{1,3}\.[0123456789]{1,3})",
ifconfig.split('wlan0')[-1])
current_ip = match.group(1)
old_ip = "000.000.00.0"
try: # if the file doesn't exist, that's fine
with open(expanduser('~/.current_ip'), 'r') as ip_file:
old_ip = ip_file.readline()
if current_ip == old_ip:
log("IP address is {current}, no change".format(current=current_ip))
return False
except FileNotFoundError:
log('~/.current_ip not found, creating.')
pass
log("IP address changed from {old} to {current}".format(
current=current_ip,
old=old_ip
))
with open(expanduser('~/.current_ip'), 'w') as ip_file:
ip_file.write(current_ip)
log("Wrote IP change to file")
return current_ip
def update_gist(ip):
log("[ip-update] Called update")
succeeded = False
json = '{ \"files\": { \"IP.txt\": { \"content\": \"%s\" } }' % ip
while not succeeded:
response = requests.patch("https://api.github.com/gists/{id}".format(id = GIST_ID),
data = json,
auth = (TOKEN, "x-oauth-basic")
)
log("Attempted to update gist, response status:"\
" {status}: {reason}".format(
status = response.status_code,
reason = response.reason
)
)
if response.status_code == 200:
log("Successfully updated gist, closing")
succeeded = True
elif response.status_code == 301:
log("New location: {l}".format(l=response.getheader("Location")))
elif response.status_code == 404:
log("A 404 error occured. Please contact Hawk -- or GitHub, they might be on fire.")
log(response.text)
break
def main():
ip = get_current_ip()
if ip: update_gist(ip)
if __name__ == "__main__":
main()
#!/usr/bin/env python2.7
# roboworker-connect.py
# by Hawk
#
# Simple script to connect to roboworker build box by fetching
# its' IP address from GitHub gist.
#
# ---- SAFETY INSTRUCTIONS ------------------------------------
# If you want to make the process a little simpler, use the
# `ssh-keygen` to generate a DSA key and put it in
# `.ssh/authorized_hosts` on roboworker. You can then use
# `ssh-agent` to keep yourself authorized so that you can log
# on without having to type your password. It's also more
# secure.
#
# You also might want to `chmod +x roboworker-connect.py` so
# that you can run this script as though it were an executable
# and you might want to put it in a location that's on your
# path so that you can call it from anywhere.
#
# I don't take any responsiblity for any property damage, loss
# of life, or general pain and suffering caused by this script.
# You're running it at it's own risk and accept all responsibi-
# -lity, blah blah blah whatever.
#
# With that said, do let me know if anything goes wrong.
# ---- USAGE --------------------------------------------------
# -> `python roboworker-connect.py [roboworker-username]`
# -> `python roboworker-connect.py` if you've added your
# roboworker username as default
# -> `roboworker-connect.py` if you're a power-user who ran
# `chmod +x roboworker-connect.py` and hardcoded your
# roboworker username.
# -------------------------------------------------------------
import subprocess, json
from urllib import urlopen
from sys import argv
GIST_ID = '10170204' # ID number of gist containing IP
USER_ID = 'hawk' # put your Roboworker username here :)
PORT_GIT = '4092' # port to bind to kittymonster Git port
PORT_JENKINS = '8080' # port to bind to kittymonster Jenkis web services port
PORT_GITLAB = '8081' # port to bind to kittymonster GitLab port
KITTY_GIT = '22' # Kittymonster Git port
KITTY_JENKINS = '8080' # Kittymonster Jenkins web services port
KITTY_GITLAB = '80' # Kittymonster GitLab port
PORT_BIND = "{port}:kittymonster:{kitty}"
def main():
try:
response = urlopen("https://api.github.com/gists/{id}".format(id=GIST_ID))
except IOException as e:
print("[roboworker-connect] Could not get IP address from gist. Sorry.")
print(e)
exit()
finally:
roboworker_ip = json.load(response)['files']['IP.txt']['content']
# if a username is given, connect as that user
if len(argv) > 1:
username = argv[1]
else:
username = USER_ID
ssh_command = [
'ssh',
'-L', PORT_BIND.format(port = PORT_GIT, kitty = KITTY_GIT), # bind Git port
'-L', PORT_BIND.format(port = PORT_GITLAB, kitty = KITTY_GITLAB), # bind GitLab port
'-L', PORT_BIND.format(port = PORT_JENKINS, kitty = KITTY_JENKINS),# bind Jenkins port
'{user}@{robo_ip}'.format(robo_ip = roboworker_ip, user = username) # username & password
]
subprocess.call(ssh_command)
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment