【发布时间】:2018-07-29 19:46:14
【问题描述】:
我能够对 cisco 设备执行 ssh 并且工作正常。我能够运行 EXEC 模式命令,但无法执行特权命令。看起来我需要进入启用模式,但我不确定需要在 ssh 中传递的参数。我的代码如下。你能帮我么?感谢您的帮助。
import os
import getpass
import paramiko
import xlsxwriter
import socket
import re
import sys
import smtplib
username = 'username'
password = 'password'
print "Running the tests..this might take some time.."
# Opens file in read mode
f1 = open('hostfile','r')
f2 = open('commandfile','r')
# Creates list based on f1
devices = f1.readlines()
commands = f2.readlines()
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
data = []
for device in devices:
column = device.split()
data.append([column[0]])
print column[0]
for command in commands:
try:
conn=ssh.connect(column[0], username=username,
password=password, timeout=4)
if conn is None:
stdin, stdout, stderr = ssh.exec_command(command)
x=stdout.readlines()
print
'*****************************************************************'
print ' Output of ' + command + ' on ' + device
print
'*****************************************************************'
raw_input("Press any key to continue.")
for j in x:
print j
data[-1].append(stdout.read())
ssh.close()
except paramiko.AuthenticationException:
output = "Authentication Failed"
data[-1].append(output)
break
except paramiko.SSHException:
output = "Issues with SSH service"
data[-1].append(output)
break
except socket.error, e:
output = "Connection Error"
data[-1].append(output)
break
data[-1] = tuple(data[-1])
f1.close()
f2.close()
【问题讨论】: