Forum Discussion
Python code using Parallel-SSH library to connect to F5 BigIP
For anyone else trying to get this to work on Mac...first:
- brew install cmake
- (In a venv) pip install ssh2-python
- (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
- EljayMar 09, 2023Cirrus
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)
- JRahmMar 09, 2023Admin
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
- EljayMar 13, 2023Cirrus
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.
Recent Discussions
Related Content
* Getting Started on DevCentral
* Community Guidelines
* Community Terms of Use / EULA
* Community Ranking Explained
* Community Resources
* Contact the DevCentral Team
* Update MFA on account.f5.com