Skip to content

Instantly share code, notes, and snippets.

@wehappyfew
Created January 18, 2018 09:10
Show Gist options
  • Save wehappyfew/8d80c927c161ff6cdbd1e8274f384573 to your computer and use it in GitHub Desktop.
Save wehappyfew/8d80c927c161ff6cdbd1e8274f384573 to your computer and use it in GitHub Desktop.
Script to grab from the Gitlab UI, the registration token needed to register the runners
# check http://kazuar.github.io/scraping-tutorial/
from bs4 import BeautifulSoup
import requests
from lxml import html
def grab_gitlab_runner_token(gl_user, gl_pass):
protocol = "http"
domain = "gitlab.example.com"
login_url = protocol+"://"+domain+"/users/sign_in"
payload = {
"user[login]":gl_user,
"user[password]":gl_pass,
"authenticity_token":"to-pairnei-dynamika"
}
session_requests = requests.session()
result = session_requests.get(login_url)
tree = html.fromstring(result.text)
authenticity_token = list(set(tree.xpath("//input[@name='authenticity_token']/@value")))[0]
payload['authenticity_token'] = authenticity_token
# login with the creds payload
r_loggedin = session_requests.post(
login_url,
data = payload,
headers = dict(referer=login_url)
)
# go to the runners page
runners_url = protocol+"://"+domain+"/admin/runners"
r_runners = session_requests.get(
runners_url,
headers = dict(referer = runners_url)
)
# get the html text
data = r_runners.text
# feed it to BeautifulSoup
soup = BeautifulSoup(data, 'html.parser')
# grab the registration_token
registration_token = soup.find(id="registration_token").get_text()
return registration_token
# debug
# print grab_gitlab_runner_token()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment