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

时间:2015-09-14 08:05:49

标签: python cisco

以下是我的工作代码,用于在Cisco路由器和交换机上执行Cli命令。我手头有一个任务,要求我从先前执行的Cli命令的输出中读取特定值,并将其用作在Cisco设备上执行的另一个命令的输入。

输出以下代码:

TEST #sh run |我主机名

主机名TEST

TEST#

我的任务是只获取" TEST"从输出中将其用作另一个命令的输入,代码如下,

chan.send(" conf t \ n")

chan.send(" tacacs服务器密钥TEST \ n")

chan.send("退出\ n&#34)

请指导我。提前谢谢。

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()

1 个答案:

答案 0 :(得分:0)

我能够让它工作,只是研究并得到了这个解决方案,

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()

谢谢大家。