How to use the lsof command using pssh/fabric library? Bash: lsof command not found

586 views Asked by At

Lsof: Bash command not found while connecting to remote server. The same works on the terminal perfectly.

I've tried to work with three of these libraries for ssh in python: pssh, fabric and paramiko.

The main error is that with the pssh and fabric libraries, most of my commands work i.e ls, df, kill etc. But the lsof command doesn't work. It throws this error 'lsof: bash command not found'. Lsof runs perfectly if I use putty to connect to the terminal and run the same commands.

So, I tried paramiko's invoke_shell variant since it uses the shell channel. While paramiko's invoke_shell runs the lsof command properly, it is time consuming.

Fabric code:

c = Connection()
result = c.run('lsof /lt')

print(result.stdout.strip())

Pssh code:

from __future__ import print_function

from pssh.clients import ParallelSSHClient

hosts = ['host']
client = ParallelSSHClient(hosts, user='usename', password='pass')

output = client.run_command('lsof /lt | grep deleted ',  sudo=True)
for host, host_output in output.items():
    for line in host_output.stdout:
        print(line)

Since I'm very new to this, any help would be really appreciated. Please let me know what's the best way to go about this.

1

There are 1 answers

0
Lipsa On

Thanks for the comment @larsks. That fixed it.

What I did was: Run type -a lsof and copy the path from there and then add it to my script.

Thanks a lot!