Python - 检索 Cisco CLI 命令的输出并将其用作输入
Python - Retrieve the output of Cisco CLI command and use it as input
下面是我的工作代码,用于在 Cisco 路由器和交换机上执行 CLI 命令。我手头有一项任务,要求我从之前执行的 Cli 命令的输出中读取特定值,并将其用作要在 Cisco 设备上执行的另一个命令的输入。
以下代码的输出:
测试#sh 运行 |我主机名
主机名测试
测试#
我的任务是仅从输出中获取 "TEST" 并将其用作相同代码中另一个命令的输入,如下所示,
chan.send("conf t\n")
chan.send("tacacs server key TEST\n")
chan.send("exit\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()
谢谢大家
下面是我的工作代码,用于在 Cisco 路由器和交换机上执行 CLI 命令。我手头有一项任务,要求我从之前执行的 Cli 命令的输出中读取特定值,并将其用作要在 Cisco 设备上执行的另一个命令的输入。
以下代码的输出:
测试#sh 运行 |我主机名
主机名测试
测试#
我的任务是仅从输出中获取 "TEST" 并将其用作相同代码中另一个命令的输入,如下所示,
chan.send("conf t\n")
chan.send("tacacs server key TEST\n")
chan.send("exit\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()
谢谢大家