Technical Forum
Ask questions. Discover Answers.
cancel
Showing results for 
Search instead for 
Did you mean: 

Python code using Parallel-SSH library to connect to F5 BigIP

Eljay
Cirrus
Cirrus

Hi,
I'm trying to use Parallel-SSH (Python library) to connect to F5 BigIP.

s = SSHClient(<host>,user=<username>, password=<password>, timeout=5, num_retries=1)
res = s.run_command("uname")
for line in res.stdout:
    print(line)

This simple code works perfectly toward a Red Hat server, but it fails toward F5 BigIP:

Here is the error message:
pssh.exceptions.AuthenticationError: ('Authentication error while connecting to %s:%s - %s - retries %s/%s', '<IP address>, 22, AuthenticationError(), 1, 1)

Has anyone gotten this to work with the Parallel-SSH library?
I have already tested Paramiko and that works, but I want to use Parallel-SSH.
7 REPLIES 7

Leslie_Hubertus
Community Manager
Community Manager

This seems like a question @JRahm may be able to answer!

JRahm
Community Manager
Community Manager

For anyone else trying to get this to work on Mac...first:

  1. brew install cmake
  2. (In a venv) pip install ssh2-python
  3. (In a venv) pip install parallel-ssh

Will post back on details if I can get it talking to BIG-IP. Just took a minute to get through the pip errors for this module.

UPDATE: Got it working. Works with password or key file if you copied your ssh keys to BIG-IP.

from pssh.clients import ParallelSSHClient

hosts = ['ltm1.test.local', 'ltm2.test.local']
#client = ParallelSSHClient(hosts, user='root', password='default')
client = ParallelSSHClient(hosts, user='root', pkey='/Users/justme/.ssh/id_rsa')
cmd = 'tmsh list ltm pool'

output = client.run_command(cmd)
for host_out in output:
    for line in host_out.stdout:
        print(line)

### One output for brevity ###
/Users/justme/PycharmProjects/playground/venv/bin/python /Users/justme/PycharmProjects/playground/sshtest.py 
ltm pool nginx-pool {
    members {
        172.16.102.5:http {
            address 172.16.102.5
            session monitor-enabled
            state up
        }
    }
    monitor http
}

Process finished with exit code 0

 

Thanks, @JRahm. Now I see that I wasn't crystal clear, I was using the single host client in Parallel-SSH. See examples for both single and multi host here: https://pypi.org/project/parallel-ssh/ 

I can adapt my code to this solution as well. But I still wonder if it's a problem with the single host client in the Parallel-SSH library.

Here's an example that works in Paramiko:

from  paramiko.client import SSHClient

p = SSHClient()
p.load_system_host_keys()
p.connect('<IP>', username=<username>, password=<password>)
stdin, stdout, stderr = p.exec_command('uname')
print(stdout.read().decode())

And here's the code using single host client in Parallel-SSH that fails:

import pssh.exceptions
from pssh.clients import SSHClient

s = SSHClient('<IP>',user=<username>, password=<password>, timeout=5, num_retries=1)
res = s.run_command("uname")
print(res)
for line in res.stdout:
    print(line)

 

JRahm
Community Manager
Community Manager

Hi @Eljay ... This also works just fine for me (both with key or password):

from pssh.clients import SSHClient

host = 'ltm15.test.local'
s = SSHClient(host, user='root', password='default')
# s = SSHClient(host, user='root', pkey='/Users/justme/.ssh/id_rsa')
cmd = 'tmsh list ltm pool'

res = s.run_command(cmd)
print(res)
for line in res.stdout:
    print(line)

Output:

/Users/justme/PycharmProjects/playground/venv/bin/python /Users/justme/PycharmProjects/playground/sshtest.py 
	host=ltm15.test.local
	alias=None
	exit_code=None
	channel=<ssh2.channel.Channel object at 0x107c46ef0>
	exception=None
	encoding=utf-8
	read_timeout=None
ltm pool nginx-pool {
    members {
        172.16.102.5:http {
            address 172.16.102.5
            session monitor-enabled
            state up
        }
    }
    monitor http
}

Process finished with exit code 0

My versions:

  • python: 3.11.2
  • ssh-python: 1.0.0
  • ssh2-python: 1.0.0
  • parallel-ssh: 2.12.0
  • BIG-IP: 15.1.8.1

 

Thanks, @JRahm. There's hardly any difference between your and my code.
My Python version is 3.9, the libraries are the same as yours and I have tested toward BigIP v151.5.1, v15.1.6 (Eng hotfix) and v16.1.x. Still the same problem. I'll try to upgrade Python and reinstall the libraries.

JRahm
Community Manager
Community Manager

any luck? Wondering if you're using root or an alternative account?

No, I haven't got this working yet. I'll make a workaround with Paramiko because I know that works. I will finish this project first, and try again Parallell-SSH later.

I use the root user. It works with Paramiko, but fails with Paralllell-SSH.