def login(device_info):
    global conn
    start_timer = datetime.now()
    try:
        conn = ch(**device_info, session_timeout=200, global_delay_factor=2)
    except Exception:
        # print(f"{device_info['session_log']} 连接失败,查看是否有登录权限/账号密码是否正确。")
        FAILED.append(device_info['session_log'])
    if device_info['device_type'] == 'hp_comware':
        conn.send_command(HP_COMMAND)
        # conn.read_until_pattern(r'.*(return\b)|(end\b).*')
    elif device_info['device_type'] == 'huawei':
        conn.send_command(HW_COMMAND)
    else:
        # conn.send_command(CISCO_COMMAND,expect_string=r'.*[(ore)(ORE)].*')
        conn.send_config_set(TEST)  # 针对现网迈普设备,more off == screen disable
        # conn.read_until_pattern(r'.*(return\b)|(end\b).*')
    time.sleep(10)
    ##    if conn.read_until_pattern(r'.*(return\b)|(end\b).*'):
    # if conn.read_until_prompt():
    # conn.close_session_log()
    # conn.disconnect()
    print(
        f"备份 {device_info['session_log']}\t共耗时\t {datetime.now() - start_timer}秒"
    )
Example #2
0
from netmiko import ConnectHandler as ch
from re import search
from datetime import datetime


password = '******'             #Credentials 
user = '******'                #Credentials
report_file = 'REPORT_FILE_NAME'            #Report file name

device = {'ip': 'x.x.x.x', \                #IP address of the ASA
         'device_type': 'cisco_asa',\
         'username': user, \
         'password': password, \
         'secret': password}

with ch(**device) as ssh:
  ssh.enable()
  line = ssh.send_command('show vpn-sessiondb summary')

val = search('\d+',line.split('\n')[5]).group()

timestamp = datetime.now()
stamp = str(timestamp).split('.')[0]

with open(report_file,'a') as file:
  line = str(stamp) + ',' + str(val) + '\n'
  file.write(line)
Example #3
0
from netmiko import ConnectHandler as ch 
from getpass import getpass
userName = input("UserName: "******"password: "******"command: ")
get_ip = input("provide ip address: ")
device_details = [{"device_type":"cisco_ios","ip":get_ip, "username":userName, "password":password}]
connect = ch(**device_details[0])
out = connect.send_command(command)
try:
    for l in range(0,len(device_details)):
        with open(f"device{int(l+1)}", "w") as file:
            file.write(out)
except:
    print("No such file found")
Example #4
0
a3 = A3()
a3.foo()  # in A \n in A3

# 重写 __init__ 不会自动调用基类的 __init__,如果需要使用基类的 __init__ 方法的话,使用 super()


class A4(A):
    def __init__(self) -> None:
        super().__init__()
        self.foo()


a4 = A4()  # in A , 因为初始化时候执行了基类的 foo 方法
print(a4.name)  # A

from netmiko import ConnectHandler as ch

host = {
    'device_type': 'hp_comware',
    'host': '192.168.56.20',
    'username': '******',
    'password': '******',
    'port': 22,
    'secret': '',  # enable密码,没有可以不写这行
}

cmds = ['dis int br', 'dis dev man', 'dis ip int br']
conn = ch(**host)
ret = conn.send_config_set(cmds)
print(ret)
Example #5
0
def config_backup(host, diff):
    conn = ch(**host)
    running_config = conn.send_command("display current configuration")
    startup_config = conn.send_command("more flash:/startup.cfg")
    return
Example #6
0
def exec_command_timing(host, command, **kwargs):
    conn = ch(**host)
    output = conn.send_command_timing(command, **kwargs)
    conn.disconnect()
    return output