1、登錄Jenkins抓包 ,小編的Jenkins部署在Tomcat上,把Jenkins.war 包放置到webapps目錄。
本次用瀏覽器自帶抓包,按下F12操作,主要看post就可以,登錄是向服務器提交表單操作,則為post請求如下:
2、body里面並不是json格式的,而是application/x-www-form-urlencoded,部分請求截圖如下:
3、實現登錄代母如下:
import requests
# 先打開登錄首頁,獲取部分 cookie
url1 = "http://localhost:8081/jenkins/j_acegi_security_check"
headers = {
"Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3",
"Accept-Encoding": "gzip, deflate, br",
"Content-Type": "application/x-www-form-urlencoded",
"Cookie": "screenResolution=1366x768; JSESSIONID=7B7F3F66948B1965ADDD332E5425A7F4; Pycharm-470d2d8b=7eb39a7e-c2b5-41ac-95f2-ee8fdf8f2188; jenkins-timestamper-offset=-28800000; csrftoken=qsBdlvEy1RoGOFiUqpvowtF9bkeP8XAXbZAhrZnOBqgwqmX3AA6TCxHFkAPSqBsN; hudson_auto_refresh=true; _ga=GA1.1.1092626020.1560260048; ACEGI_SECURITY_HASHED_REMEMBER_ME_COOKIE=YWRtaW46MTU2Mjc2NjkzMTQzODo1ODAxZTNhNDExMDlkODU1ZjFkZWIzZDg1ZGQxNWY4ZjA3NDExMmY2MDJlZDg4YzhlYmQ4YWZhYzMxYTc2OWY3; Pycharm-b245d0ed=4a1ebe68-e0eb-4b41-b28d-d0edee006e77",
"Origin": "http://localhost:8081",
"Referer": "http://localhost:8081/jenkins/login?from=%2Fjenkins%2F",
"User-Agent": "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3683.103 Safari/537.36"
}
form_data = {
"j_username":" 自己的賬號",
"j_password": "自己的密碼",
"from": "/jenkins/",
"Submit": "登錄",
"remember_me": "on"
}
r = requests.post(url=url1,headers=headers,data=form_data,verify=False)
# session 會話
# s = requests.session()
print(r.text)
4、返回的結果並不是json的格式,而是HTML格式,這里只截圖部分的html結果
5、也可以用正則表達式進行提取是否登錄成功,這里截取HTML的兩個關鍵字 ,代碼如下:
import requests
# 先打開登錄首頁,獲取部分 cookie
url1 = "http://localhost:8081/jenkins/j_acegi_security_check"
headers = {
"Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3",
"Accept-Encoding": "gzip, deflate, br",
"Content-Type": "application/x-www-form-urlencoded",
"Cookie": "screenResolution=1366x768; JSESSIONID=7B7F3F66948B1965ADDD332E5425A7F4; Pycharm-470d2d8b=7eb39a7e-c2b5-41ac-95f2-ee8fdf8f2188; jenkins-timestamper-offset=-28800000; csrftoken=qsBdlvEy1RoGOFiUqpvowtF9bkeP8XAXbZAhrZnOBqgwqmX3AA6TCxHFkAPSqBsN; hudson_auto_refresh=true; _ga=GA1.1.1092626020.1560260048; ACEGI_SECURITY_HASHED_REMEMBER_ME_COOKIE=YWRtaW46MTU2Mjc2NjkzMTQzODo1ODAxZTNhNDExMDlkODU1ZjFkZWIzZDg1ZGQxNWY4ZjA3NDExMmY2MDJlZDg4YzhlYmQ4YWZhYzMxYTc2OWY3; Pycharm-b245d0ed=4a1ebe68-e0eb-4b41-b28d-d0edee006e77",
"Origin": "http://localhost:8081",
"Referer": "http://localhost:8081/jenkins/login?from=%2Fjenkins%2F",
"User-Agent": "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3683.103 Safari/537.36"
}
form_data = {
"j_username":" 自己的賬號",
"j_password": "自己的密碼",
"from": "/jenkins/",
"Submit": "登錄",
"remember_me": "on"
}
r = requests.post(url=url1,headers=headers,data=form_data,verify=False)
# session 會話
# s = requests.session()
print(r.text)
# 正則表達式提取賬號和登錄按鈕
import re
t = re.findall("<b>(.+?)</b>",r.content.decode("utf-8")) #由於是編碼格式,所以要進行decode解碼
print(t[0])
print(t[1])
運行的結果為 1.自己的賬號名 2.注銷 。
6、復制出html頁面到 http://www.kjson.com/xml/ 中進行解析下,有層次感。