Last active
August 29, 2015 14:13
-
-
Save vadimii/a50ba5cd6b1d3a0c1ed8 to your computer and use it in GitHub Desktop.
Get /proc info from remote server
This file contains hidden or 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
#!/usr/bin/env python3 | |
import paramiko | |
def main(): | |
client = paramiko.client.SSHClient() | |
client.load_system_host_keys() | |
client.connect('share', username='vadim', compress=True) | |
result = exec_remote(client, 'procdata.py') | |
print(result) | |
def exec_remote(client, filename): | |
bufsize = -1 | |
with open(filename, 'r') as stream: | |
python_code = stream.read() | |
cmd ='sudo python -c "{}"'.format(python_code) | |
chan = client.get_transport().open_session() | |
chan.get_pty() | |
chan.exec_command(cmd) | |
output_stream = chan.makefile('r', bufsize) | |
exit_code = chan.recv_exit_status() | |
output_stream.flush() | |
output = output_stream.read().decode('utf-8') | |
if exit_code != 0: | |
raise RemoteExecError(exit_code, output) | |
return output | |
class RemoteExecError(Exception): | |
def __init__(self, exit_code, stderr): | |
self.exit_code = exit_code | |
self.stderr = stderr | |
def __str__(self): | |
return repr(self.stderr) | |
if __name__ == '__main__': | |
import sys | |
sys.exit(main()) |
This file contains hidden or 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 os | |
import re | |
procfs = '/proc' | |
procre = re.compile(r'\d+') | |
# magic0044857 | |
def procdirs(): | |
return [pd for pd in os.listdir(procfs) if procre.match(pd)] | |
for proc in procdirs(): | |
try: | |
cmdline_file = os.path.join(procfs, proc, 'cmdline') | |
with open(cmdline_file, 'r') as stream: | |
proccmd = stream.read().decode('utf-8') | |
proccmd = proccmd.replace('\0', ' ') | |
if 'magic0044857' in proccmd: | |
continue | |
proccmd = proccmd[:80] | |
except OSError: | |
continue # procees has exited for now | |
print proccmd or proc | |
print '-' * 80 | |
procfddir = os.path.join(procfs, proc, 'fd') | |
try: | |
for fd in os.listdir(procfddir): | |
try: | |
print os.readlink(os.path.join(procfddir, fd)) | |
except OSError: | |
continue | |
except OSError: | |
pass | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment