29 lines
1.3 KiB
Python
29 lines
1.3 KiB
Python
import urllib.request
|
|
import json
|
|
import urllib.parse
|
|
|
|
base_url = "http://192.168.100.33:9000/api"
|
|
data = json.dumps({"Username":"bestof","Password":"2L2u519wportainer"}).encode('utf-8')
|
|
req = urllib.request.Request(f"{base_url}/auth", data=data, headers={'Content-Type': 'application/json'})
|
|
token = json.loads(urllib.request.urlopen(req).read().decode())['jwt']
|
|
|
|
req = urllib.request.Request(f"{base_url}/endpoints", headers={'Authorization': f'Bearer {token}'})
|
|
endpoint_id = json.loads(urllib.request.urlopen(req).read().decode())[0]['Id']
|
|
|
|
req = urllib.request.Request(f"{base_url}/endpoints/{endpoint_id}/docker/containers/json?all=1", headers={'Authorization': f'Bearer {token}'})
|
|
containers = json.loads(urllib.request.urlopen(req).read().decode())
|
|
|
|
container_id = None
|
|
for c in containers:
|
|
if "/context-hub" in c.get('Names', []):
|
|
container_id = c['Id']
|
|
break
|
|
|
|
if container_id:
|
|
# Get logs
|
|
params = urllib.parse.urlencode({"stdout": 1, "stderr": 1, "tail": 50})
|
|
req = urllib.request.Request(f"{base_url}/endpoints/{endpoint_id}/docker/containers/{container_id}/logs?{params}", headers={'Authorization': f'Bearer {token}'})
|
|
logs = urllib.request.urlopen(req).read()
|
|
# Docker logs from API have headers, we just print as is, it might have garbage bytes but we can read it.
|
|
print(logs)
|