Created
February 11, 2018 15:46
-
-
Save sejktmcsdlmfhsc/bdebf01d31d9383f17bb30b6174629d5 to your computer and use it in GitHub Desktop.
get suffix filtered user proc names and ids from remote linux machines without psutil via chopsticks
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import sys | |
import os | |
import platform | |
def get_machine_info(): | |
info = {} | |
info["sys.version"] = sys.version | |
info["platform"] = platform.platform() | |
info["release"] = platform.release() | |
return info | |
def get_suffixed_user_procs(suffix): | |
filtered_procs = {} | |
user_id = os.getuid() | |
for entry in os.scandir('/proc'): | |
if not entry.is_dir(): | |
continue | |
if not entry.stat().st_gid == user_id: | |
continue | |
dirname = entry.name | |
if not dirname.isdigit(): | |
continue | |
with open('/proc/{}/cmdline'.format(dirname), mode='rb') as fd: | |
proc_path = fd.read().decode().split('\x00')[0] | |
if proc_path.endswith(suffix): | |
filtered_procs[dirname] = proc_path | |
return filtered_procs |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from chopsticks.tunnel import SSHTunnel | |
import chopsticks_helper | |
tun = SSHTunnel("192.168.2.2", user="pi") | |
print(tun.host, tun.call(chopsticks_helper.get_suffixed_user_procs, "d")) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment