Network Management

 View Only
last person joined: 21 hours ago 

Keep an informative eye on your network with HPE Aruba Networking network management solutions
Expand all | Collapse all

Aruba Python automation

This thread has been viewed 75 times
  • 1.  Aruba Python automation

    Posted Jul 01, 2022 12:03 PM
    I'm new to this forum, also new to Aruba.  

    I'm trying to automate SSH into Aruba switches, but when I run my Python code, I'm getting this.
    "SSH command execution is not supported."

    When I SSH from Putty, there's no problem, but for some reason the switch doesn't like SSH from Python


  • 2.  RE: Aruba Python automation

    Posted Jul 04, 2022 03:43 AM
    Hi,
    there is a difference between an interactive session from Putty and just command execution with SSH. The Switches need an interactive session. With Python just try to use netmiko (https://github.com/ktbyers/netmiko).

    hth
    Alex


  • 3.  RE: Aruba Python automation

    Posted Aug 23, 2023 10:18 AM

    I've tried 3. Thanks in advance for any advice. 

    1- PARAMIKO: 
    This one output this message in to the "output.txt" file: 

    Command: show version
    SSH command execution is not supported.

    ==============================

    import paramiko
    import os

    def main():
        target_ip = '10.10.10.10'
        username = 'root'
        password = input("Enter your SSH password: ")
       
        ssh = paramiko.SSHClient()
        ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
       
        try:
            ssh.connect(target_ip, username=username, password=password)
            print("Connected to the target device.")
           
            output_directory = os.path.join(os.path.dirname(os.path.abspath(__file__)), "output")
            os.makedirs(output_directory, exist_ok=True)
           
            while True:
                user_input = input("Enter a command or press Enter to exit: ")
                if not user_input:
                    break
               
                stdin, stdout, stderr = ssh.exec_command(user_input)
                output = stdout.read().decode("utf-8")
               
                output_file_path = os.path.join(output_directory, "output.txt")
                with open(output_file_path, "a") as file:
                    file.write(f"Command: {user_input}\n")
                    file.write(output)
                    file.write("\n" + "="*30 + "\n")
               
                print(f"Output saved to {output_file_path}")
               
        except paramiko.AuthenticationException:
            print("NOT AUTHORIZED")
        except paramiko.SSHException as e:
            print(f"SSH error: {e}")
        except KeyboardInterrupt:
            print("Session terminated by user.")
        finally:
            ssh.close()

    if __name__ == "__main__":
        main()




    2- NETMIKO+PARAMIKO: 
    The error message: 
    "

     File "C:\Users\...\Python311\Lib\site-packages\netmiko\base_connection.py", line 721, in read_until_pattern
        raise ReadTimeout(msg)
    netmiko.exceptions.ReadTimeout:

    Pattern not detected: 'Show\\ version' in output.

    Things you might try to fix this:
    1. Adjust the regex pattern to better identify the terminating string. Note, in
    many situations the pattern is automatically based on the network device's prompt.
    2. Increase the read_timeout to a larger value.

    You can also look at the Netmiko session_log or debug log for more information."

    import paramiko
    from netmiko import ConnectHandler
    import os
    from paramiko.ssh_exception import SSHException

    def main():
        target_ip = '10.10.10.10'
        username = 'root'
        password = input("Enter your SSH password: ")
       
        device = {
            'device_type': 'autodetect',
            'ip': target_ip,
            'username': username,
            'password': password,
            'secret': '',  # Enable secret, if needed
            'verbose': False,
            'global_delay_factor': 3,  # Adjust this value as needed
        }
       
        try:
            with ConnectHandler(**device) as ssh:
                ssh.remote_conn_pre.timeout = 200  # Set the timeout here
               
                print("Connected to the target device.")
               
                output_directory = os.path.join(os.path.dirname(os.path.abspath(__file__)), "output")
                os.makedirs(output_directory, exist_ok=True)
               
                while True:
                    user_input = input(r"Enter a command or press Enter to exit: ")
                    if not user_input:
                        break
                   
                    try:
                        output = ssh.send_command(user_input)
                       
                        output_file_path = os.path.join(output_directory, "output.txt")
                        with open(output_file_path, "a") as file:
                            file.write(f"Command: {user_input}\n")
                            file.write(output)
                            file.write("\n" + "="*30 + "\n")
                       
                        print(f"Output saved to {output_file_path}")
                   
                    except SSHException as e:
                        print(f"SSH error: {e}")
                   
        except paramiko.AuthenticationException:
            print("NOT AUTHORIZED")
        except SSHException as e:
            print(f"SSH error: {e}")
        except KeyboardInterrupt:
            print("Session terminated by user.")

    if __name__ == "__main__":
        main()