from idlelib.iomenu import encoding import paramiko import os import time import random import datetime import pandas as pd import re import numpy as np from sqlalchemy import text, create_engine import psycopg2 from psycopg2 import sql from sqlalchemy.orm import sessionmaker from sqlalchemy.dialects.postgresql import insert as pg_insert from sqlalchemy import create_engine, MetaData, Table, inspect import schedule import telnetlib import threading import time def get_info_telnet(host_ip, username, password): #host_ip = '129.60.161.169' #username = 'zte' #password = 'zte' tn = telnetlib.Telnet() try: tn.open(host_ip, port=23, timeout=5) print('%s connected ssuccess !' % host_ip) tn.read_until(b'Username:', timeout=5) tn.write(username.encode('ascii') + b'\n') tn.read_until(b'Password:', timeout=5) tn.write(password.encode('ascii') + b'\n') time.sleep(1) command_result = tn.read_until(b'#', timeout=5) if b'#' not in command_result: print('%s登录失败' % host_ip) else: print('%s登录成功' % host_ip) except: print('%s网络连接失败' % host_ip) command = "show clock" command = bytes(command, encoding='utf-8') tn.write(command + b'\r\n') run_time = tn.read_until(b'#') run_time = re.findall(r"\d+:\d+:\d+\s+\w+\s+\w+\s+\w+\s+\d+\s+2024", run_time.decode('GB18030'))[0] command = "show interface brief" command = bytes(command, encoding='utf-8') tn.write(command + b'\n') time.sleep(1) result_list = [] while (True): command_result = tn.read_very_eager().decode('ascii') # print(command_result) result_list.append(command_result) if re.findall(r"--More--", command_result.strip()): tn.write(b" ") elif re.findall(r"#", command_result.strip()): break else: time.sleep(0.05) continue result_str = "\n".join(result_list) list_str = result_str.split('\n') pd_result = pd.DataFrame() list_temperature_vec = [] for j in list_str: regex = re.compile(r'\w+gei.+\s+.+\s+.+\s+.+\s+.+\s+.+\s+.+', re.S) # print(regex.findall(j)) # print(len(regex.findall(j))) if len(re.findall(r"Interface", j)) > 0: new_columns = list_find_str = re.split(r'\s+', j) new_columns = new_columns[0:8] if len(regex.findall(j)) > 0: list_find_str = regex.findall(j)[0] list_find_str = re.split(r'\s+', list_find_str) list_temperature_vec.append(list_find_str) pd_result = pd.DataFrame(list_temperature_vec) pd_result.columns = new_columns tn.close() return pd_result #修改配置函数 def set_conf_telnet(host_ip, username, password): #host_ip = '129.60.161.169' #username = 'zte' #password = 'zte' tn = telnetlib.Telnet() try: tn.open(host_ip, port=23, timeout=5) print('%s connected ssuccess !' % host_ip) tn.read_until(b'Username:', timeout=5) tn.write(username.encode('ascii') + b'\n') tn.read_until(b'Password:', timeout=5) tn.write(password.encode('ascii') + b'\n') time.sleep(1) command_result = tn.read_until(b'#', timeout=5) if b'#' not in command_result: print('%s登录失败' % host_ip) else: print('%s登录成功' % host_ip) except: print('%s网络连接失败' % host_ip) command = "show clock" command = bytes(command, encoding='utf-8') tn.write(command + b'\r\n') run_time = tn.read_until(b'#') run_time = re.findall(r"\d+:\d+:\d+\s+\w+\s+\w+\s+\w+\s+\d+\s+2024", run_time.decode('GB18030'))[0] command = "configure t" command = bytes(command, encoding='utf-8') tn.write(command + b'\n') time.sleep(1) command = "interface xgei-0/2/0/13" command = bytes(command,encoding='utf-8') tn.write(command + b'\n') time.sleep(1) command = "no shutdown" command = bytes(command, encoding='utf-8') tn.write(command + b'\n') time.sleep(1) command = "show interface brief" command = bytes(command, encoding='utf-8') tn.write(command + b'\n') time.sleep(1) result_list = [] while (True): command_result = tn.read_very_eager().decode('ascii') # print(command_result) result_list.append(command_result) if re.findall(r"--More--", command_result.strip()): tn.write(b" ") elif re.findall(r"#", command_result.strip()): break else: time.sleep(0.05) continue result_str = "\n".join(result_list) list_str = result_str.split('\n') pd_result = pd.DataFrame() list_temperature_vec = [] for j in list_str: regex = re.compile(r'\w+gei.+\s+.+\s+.+\s+.+\s+.+\s+.+\s+.+', re.S) # print(regex.findall(j)) # print(len(regex.findall(j))) if len(re.findall(r"Interface", j)) > 0: new_columns = list_find_str = re.split(r'\s+', j) new_columns = new_columns[0:8] if len(regex.findall(j)) > 0: list_find_str = regex.findall(j)[0] list_find_str = re.split(r'\s+', list_find_str) list_temperature_vec.append(list_find_str) pd_result = pd.DataFrame(list_temperature_vec) pd_result.columns = new_columns tn.close() return pd_result if __name__ == '__main__': host_ip = '129.60.161.169' username = 'zte' password = 'zte' set_conf_telnet(host_ip,username,password) #pd_output = get_info_telnet(host_ip, username, password) #pd_output.to_csv('status' + datetime.datetime.strftime(datetime.datetime.now(), '%Y%m%d%H%M%S') + '.csv',index=False)
python数通运维基础---配置变更(待完善)
于 2024-09-24 11:18:54 首次发布