基于pexpect通过堡垒机备份交换机配置

    xiaoxiao2021-11-11  59

    #!/usr/bin/env python #coding=utf-8 ''' import time import pexpect time1 = time.strftime('%y.%m.%d-%H:%M:%S') List = ['10.109.101.99','10.107.101.99'] filename = open('/tmp/test1.txt', 'w') con = pexpect.spawn('/usr/bin/ssh cisco@192.168.1.254') con.expect() ''' import time import getpass import pexpect storage = open('/tmp/test.txt', 'w') con = pexpect.spawn('/usr/bin/ssh yong@114.80.xx.xx -p 5022') password = getpass.getpass('Password:') con.expect('Password:') con.send(password + '\n') con.expect('.*Select group: ') con.send('0\n') con.expect('.*Select page: ') con.send('10.109.101.99\n') con.expect('Input account: ') con.send('nfyg\n') con.expect('.*password: ') con.send('password123\n') con.expect('.>') con.send('enable\n') con.expect('Password:') con.send('password456') con.logfile = storage con.expect('.*#') con.send('terminal length 0\n') con.expect('.*#') con.send('show run\n') con.expect('.*#') con.close()

    storage.close()

    这只是个简单的框架和测试草稿。

    优化计划:(懒癌发作)

    设备清单使用列表,文件名根据时间、设备名自动命名。

    打算通过输出文件变化来丢弃或隐藏帐号交互信息、堡垒机交互信息。

    不使用terminal len 0 ,通过判断 “--more--”来发送空格。

    最重要一点,这仅是个例子,会使用循环或递归(还没想好)来优化脚本。

    转载请注明原文地址: https://ju.6miu.com/read-678195.html

    最新回复(0)