Paramiko capturing command output

前端 未结 3 1061
抹茶落季
抹茶落季 2020-12-22 06:37

I have an issue that has been giving me a headache for a few days. I am using the Paramiko module with Python 2.7.10 and I\'d like to issue multiple commands to a Brocade ro

3条回答
  •  轮回少年
    2020-12-22 07:28

    After reading all of the comment I have made the following changes:

    #!/usr/bin/env python
    import paramiko, time
    
    router = 'r2.test.example.com'
    password = 'password'
    username = 'testuser'
    
    ssh = paramiko.SSHClient()
    ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
    ssh.connect(router, username=username, password=password)
    print('Successfully connected to %s' % router)
    
    remote_conn = ssh.invoke_shell()
    output = remote_conn.recv(1000)
    
    # Disable paging on Brocade.
    remote_conn.send('terminal length 0\n')
    time.sleep(2)
    # Clearing output.
    if remote_conn.recv_ready():
        output = remote_conn.recv(1000)
    
    # Check interface status.
    remote_conn.send('show interfaces ethernet 4/1\n') # I only want output from this command.
    time.sleep(2)
    # Getting output I want.
    if remote_conn.recv_ready():
        output = remote_conn.recv(5000)
    print(output)
    
    # Test: Check if interface is up.
    for line in output.split('\n'):
        if 'line protocol is up' in line:
            print(line)
    

    Everything works great now.

    Thank you for all the help.

    Best regards,

    Aaron C.

提交回复
热议问题