Forum Discussion

Eljay's avatar
Eljay
Icon for Cirrus rankCirrus
Mar 07, 2023

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

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.

10 Replies

  • MIS's avatar
    MIS
    Icon for Nimbostratus rankNimbostratus

    Hello,

    I've tried mimicking the code shared here, but it returns a similar error 

    pssh.exceptions.AuthenticationError: ('Authentication error while connecting to %s:%s - %s - retries %s/%s', '<IP_address>', 22, AuthenticationError(), 3, 3

    A difference I noticed is that my user, when connected through SSH, gets placed in TMSH instead of BASH.

    I've tried 

    from pssh.clients import ParallelSSHClient
    
    hosts = ['<IP_address1>', '<IP_address2>']
    client = ParallelSSHClient(hosts, user='<MyUser>',password='<MyPassword>')
    cmd = 'show sys clock'
    
    output = client.run_command(cmd)
    for host_out in output:
        for line in host_out.stdout:
            print(line)

      and 

    from pssh.clients import SSHClient
    
    host = '<IP_address>'
    s = SSHClient(host, user='<MyUser>', password='<MyPassword>')
    cmd='show system clock'
    
    res = s.run_command(cmd)
    
    print(res)
    for line in res.stdout:
        print(line) 

     

    Thanks in advance. All help will be appreciated.

    My versions

    • WSL, Ubuntu 22.04.4 LTS
    • Python          3.10.12
    • parallel-ssh   2.12.0
    • ssh-python    1.0.0
    • ssh2-python  1.0.0
    • BIG-IP          15.1.10.3
    • JRahm's avatar
      JRahm
      Icon for Admin rankAdmin

      I'm not able to get ssh2-python installed on either my intel or apple silicon mac currently, erroring out. Might be a little while before I can troubleshoot. I'm guessing since you're in the tmsh shell that this won't work as advertised.

      • MIS's avatar
        MIS
        Icon for Nimbostratus rankNimbostratus

        JRahm,

        Thanks for your prompt response.

        In my uneducated and unexperienced state, I was hoping it would work. In the end, it's just a command in the terminal, I thought.

        If it's within my capabilities, I'll be glad to try anything you can suggest.

        I'll see if I can make my user fall into BASH instead of TMSH when SSHing to the F5s.

        Regards,

  • 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

     

    • Eljay's avatar
      Eljay
      Icon for Cirrus rankCirrus

      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's avatar
        JRahm
        Icon for Admin rankAdmin

        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