netmiko使用案例13 term_server

term_server.py

#!/usr/bin/env python
import time
from netmiko import ConnectHandler, redispatch

net_connect = ConnectHandler(
    device_type='terminal_server',
    ip='10.10.10.10',
    username='admin',
    password='admin123',
    secret='secret123')

# Manually handle interaction in the Terminal Server (fictional example, but 
# hopefully you see the pattern)
net_connect.write_channel("\r\n")
time.sleep(1)
net_connect.write_channel("\r\n")
time.sleep(1)
output = net_connect.read_channel()
# Should hopefully see the terminal server prompt
print(output)

# Login to end device from terminal server
net_connect.write_channel("connect 1\r\n")
time.sleep(1)

# Manually handle the Username and Password
max_loops = 10
i = 1
while i <= max_loops:
    output = net_connect.read_channel()
    
    if 'Username' in output:
        net_connect.write_channel(net_connect.username + '\r\n')
        time.sleep(1)
        output = net_connect.read_channel()

    # Search for password pattern / send password
    if 'Password' in output:
        net_connect.write_channel(net_connect.password + '\r\n')
        time.sleep(.5)
        output = net_connect.read_channel()
        # Did we successfully login
        if '>' in output or '#' in output:
            break

    net_connect.write_channel('\r\n')
    time.sleep(.5)
    i += 1

# We are now logged into the end device 
# Dynamically reset the class back to the proper Netmiko class
redispatch(net_connect, device_type='cisco_ios')

# Now just do your normal Netmiko operations
new_output = net_connect.send_command("show ip int brief")
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 最近看了Ongaro在2014年的博士论文《CONSENSUS: BRIDGING THEORY AND PRAC...
    山本聪阅读 4,629评论 3 11
  • 这篇文章实际上并不是我在阅读etcd中跟Raft相关的部分之后得出的,而是我在读了ZooKeeper中Zab的实现...
    AlstonWilliams阅读 1,549评论 0 1
  • 2018年7月5日 星期四 晴 [亲子日记529天] 这个闷热的季节,西瓜自然很受大家的欢迎。 ...
    窝窝家阅读 153评论 0 1
  • 熟悉的声音响起:“起床啦!安梓昕,别怪我没提醒你。再不起床,我可饶不了你。”一个小姑娘,依然闭着眼睛,仿佛...
    浅陌微澜阅读 196评论 0 0