简体   繁体   English

Python-检索Cisco CLI命令的输出并将其用作输入

[英]Python - Retrieve the output of Cisco CLI command and use it as input

Below is my working code which is used to execute Cli commands on Cisco Routers and Switches. 下面是我的工作代码,用于在Cisco路由器和交换机上执行Cli命令。 I have a task at my hand which requires me to read specific values from the output of the Cli commands executed earlier and use is as input for another command to be executed on the Cisco device. 我手头有一项任务,要求我从先前执行的Cli命令的输出中读取特定值,并用作要在Cisco设备上执行的另一命令的输入。

Output of the below code: 以下代码的输出:

TEST#sh run | TEST#sh运行| i hostname 我的主机名

hostname TEST 主机名TEST

TEST# 测试#

My task is to fetch only "TEST" from the output and use it as input for another command in the same code as follows, 我的任务是从输出中仅获取“ TEST”,并将其用作另一条命令的输入,其代码如下所示,

chan.send("conf t\\n") chan.send(“ conf t \\ n”)

chan.send("tacacs server key TEST\\n") chan.send(“ tacacs服务器密钥TEST \\ n”)

chan.send("exit\\n") chan.send(“ exit \\ n”)

Please guide me on this. 请指导我。 Thanks in advance. 提前致谢。

import paramiko
import sys
import os
import subprocess
import cmd
import time
import datetime
import getpass
from netaddr import *

buff = ''
resp = ''
now = datetime.datetime.now()
usr = raw_input('Enter Username:')
pwd = getpass.getpass('Enter Password:')

with open('Fetch.txt') as f:
    for line in f:
        line = line.strip()
        with open(os.devnull, "wb") as limbo:
                ip = line
                result = subprocess.Popen(["ping", "-n", "2", "-w", "200", ip],
                    stdout=limbo, stderr=limbo).wait()
                if result:
                        print ip, "Link Down - Site unreachable"
                        f = open('Down Sites.txt', 'a+')
                        f.write( line + '\n' )
                        f.close()
                else:
                        try:
                            dssh = paramiko.SSHClient()
                        dssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
                            dssh.connect(line, username=usr, password=pwd)
                            chan = dssh.invoke_shell()
                            chan.send("sh run | i hostname\n")
                            time.sleep(6)
                            data = chan.recv(99999)
                            filename_prefix = line
                            filename = "%s-%.2i-%.2i-%i_%.2i-%.2i-%.2i.txt" % (filename_prefix,now.day,now.month,now.year,now.hour,now.minute,now.second)
                            f=open(filename,"w")
                            f.write(data)
                            f.close()
                            print "Command Executed"
                        except Exception as e:
                            err = str(e)
                            f = open('Exceptions.txt', 'a+')
                            f.write(ip + ": " + err + '\n')
                            f.close()
                            print ip, "ERROR:", e
dssh.close()

I was able to get it working, just researched and got this solution, 我能够使其正常运行,只是进行了研究并获得了此解决方案,

import paramiko
import sys
import os
import subprocess
import cmd
import time
import datetime
import getpass
import re
from netaddr import *

buff = ''
resp = ''
now = datetime.datetime.now()
usr = raw_input('Enter Username:')
pwd = getpass.getpass('Enter Password:')

with open('Fetch.txt') as f:
    for line in f:
        line = line.strip()
        with open(os.devnull, "wb") as limbo:
            ip = line
            result = subprocess.Popen(["ping", "-n", "2", "-w", "200", ip],
                    stdout=limbo, stderr=limbo).wait()
            if result:
                    print ip, "Link Down - Site unreachable"
                    f = open('Down Sites.txt', 'a+')
                    f.write( line + '\n' )
                    f.close()
            else:
                    try:
                        dssh = paramiko.SSHClient()
                        dssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
                        dssh.connect(line, username=usr, password=pwd,timeout=60)
                        stdin, stdout, stderr = dssh.exec_command('sh run | i hostname')
                        mystring = stdout.read()
                        v = mystring[9:]
                        dssh.connect(line, username = usr, password = pwd)
                        chan = dssh.invoke_shell()
                        chan.send("conf t\n tacacs-server key %s\n"%v)
                        time.sleep(2)
                        resp = chan.recv(9999)
                        f=open('Output.txt',"a+")
                        f.write(resp+ '\n')
                        f.close()
                        print "Command Executed"
                    except Exception as e:
                        err = str(e)
                        f = open('Exceptions.txt', 'a+')
                        f.write(ip + ": " + err + '\n')
                        f.close()
                        print ip, "ERROR:", e
dssh.close()

Thank u all. 谢谢大家

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM