0

I want to be able to search for process name remotely on a machine. I'm using paramiko to run the command "tasklist". The output of the command is in bytes and I cant search for a specific name, so as far as I know I need to use decode() to convert it into string but then I can't loop on all items as well. Any idea how can I do it ?

ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect(ip, username="Administrator", password="Password1")
print("runnning command")
stdin, stdout, stderr = ssh.exec_command("tasklist")
output = stdout.read()
output = output.decode()

Example for output I get when printing 'output':

Image Name                     PID Session Name        Session#    Mem Usage
========================= ======== ================ =========== ============
System Idle Process              0 Services                   0          8 K
System                           4 Services                   0         32 K
Registry                       120 Services                   0     25,928 K
smss.exe                       404 Services                   0        500 K
csrss.exe                      512 Services                   0      2,628 K
Aaron
  • 29
  • 3
  • Why try to write code others have already written and tested? A simple web search finds me https://thispointer.com/python-get-list-of-all-running-processes-and-sort-by-highest-memory-usage/ – Mike 'Pomax' Kamermans Mar 08 '20 at 16:39
  • But I want to do it remotely and not run locally on the machine. Issue here is how do I convert the output to object I can search on. – Aaron Mar 08 '20 at 16:43
  • 1
    Not quite sure what the problem is... did you search for "python convert bytes to string" on google/duckduckgo/etc first? Because if that's the only thing you're having problems with, that's already well documented and easy to find? It even has a very well regarded post right here on Stackoverflow already: https://stackoverflow.com/questions/606191/convert-bytes-to-a-string – Mike 'Pomax' Kamermans Mar 08 '20 at 17:04
  • @Mike 'Pomax' Kamermans, maybe It wasn't clear enough but I already converted it into string using decode, but I want to be able to loop on that string and count how many process with specific name are running. It would be easy if I could somehow convert it into arranged list somehow. – Aaron Mar 09 '20 at 09:03
  • What do you mean by you can't loop on items? Why can't you just loop over the output lines? Use `output.split('\n')` – najeem Mar 09 '20 at 13:46
  • Yeah, it feels like your question's drifting away from what you originally posted:what do you mean with "loop on all items"? There are no items, the output you got was a byte stream, which you can convert to a single (probably very long) string, so now _you_ need to decide how to parse that string (either with an output parser, or naively by splitting on end-of-line and then guessing how to do some regex matching or the like based on the output you now show in your post). – Mike 'Pomax' Kamermans Mar 09 '20 at 16:36

0 Answers0