Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/362.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python-检索Cisco CLI命令的输出并将其用作输入_Python_Cisco - Fatal编程技术网

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

Python-检索Cisco CLI命令的输出并将其用作输入,python,cisco,Python,Cisco,下面是我的工作代码,用于在Cisco路由器和交换机上执行Cli命令。我手头有一项任务,要求我从先前执行的Cli命令的输出中读取特定值,并使用is作为要在Cisco设备上执行的另一个命令的输入 以下代码的输出: TESTsh运行| i主机名 主机名测试 试验 我的任务是只从输出中获取TEST,并将其用作另一个命令的输入,代码如下: chan.sendconf\n chan.sendtacs服务器密钥测试\n chan.sendexit\n 请在这方面指导我。提前谢谢 import paramiko

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

以下代码的输出:

TESTsh运行| i主机名

主机名测试

试验

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

chan.sendconf\n

chan.sendtacs服务器密钥测试\n

chan.sendexit\n

请在这方面指导我。提前谢谢

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

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

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()
谢谢大家