本文整理汇总了Python中netmiko.ConnectHandler.check_enable_mode方法的典型用法代码示例。如果您正苦于以下问题:Python ConnectHandler.check_enable_mode方法的具体用法?Python ConnectHandler.check_enable_mode怎么用?Python ConnectHandler.check_enable_mode使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类netmiko.ConnectHandler
的用法示例。
在下文中一共展示了ConnectHandler.check_enable_mode方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: main
# 需要导入模块: from netmiko import ConnectHandler [as 别名]
# 或者: from netmiko.ConnectHandler import check_enable_mode [as 别名]
def main():
try:
hostname = raw_input("Enter remote host to test: ")
username = raw_input("Enter remote username: ")
except NameError:
hostname = input("Enter remote host to test: ")
username = input("Enter remote username: ")
linux_test = {
'username': username,
'use_keys': True,
'ip': hostname,
'device_type': 'ovs_linux',
'key_file': '/home/{}/.ssh/test_rsa'.format(username),
'verbose': False}
net_connect = ConnectHandler(**linux_test)
print()
print(net_connect.find_prompt())
# Test enable mode
print()
print("***** Testing enable mode *****")
net_connect.enable()
if net_connect.check_enable_mode():
print("Success: in enable mode")
else:
print("Fail...")
print(net_connect.find_prompt())
net_connect.exit_enable_mode()
print("Out of enable mode")
print(net_connect.find_prompt())
# Test config mode
print()
print("***** Testing config mode *****")
net_connect.config_mode()
if net_connect.check_config_mode():
print("Success: in config mode")
else:
print("Fail...")
print(net_connect.find_prompt())
net_connect.exit_config_mode()
print("Out of config mode")
print(net_connect.find_prompt())
# Test config mode (when already at root prompt)
print()
print("***** Testing config mode when already root *****")
net_connect.enable()
if net_connect.check_enable_mode():
print("Success: in enable mode")
else:
print("Fail...")
print(net_connect.find_prompt())
print("Test config_mode while already at root prompt")
net_connect.config_mode()
if net_connect.check_config_mode():
print("Success: still at root prompt")
else:
print("Fail...")
net_connect.exit_config_mode()
# Should do nothing
net_connect.exit_enable_mode()
print("Out of config/enable mode")
print(net_connect.find_prompt())
# Send config commands
print()
print("***** Testing send_config_set *****")
print(net_connect.find_prompt())
output = net_connect.send_config_set(['ls -al'])
print(output)
print()
示例2: IOSDevice
# 需要导入模块: from netmiko import ConnectHandler [as 别名]
# 或者: from netmiko.ConnectHandler import check_enable_mode [as 别名]
class IOSDevice(BaseDevice):
def __init__(self, host, username, password, secret='', port=22, **kwargs):
super(IOSDevice, self).__init__(host, username, password, vendor='cisco', device_type=IOS_SSH_DEVICE_TYPE)
self.native = None
self.host = host
self.username = username
self.password = password
self.secret = secret
self.port = int(port)
self._connected = False
self.open()
def open(self):
if self._connected:
try:
self.native.find_prompt()
except:
self._connected = False
if not self._connected:
self.native = ConnectHandler(device_type='cisco_ios',
ip=self.host,
username=self.username,
password=self.password,
port=self.port,
secret=self.secret,
verbose=False)
self._connected = True
def close(self):
if self._connected:
self.native.disconnect()
self._connected = False
def _enter_config(self):
self._enable()
self.native.config_mode()
def _enable(self):
self.native.exit_config_mode()
if not self.native.check_enable_mode():
self.native.enable()
def _send_command(self, command, expect=False, expect_string=''):
if expect:
if expect_string:
response = self.native.send_command_expect(command, expect_string=expect_string)
else:
response = self.native.send_command_expect(command)
else:
response = self.native.send_command(command)
if '% ' in response or 'Error:' in response:
raise CommandError(command, response)
return response
def config(self, command):
self._enter_config()
self._send_command(command)
self.native.exit_config_mode()
def config_list(self, commands):
self._enter_config()
entered_commands = []
for command in commands:
entered_commands.append(command)
try:
self._send_command(command)
except CommandError as e:
raise CommandListError(
entered_commands, command, e.cli_error_msg)
self.native.exit_config_mode()
def show(self, command, expect=False, expect_string=''):
self._enable()
return self._send_command(command, expect=expect, expect_string=expect_string)
def show_list(self, commands):
self._enable()
responses = []
entered_commands = []
for command in commands:
entered_commands.append(command)
try:
responses.append(self._send_command(command))
except CommandError as e:
raise CommandListError(
entered_commands, command, e.cli_error_msg)
return responses
def save(self, filename='startup-config'):
command = 'copy running-config %s' % filename
expect_string = '\[%s\]' % filename
self.show(command, expect=True, expect_string=expect_string)
time.sleep(5)
#.........这里部分代码省略.........