0

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.

Lipsa
  • 9
  • 2
  • What is the path to the `lsof` command on the remote server? What happens if you use the fully qualified path to this binary (e.g., `/usr/local/bin/lsof`) instead of the unqualified name (`lsof`)? – larsks Aug 13 '19 at 13:18

1 Answers1

0

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!

Lipsa
  • 9
  • 2