0

Im a new bie to python programming and writing a script to login to a linux machine (from a server) and check for connectivity to multiple IPs in a quick manner. i came across this thread Multiple ping script in Python which uses subprocess.Popen and does it fast but it does check for ping from the server where the script is run rather than the linux machine

I have the code to ssh to the linux machine and get its handle. snippet below is there a way to pass on this handle to the subprocess.Popen so that the ping can be checked from the linux machine instead of the server.

I tried to read the documentation of subprocess.Popen but could not get a clarity.

kindly help.

    ssh = SSHClient()
    ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
    ssh.load_system_host_keys()
    for x in range(retries):
        try:
            ssh.connect(ip, username = username , password = password)
            log.info('Connected to the machine {0} successfully..'.format(ip))
            return ssh
        except (paramiko.BadHostKeyException, paramiko.AuthenticationException, paramiko.SSHException, socket.error) as e:
            log.info('Exception : {0}'.format(e))
            return False 

I used the code using subprocess.call() from the same link pasted above but that is not quick enough..

any help would be greatly appreciated.

S VIJAY
  • 55
  • 6
  • You want to use `ssh` instead of `subprocess` to run a command on the remote host. The interface is slightly different but it's not hard to adapt your code once you understand what's wrong. `subprocess` still runs on your local computer, as you note – tripleee Mar 30 '22 at 11:37
  • I;m able to achieve that by using exec_command() but that is pretty slow as i see . so was wondering if there is a better way to do it – S VIJAY Mar 30 '22 at 11:44
  • The `Popen` solution you looked at created many `ping` processes running in parallel. I believe you can do that with Paramiko too but I'm not particularly familiar with the library so I leave it to somebody else to straighten you out on how exactly to do that. https://stackoverflow.com/questions/66191219/run-multiple-commands-in-different-ssh-servers-in-parallel-using-python-paramiko has a recipe for running on multiple servers in parallel but perhaps it could be simplified to run on just one host. – tripleee Mar 30 '22 at 11:49

0 Answers0