我已经尝试调试了几个小时,但我仍然不知道为什么这样做。在这段代码中,我将使用python的paramiko库使用SSH连接到交换机。我已经编写了可以正常工作的paramiko SSH程序,但是由于某种原因我可以上班。我试图连接到此设备,调用外壳程序,然后输入两个命令:“ system-view”和“ show interface brief”。我认为罪魁祸首是“系统视图”,因为例外:
paramiko.ssh_exception.ChannelException: (1, 'Administratively prohibited')
不断抛出,我不知道为什么要抛出它,或者在这种情况下“行政上禁止”是什么意思。我知道系统视图用于输入实际系统,因此您可以进行所需的命令
我尝试添加timeout = 10,诸如此类,但它不起作用:
stdin, stdout, stderr = ssh.exec_command("system-view", timeout=10)
这是我正在使用的代码:
def displayInterfaceBrief_SNET(ssh): # I'm only concerned about this function for now
channel = ssh.invoke_shell()
stdin, stdout, stderr = ssh.exec_command("system-view")
stdin_2, stdout_2, stderr = ssh.exec_command("show interface brief")
print(stdout_2.read())
time.sleep(2)
channel.close()
def runMain():
scriptName = os.path.basename(__file__)
print("The name of this script is: " + scriptName)
print("**************************************\n")
print("This script allows you to enable and disable ports on the SNET or SOOBM switches, have fun ! \n")
print("**************************************\n")
scriptPurpose = 'This script enables and disables the SAN switches'
parser = argparse.ArgumentParser(description=scriptPurpose, formatter_class=RawTextHelpFormatter)
parser.add_argument("-deviceIP", help="Target device IP address", type=str)
parser.add_argument("-deviceUsername", help="Target device username", type=str)
parser.add_argument("-devicePassword", help="Target device password", type=str)
args = parser.parse_args()
if args.deviceIP is None:
print("The device IP parameter is blank\n")
else:
deviceIP = args.deviceIP
if args.deviceUsername is None:
print("The device userName parameter is blank\n")
else:
deviceUsername = args.deviceUsername
if args.devicePassword is None:
print("The device password parameter is blank\n")
else:
devicePassword = args.devicePassword
print("This script allows you to enable and disable ports on an HPE switch")
confirm_ping = canPing(deviceIP)
if confirm_ping:
print("The switch is pingable, let's proceed !\n")
else:
print("This device is not pingable unfortunately, sorry... : (\n")
sys.exit(-1)
ssh_connection = connectToSSH_SNET(deviceIP, deviceUsername, devicePassword)
while True:
SNET_options()
user_input = input("Select an option...")
switch_result = SNET_switch_func(user_input)
if switch_result == "displayInterfaceBrief": # I'm only concerned about here for now
displayInterfaceBrief_SNET(ssh_connection)
elif switch_result == "enablePort":
enablePort_SNET(ssh_connection)
elif switch_result == "disablePort":
disablePort_SNET(ssh_connection)
elif switch_result == "switchReboot":
reboot_SNET_switch(ssh_connection)
else:
print("Exiting the program now....")
sys.exit(-1)
这是回溯:
No handlers could be found for logger "paramiko.transport"
Traceback (most recent call last):
File "remove_bridge_aggragation.py", line 347, in <module>
runMain()
File "remove_bridge_aggragation.py", line 334, in runMain
displayInterfaceBrief_SNET(ssh_connection)
File "remove_bridge_aggragation.py", line 110, in displayInterfaceBrief_SNET
stdin, stdout, stderr = ssh.exec_command("system-view")
File "C:\Python27\lib\site-packages\paramiko\client.py", line 493, in exec_command
chan = self._transport.open_session(timeout=timeout)
File "C:\Python27\lib\site-packages\paramiko\transport.py", line 806, in open_session
timeout=timeout,
File "C:\Python27\lib\site-packages\paramiko\transport.py", line 944, in open_channel
raise e
paramiko.ssh_exception.ChannelException: (1, 'Administratively prohibited')
感谢您的帮助!