哈哈哈逗你玩的随便编一下
list去重:
list1 = [1.1,1,5,4,3,2,1]
listset = set(list1)
====================================
def write_txt():
#1.文件路径,不写前缀在当前目录,2权限(w 写,r度,b 二级制,a+尾部追加) 3 encoding 编码格式
f = open(‘xxxx.txt’,‘a+’,encoding=‘utf8’)
f.write(‘测试内容’)
f.close()
write_txt()
with open(‘0904test4_peizhi.txt’,‘r’) as f :
cmds = f.readlines()
===========================================
wb = xlwt.Workbook()
sheet= wb.add_sheet(‘用户信息’)
sheet.write(0,0,‘编号’)
wb.save(‘0903_test4_biaoge.xls’)
wb = xlrd.open_workbook(‘0903_test4_biaoge.xls’)
#1.方法通过sheet名读取
sheet = wb.sheet_by_name(‘用户信息’)
print(sheet.nrows)#读取表格行数
print(sheet.ncols)#读取表格列数
print(sheet.row_values(0))#按行读取,返回list
print(sheet.col_values(0)) # 按列读取,返回list
print(sheet.cell_value(0,0)) # 按行读取,返回表格值
print(sheet.cell(0,0)) # 按行读取,返回cell
=======================================
ssh_my = paramiko.SSHClient()
ssh_my.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh_my.connect(hostname=ip ,port=22,username=‘‘,password=’’)
print(‘登录成功’)
my_cmd=ssh_my.invoke_shell()
my_cmd.send(b’n\n’)
print(my_cmd.recv(65535).decode(‘utf-8’))
while True:
cmd_input = input(‘请输入命令’)
if cmd_input == ‘exit’:
break
my_cmd.send((cmd_input+‘\n’).encode(‘utf-8’))
time.sleep(3)
print(my_cmd.recv(65535).decode(‘utf-8’))
ftp:
time_now_str = time.strftime(‘%Y_%m_%d_%H_%M_%S’)
my_sftp = ssh.open_sftp()
my_sftp.get(remotepath=‘vrpcfg.cfg’,localpath=‘{}_cfg.cfg’.format(time_now_str))
print(‘get成功’)
my_sftp.put(localpath=‘0902.txt’,remotepath=‘puttest.txt’)
print(‘put成功’)
=getCmd=======
from pysnmp.hlapi import *
etinfo = getCmd(SnmpEngine(),
UsmUserData(userName=‘—’,authKey=‘—’,privKey=‘—’,authProtocol=usmHMACSHAAuthProtocol,privProtocol=usmAesCfb128Protocol),
UdpTransportTarget((ip,161)) ,
ContextData(),
#ObjectType(ObjectIdentity(‘1.3.6.1.2.1.1.5.0’))#取设备SNMP
ObjectType(ObjectIdentity(‘1.3.6.1.4.1.2011.5.25.31.1.1.1.1.5.16842753’))
)
或者直接 ObjectType(ObjectIdentity(‘SNMPv2-MIB’, ‘sysDescr’, 0))) #取sysDescr的值
a,b,c,d = next(getinfo)
#a,b,c 是None 0 0
for i in d :
print(type(i))
print(type(i[-1]))
print(str(i[-1]))
‘’’
<class ‘pysnmp.smi.rfc1902.ObjectType’>
<class ‘DisplayString’>
HUAWEI
‘’’
bulkCmd=============
getinfos = bulkCmd(
SnmpEngine(),
UsmUserData(userName=‘—’,authKey=‘----’,privKey=‘----’,authProtocol=usmHMACSHAAuthProtocol,privProtocol=usmAesCfb128Protocol),
UdpTransportTarget((ip,161)) ,
ContextData(),0,50,
ObjectType(ObjectIdentity(‘1.3.6.1’)),
)
for (errorIndication, errorStatus, errorIndex, varBinds) in getinfos:
if not errorIndication and not errorStatus:
oid_value = []
for varBind in varBinds:
# result = ‘,’.join([x.prettyPrint() for x in varBind])
#oid_value = [x.prettyPrint() for x in varBind]
oid_value = [x for x in varBind]
result.append(oid_value)
for i in result:
if (‘[’+‘1.3.6.1.2.1.2.2.1.2.’) in str(i):
print(str(i))
‘’’
[ObjectIdentity(<ObjectName value object, tagSet <TagSet object, tags 0:0:6>, payload [1.3.6.1.2.1.2.2.1.2.2]>), <OctetString value object, tagSet <TagSet object, tags 0:0:4>, subtypeSpec <ConstraintsIntersection object, consts <ValueSizeConstraint object, consts 0, 65535>>, encoding iso-8859-1, payload [NULL0]>]
[ObjectIdentity(<ObjectName value object, tagSet <TagSet object, tags 0:0:6>, payload [1.3.6.1.2.1.2.2.1.2.3]>), <OctetString value object, tagSet <TagSet object, tags 0:0:4>, subtypeSpec <ConstraintsIntersection object, consts <ValueSizeConstraint object, consts 0, 65535>>, encoding iso-8859-1, payload [InLoopBack0]>]
‘’’
for i in result:
if (‘[’+‘1.3.6.1.2.1.2.2.1.2.’) in str(i):
print(str(i).split(‘payload [’)[-1].split(‘]>’)[0])
‘’’
NULL0
InLoopBack0
‘’’
=ncclient=============
import ncclient.manager as mg
from ncclient.xml_ import to_ele
ip = ‘----’
mymg=mg.connect(host=ip,username=‘----’,password=‘-----’,look_for_keys = False,
hostkey_verify=False,allow_agent = False,device_params={‘name’:‘huawei’})
查看全部配置
running_config = mymg.get_config(‘running’)
单项配置
XMLS=‘’
content = to_ele(XMLS)
res = mymg.rpc(content)
print(res)
mymg.close_session()