文章目录
- 1.百度人脸api:https://ai.baidu.com/docs#/Auth/top 获取Access Token
- 2.aiohttp: tomcat配置文件中监听的IP地址设置了0.0.0.0表示tomcat服务器监听在本机的所有IP地址,通任何一个IP都可访问到
- 3.curl(http请求):-h:查看可接的参数,-s:省略进度条,-o:output,-H:请求头,-d:请求内容,-X:请求方法
- 4.闭包和装饰器:函数里return就是闭包
- 5.解析eeprom:如下是二进制文件,C8是一个字节
- 6.json/configparser/optparse:json.dumps(将字典转化为字符串,将json信息写进文件),json.loads(将字符串转化为字典)
- 7.map:[]列表 , ()元组 , {}集合 , {a:b}字典。for else中break后不走else
- 8.walk/split/getattr/bin/lspci/ethtool/usb/serial:bin转为二进制0b...,re.compile
1.百度人脸api:https://ai.baidu.com/docs#/Auth/top 获取Access Token
# encoding:utf-8
import os
import base64
import json
import xlwt
import urllib.request,urllib.parse
request_url = "https://aip.baidubce.com/rest/2.0/face/v3/detect" # 在人脸识别API文档中人脸检测中
row = 0
col = 0
workbook = xlwt.Workbook('123.xls')
worksheet = workbook.add_sheet("gongzuobiao")
worksheet.write(row, col+0, "时间")
worksheet.write(row, col+1, "相机ID")
worksheet.write(row, col+2, "性别")
worksheet.write(row, col+3, "年龄")
worksheet.write(row, col+4, "种族")
worksheet.write(row, col+5, "表情")
for imgname in os.listdir("G:/img3"):
print("imgname:",imgname) # imgname:
data = imgname.split('_')[0]
print("data:", data) # data:
camera = imgname.split('_')[2].split(".")[0]
print("camera:",camera) # camera:
imgpath = "G:/img3"+imgname
f = open(imgpath, 'rb')
img = base64.b64encode(f.read())
params ={"image":img,
"image_type":"BASE64",
"face_field":"age,gender,race,emotion"}
params = urllib.parse.urlencode(params).encode(encoding='UTF8')
access_token = '24.0449cf2437689f550ca5f93dac53fce3.2592000.1574310334.282335-17084453'
request_url = request_url + "?access_token=" + access_token
request = urllib.request.Request(url=request_url, data=params)
request.add_header('Content-Type', 'application/json')
response = urllib.request.urlopen(request)
content = response.read()
print("content:",content)
if content:
str = content.decode()
content = json.loads(str)
results = content['result']
if results:
row += 1
for item in results["face_list"]:
print("item:\n",item['gender']['type'])
worksheet.write(row, col + 0, data)
worksheet.write(row, col + 1, camera)
worksheet.write(row, col + 2, item['gender']['type'])
worksheet.write(row, col + 3, item['age'])
worksheet.write(row, col + 4, item['race']['type'])
worksheet.write(row, col + 5, item['emotion']['type'])
workbook.save('123.xls')
2.aiohttp: tomcat配置文件中监听的IP地址设置了0.0.0.0表示tomcat服务器监听在本机的所有IP地址,通任何一个IP都可访问到
localhost
:是一个域名,默认指向127.0.0.1这ip(本地回环地址,速度快,不会出现在主机外部网络),绑定了localhost的服务只能在本机访问。
# a.py:创建一个后端app应用
from aiohttp import web
# from application.routes import setup_routes
app = web.Application()
# setup_routes(app)
web.run_app(app, host='0.0.0.0', port=9000) #开启后端
python a.py:在浏览器中打开http://localhost:9000/
或者使用命令curl -X GET http://localhost:9000
,对于请求现在只会返回404: Not Found。
如下创建一个视图(返回值)和路由
并在a.py中将注释的两行放开运行: curl -X GET localhost:9000/hello ,Hello Aiohttp!
# application文件夹/views.py
from aiohttp import web
async def hello(request):
return web.Response(text='Hello Aiohttp!')
# application文件夹/routes.py
from .view import hello
def setup_routes(app):
app.router.add_get('/hello', hello) # 第二个hello是调用views.py中函数,相当于response。
# 第一个/hello可单独写一个.py文件里面写成如下,取board_routes[0]
# board_routes = [
# "/api/psu/info",
# "/api/psu/info/{name}",
# ]
如下问题是发送的curl中data的json数据格式不对。404 not found是curl中url路径错误。
2.1 fb openbmc中restful:Connection refused说明restapi服务没起,curl http://127.0.0.1:8080/api/v1.0/info
# board_endpoint.py:视图即response,调用rest_bmc.py中函数接口
import re
import rest_help
import rest_psu
import rest_fan
from aiohttp import web
from rest_utils import dumps_bytestr
class boardApp_Handler:
async def rest_help_hdl(self, request):
return web.json_response( rest_help.get_help(), dumps=dumps_bytestr)
async def rest_psu_model_name_hdl(self, request):
return web.json_response( rest_psu.get_model_name(request.match_info["name"]), dumps=dumps_bytestr)
# boardroutes.py:路由即/
board_routes = []
board_routes.append("/api/help")
board_routes.append("/api/psu/{name}/model_name")
# board_setup_routes.py:连接作用
from board_endpoint import boardApp_Handler
from boardroutes import *
from aiohttp.web import Application
def setup_board_routes(app: Application, write_enabed: bool):
bhandler = boardApp_Handler()
# 下行第一个参数路径(boardroutes.py路由),第二个参数response(board_endpoint.py视图)
app.router.add_get(board_routes[0], bhandler.rest_help_hdl)
app.router.add_get(board_routes[3], bhandler.rest_psu_model_name_hdl)
3.curl(http请求):-h:查看可接的参数,-s:省略进度条,-o:output,-H:请求头,-d:请求内容,-X:请求方法
3.1 用法:没有-H指定header,但是默认附带3个header
如下exe程序主要监听88端口并能将请求内容
打印出来。
如下多出一行请求头。
如下会post带参数-d,自动多出content-*两个请求头。
如下content-type以json形式提交替代表单形式提交。
3.2 restful获取动态ip:runsv /etc/sv/restapi &,sv d restapi(停止)
# /mnt/data/etc/rc.local
#!/bin/bash
/usr/local/bin/set_fan_speed.sh 30
if [ ! -f /usr/local/bin/sshpass ];then
ln -snf /mnt/data/etc/sshpass /usr/local/bin #将前者sshpass复制到后面文件夹里
fi
ln -snf /mnt/data/etc/debug.api /usr/local/bin
/usr/bin/python3 /mnt/data/etc/client.py 10.75.92.230 a &
sshpass -p密码(不会跳出输入密码),-o StrictHostKeyChecking=no:内网中非常信任的服务器之间的ssh连接或/etc/ssh下vi ssh_config。
# client.py放在需要得到ip的机器上即10.75.92.1
#!/usr/bin/python3
# -*- coding: utf-8 -*-
import socket
import subprocess
import sys
import json
import time
def get_self_ip():
try:
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.connect(("8.8.8.8", 80))
ip = s.getsockname()[0]
finally:
s.close()
return ip # 本身ip
if len(sys.argv) == 3:
server = sys.argv[1] # 编译服务器ip
client = sys.argv[2] # 标识
else:
print ("usage")
while True:
url_body = {}
url_body["slave"] = client # 上面定义
url_body["ip"] = get_self_ip()
cmd = "curl -s http://{}:8080/api/slave -d '{}'".format(server, json.dumps(url_body, ensure_ascii=False))
# print(cmd) # curl -s http://10.75.92.230:8080/api/slave -d '{"ip": "10.75.92.1", "slave": "a"}'
# -d:data 即server.py中data 即 请求request数据
(returncode, output) = subprocess.getstatusoutput(cmd) #subprocess启动一个新进程
print (output)
if returncode == 0:
if output:
d = json.loads(output)
if d:
if 'result' in d.keys():
if d['result'] == 'success':
print ('success')
break
time.sleep(300) # 5 minute
# python3 client.py 10.75.92.230 a (必须先开启服务端且用python3)
# {"result": "success", "data": {"slave": {"a": "10.75.92.1"}}}
// usr_slave.json文件,其中a是python3 client.py 10.75.92.230 a 多出
{
"slave": {
"a": "10.75.92.1",
}
}
# server.py
#!/usr/bin/python3
# -*- coding: utf-8 -*-
from aiohttp import web
import json
import requests
import json
import sys
PATH = "./usr_slave.json"
def parse_config_file():
with open(PATH,'r') as load_f:
load_dict = json.load(load_f)
return load_dict
def add_slave(data): # 写入json
configs = parse_config_file()
if not configs:
configs = {}
configs['slave'] = {}
configs['slave'][data['slave']] = data['ip'] # configs['slave'][a]
with open(PATH, 'w') as write_f:
json.dump(configs, write_f, indent=4, ensure_ascii=False) # 将configs写入write_f对象
return {'result':'success','data': configs}
def update_slave(data):
configs = parse_config_file()
configs['slave'][data['slave']] = data['ip']
with open(PATH, 'w') as write_f:
json.dump(configs, write_f, indent=4, ensure_ascii=False)
return {'result':'success','data': configs}
async def get_help(request):
configs = parse_config_file()
return web.json_response({'result':'success','data': configs})
async def slave(request):
data = await request.json() # await阻塞等待执行结束
configs = parse_config_file()
if 'slave' in data or 'ip' in data:
if configs:
if configs['slave']:
for k in configs['slave'].keys():
if k == data['slave']:
if k == data['slave'] and configs['slave'][k] == data['ip']:
return web.json_response({'result':'success','data': configs})
else:
return web.json_response(update_slave(data))
return web.json_response(add_slave(data))
else:
return web.json_response(add_slave(data))
def app_factory(args=()):
app = web.Application()
app.router.add_get('/api/help', get_help)
app.router.add_post('/api/slave', slave)
return app
# curl -s http://10.75.92.230:8080/api/help | python -m json.tool
{
"data": {
"slave": {
"a": "10.75.92.1",
}
},
"result": "success"
}
3.3 curl认证:app = web.Application(middlewares=[jsonerrorhandler, auth_enforcer]),利用aiohttp创建一个app实例并指定两个middlewares中间件函数(request和response的中间)
basic auth:最简单的一种(户名+密码)认证方式,用base64_encode加密,安全性低,每次都需要传递用户名和密码,用户名和密码很大程度上存在被监听盗取的可能。
客户端curl -u usr:pwd
,服务端Headers中显示如下字段用base64_decode_auth_headers函数
解码,获取usr和pwd明文后,用creds_validator函数
对其加密并与/etc/shaodow下用户名和加密密码段做匹配,如果一致则处理curl请求,否则返回401: Unauthorized。
import spwd
import crypt
import base64
def base64_encode_auth_headers(username, password): # 没用到
temp_str = username + ':' + password
# convert bytes string
bytesString = temp_str.encode(encoding="utf-8")
# base64 encode
encodestr = base64.b64encode(bytesString)
return 'Basic ' + encodestr.decode()
def base64_decode_auth_headers(encodestr):
# convert bytes string
bytesString = encodestr.encode(encoding="utf-8")
# base64 decode
decodestr = base64.b64decode(bytesString)
return decodestr.decode()
def creds_validator(username, password):
crypted_root_pwd = spwd.getspnam(username).sp_pwd # 拿到/etc/shadow里bmc正确存此username对应密码的密文
crypted_method, crypted_salt = (crypted_root_pwd.split('$')[1],
crypted_root_pwd.split('$')[2])
result_str = '{0}{1}{0}{2}{0}'.format('$', crypted_method, crypted_salt)
crypted_input_passwd = crypt.crypt(password, result_str) # 按照上面正确方式给明文password加密
return crypted_input_passwd == spwd.getspnam(username).sp_pwd #对比,PASS :retrun "TRUE" ; NO PASS :return "FALSE"
def basic_auth(authstr): # basic_auth(request.headers["Authorization"]) #总和,调用上面接口
# compare authorization field whit encrypted password in shadow file
encode_str = authstr.split(' ')[1]
clearstr = base64_decode_auth_headers(encode_str)
username = clearstr.split(':')[0] #明文
password = clearstr.split(':')[1] #明文
return creds_validator(username, password)
$ sudo cat /etc/shadow | grep test
test:$6$sT8RMKkg$QLhvrR/iSwurIM0Z0.ZVkxPKRvKXueuAx7fZzq5Umffm3ydNbeGujV7mUKnTNYtbpLIbIdJ2.Q4Spw9OUjhl91:16502:0:99999:7:::
在我们每次改写密码时,都会随机生成一个这样的salt(某个固定长度的随机字符串)。我们登录时输入的明文密码经过上述的演化后与shadow里的密码域进行字符串比较,以此来判断是否允许用户登录。
// passwd.c
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <shadow.h>
#include <crypt.h>
#include <errno.h>
int main(int argc, char *argv[])
{
int i, j;
char salt[16];
char crypt_char[128];
struct spwd *shd;
/* 参数检查 */
if (argc != 3) {
fprintf(stderr, "./passwd USERNAME PASSWORD\n");
return -1;
}
/* 访问shadow文件,参数为所指定的用户名 */
shd = getspnam(argv[1]);
if (shd == NULL) {
printf("Error: %s\n", strerror(errno));
return 0;
}
strcpy(crypt_char, shd->sp_pwdp);
i = 0, j = 0;
/* 读到第三个'$'符号*/
while ((crypt_char[i] != '\0') && (j != 3))
{
salt[i] = crypt_char[i];
if (crypt_char[i] == '$') {
j++;
}
i++;
}
salt[i] = '\0';
printf("salt:\n%s\n", salt);
printf("crypt_pw:\n%s\n", shd->sp_pwdp);
printf("------------------count ----------------\n");
printf("salt:\n%s\n", salt);
printf("crypt_pw:\n%s\n", crypt(argv[2], salt));
return 0;
}
$ gcc passwd.c -o passwd -lcrypt
$ sudo ./passwd test shadow1234
salt:
$6$sT8RMKkg$
crypt_pw:
$6$sT8RMKkg$QLhvrR/iSwurIM0Z0.ZVkxPKRvKXueuAx7fZzq5Umffm3ydNbeGujV7mUKnTNYtbpLIbIdJ2.Q4Spw9OUjhl91
adduser abc (home目录里产生abc文件夹)
cat /etc/passwd :查看所有用户除root (compgen -u也可查用户)
whoami, id abc, userdel -r abc
root用户vi /etc/sudoers添加: abc ALL=(ALL:ALL) ALL ,wq!保存退出,abc用户sudo -i就可以了
ssh abc@10.75.135.29 ,输入123456 , linux会调用pam与/etc/shadow比较,默认采用6即SHA512加解密
// cat a.c
#include<stdio.h>
#include<pwd.h>
int main()
{
struct passwd *pw; //定义指针pw记录返回值
pw=getpwnam("root");
printf("%s %s %d %d %s %s %s\n",pw->pw_name,pw->pw_passwd,pw->pw_uid,pw->pw_gid,pw->pw_gecos,pw->pw_dir,pw->pw_shell);
return 0;
}
// gcc a.c
// ./a.out 与 /etc/passwd一致
root x 0 0 root /root /bin/bash
// cat b.c
#include<stdio.h>
#include<grp.h>
int main()
{
struct group *grp;
grp=getgrnam("root");
printf("%s %s %d\n",grp->gr_name,grp->gr_passwd,grp->gr_gid);
return 0;
}
// gcc b.c
// ./a.out 与 /etc/group一致
root x 0
// cat c.c
#include<stdio.h>
#include<shadow.h>
int main()
{
struct spwd *spw;
while((spw=getspent())!=NULL)
printf("%s %s\n",spw->sp_namp,spw->sp_pwdp);
endspent();
return 0;
}
// gcc c.c
// ./a.out 与 /etc/shadow一致
root $1$GpBw2JM2$wdjExHPJ0qkM9lej7mzzq/
bin *
daemon *
4.闭包和装饰器:函数里return就是闭包
如下打印第一行是:我在~。
5.解析eeprom:如下是二进制文件,C8是一个字节
如下5x4+4+2x4=32。
6.json/configparser/optparse:json.dumps(将字典转化为字符串,将json信息写进文件),json.loads(将字符串转化为字典)
#!/usr/bin/python3
# -*- coding: utf-8 -*-
import json
# dict1 = {"age": "12"} # <type 'dict'>
# json_info = json.dumps(dict1)
# print("通过json.dumps()函数处理:json_info的类型:"+str(type(json_info))) # <type 'str'>
# print(json_info) # {"age": "12"}
json_info = '{"age": "12"}' # <type 'str'>
dict1 = json.loads(json_info)
print("通过json.loads()函数处理:dict1的类型:"+str(type(dict1))) # <type 'dict'>
print(dict1) # {u'age': u'12'} # u:unicode中文显示
# json_info = "{'age': '12'}"
# file = open('1.json','w')
# json.dump(json_info,file) #生成1.json里面内容为 "{'age': '12'}"
win下.ini多。
# coding=UTF-8<code>
import os
import json
def d():
info_list = ['a','b','c','d','e','f','g','h','i']
split_list = [info_list[i:i+4] for i in range(0, len(info_list), 4)]
print(split_list) # [['a', 'b', 'c', 'd'], ['e', 'f', 'g', 'h'], ['i']]
x = set('runoob')
print(x) # set(['b', 'r', 'u', 'o', 'n'])
print(len(x)) # 5
i2clist={
'2':'port 1 SFP',
'3':'port 2 SFP',
'4':'port 3 SFP',
'5':'port 4 SFP'
}
def e():
for i in i2clist:
print(i) # 3 2 5 4
def search_dir_by_name(name, dir):
result = []
try:
files = os.listdir(dir)
for file in files:
if name in file:
result.append(os.path.join(dir, file))
except Exception as e:
pass
return result
dir = "/sys/block/"
spect = "sd"
ssdpath = []
# /sys/block/sd* remove=1 sda fdisk | grep sda 有/dev/sda , 没有的话只有sda, 拼接/dev/sda
if __name__ == '__main__':
result = search_dir_by_name(spect, dir)
#print(result) #['/sys/block/sdd', '/sys/block/sdb', '/sys/block/sde', '/sys/block/sdc', '/sys/block/sda']
for item in result:
with open(os.path.join(item, "removable"), 'r') as fd:
#print(item) # /sys/block/sde
#print(os.path.join(item, "removable")) # /sys/block/sde/removable
#print(fd.read()) #0或1 # SyntaxError: Non-ASCII character '\xe6' 加#coding=UTF-8<code>
value = fd.read()
if value.strip() == "0": # found ssd #去除1
ssd_disk = "/dev/" + os.path.basename(item) #ssd_disk:/dev/sda .../dev/sdd
ssdpath.append(ssd_disk)
7.map:[]列表 , ()元组 , {}集合 , {a:b}字典。for else中break后不走else
python中None,False,空字符串"",0,空列表[],空字典{},空元组()都相当于False。
cpu和switch连着,中间有一个nic卡(mellanox或e810),switch连着外面的光模块。fpga一端连着cpu,一端连着switch,fpga的eeprom存指令控制switch。
前面板前36个是loopback(光模块插DAC线或光纤,lookpack内部TX和RX接在一起),37-64网口是fpga和nic卡, 61和62->mellax 100G(100G无4个lan), e810 25G。
traffic:外部loocpack,cpu发包,cpu收包。
fpga 100g : cpu - fpga - switch - loopback - switch - fpga。
fpga 25g : fpga - switch。
layout只fpga,1个fpga有8个pcie设备(pf),1个pcie设备有16个通道。
如下25G有4个lan。
def stop_all(self, bdf_lst, bar, port, channel) 。如下self. 默认第一个参数有self。
8.walk/split/getattr/bin/lspci/ethtool/usb/serial:bin转为二进制0b…,re.compile
# a.py
# import json, os
# print(json.__file__) # /usr/lib/python3.8/json/__init__.py
# print(os.path.abspath(__file__)) # /home/y/utool/a.py
# print(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) # /home/y
sys.path.append(dirname(dirname(abspath(__file__)))) #当前文件所在绝对路径的文件夹的上级文件夹
Project_path = dirname(abspath(__file__)) #获取所在路径文件夹
# a.py
import binascii
def crc2hex(crc):
crc_bytes = bytes(str(crc), 'utf-8')
print("11",crc_bytes) # b'a'
print("22",binascii.crc32(crc_bytes)) # 3904355907
return '0x{:0>8X}'.format(binascii.crc32(crc_bytes)) # 计算CRC32校验值(摘要算法),并将其转换为8位的十六进制字符串
a=crc2hex("a")
print(a) # 0xE8B7BE43
# b.py
import os
import json
import sys
def seek_file(path, name):
fullpath = None
for root, dirs, files in os.walk(path):
if name in files:
fullpath = '{0}/{1}'.format(root, name)
break
return fullpath
if len(sys.argv)==3:
print(sys.argv[1],sys.argv[2])
path=seek_file(sys.argv[1],sys.argv[2])
print(path)
else:
print(len(sys.argv),'err')
root@bmc-oob:~# python b.py /sys/bus/i2c/devices/26-0048 in3_input
/sys/bus/i2c/devices/26-0048 in3_input
/sys/bus/i2c/devices/26-0048/hwmon/hwmon4/in3_input
def get_baseboard_sku():
try:
proc = subprocess.Popen([IPMITOOL_PATH, "raw", "0x3a", "0x10", "9", "0x1a", "1", "0x12"],
stdout=subprocess.PIPE,
shell=False,
stderr=subprocess.STDOUT) # proc返回20
stdout = proc.communicate()[0] # b' 20\n'
proc.wait()
sku_str = stdout.decode() # 20
sku_bin = bin(int("0x" + sku_str.strip(), 16))[-5:] # 0x20(十六进制 )= 32(十进制 ),bin(32) = 0b100000
sku_dict = {
"00001" : "ns35x",
"00010" : "ns34x",
"00100" : "ns33x",
"01000" : "ns32x",
"10000" : "ns31x"
}
sku = sku_dict.get(sku_bin, "")
except OSError as e:
raise OSError("Cannot detect platform")
return sku
output = remote.command("sol.sh", time=10, expect_list=['-----------------------------------'])
output1 = remote.command('\x15\x12\x14\x33', current=True, expect_list=expect_list)
output2 = remote.command('\r\n', current=True, expect_list=expect_list)
if "bmc-oob" in output1 or "bmc-oob" in output2:
emmc_info_cmd = "mmc cid read /sys/block/mmcblk0/device",
emmc_size_cmd = "cat /sys/block/mmcblk0/size"
def bmc_ping_cpu_test(self, also_print_console=False):
count = 5
ping_cmd = "ping %s -c %d | grep received" % (
self.bmc_login["ip"], count)
status, output = run_command(ping_cmd)
self.logger.log_info(output, also_print_console)
if output.find(" 0% packet loss") > 0:
ret = E.OK
else:
self.fail_reason.append("cpu ping server lost packages")
ret = E.EMGMT11002
return ret
bmc_ddr_name_cmd = "cat /proc/meminfo | awk '{print $1}'"
bmc_emmc_show_cmd = "fdisk -l | grep /dev/mmcblk0"
bmc_emmc_stress_test_cmd = "touch /var/log/emmc.test ; \
echo '==emmc read stress test==' ; \
(time dd if=/dev/mmcblk0 of=/dev/null bs=1M count=10 && sync) >> /var/log/emmc.log ; echo ' ' ; \
echo '==emmc write stress test==' ; \
(time dd if=/dev/zero of=/var/log/emmc.test bs=1M count=10 && sync) >> /var/log/emmc.log ; \
rm /var/log/emmc.test"
def str_to_hex(self, s): # 把字符串转化为对应的16进制串
return ' '.join([hex(ord(c)).replace('0x', '') for c in s])
def hex_to_str(self, s):
return ''.join([chr(i) for i in [int(b, 16) for b in s.split(' ')]])
# def str2bin(s): # bin二进制
# return ' '.join([bin(ord(c)).replace('0b', '') for c in s])
# def bin2str(s):
# return ''.join([chr(i) for i in [int(b, 2) for b in s.split(' ')]])
def replace_char(self, string, char, index):
string = list(string)
string[index] = char
return ''.join(string)
def modify_profile(self, also_print_console=True):
ret, Before_mac = self.get_mac_address_from_tlv_eeprom(also_print_console)
if ret != E.OK:
return ret
After_mac = Before_mac # 00E0EC000000
for i in range(11, -1, -1): # 从11到0 (竖着打印) , 12个(相当于索引)
last = After_mac[i] # 从右向左单个遍历
if last == "F":
After_mac = self.replace_char(After_mac, "0", i)
continue
else:
last = self.str_to_hex(last) # 字符0转为ascii码48,再转为十六进制0x30即30
last = int(last) + int(self.offset["offset"]["bmc_offset"])
last = str(last)
last = self.hex_to_str(last) # 函数里有split,所以last必须为str类型
After_mac = self.replace_char(After_mac, last, i) # After_mac = "EFFFFFFFFFFF" , 因为下行break,只打印出一行F00000000000
break
sign = 0
os.chdir(self.hwsku_path)
with open('fru.ini', 'r') as f:
lines = []
for line in f.readlines():
if line != '\n':
lines.append(line)
f.close()
with open('fru.ini', 'w') as f:
for line in lines:
if "bmc_base_mac_address=" in line:
if sign == 1:
f.write('%s' % line)
continue
line = "bmc_base_mac_address="+After_mac
f.write('%s\n' %line)
sign = 1
else:
f.write('%s' %line)
return After_mac
"gpio":{
"gpio504":{ # cpu的gpio504
"pin":"GPIO8", # cpld的gpio8
"net":"CTC7132_CPLD_JTAG_EN" # cpld的gpio8的备注作用:使能
},
"gpio505":{
"pin":"GPIO9",
"net":"CTC7132_CPLD_JTAG_TCK_R" # 时钟
},
"gpio506":{
"pin":"GPIO10",
"net":"CTC7132_CPLD_JTAG_TMS_R"
},
"gpio507":{
"pin":"GPIO11",
"net":"CTC7132_CPLD_JTAG_TDI_R" # 数据输入
},
"gpio508":{
"pin":"GPIO12",
"net":"CTC7132_CPLD_JTAG_TDO" # 数据输出
}
}
# root@localhost:~# lspci -nnn -s 07:00.0 (07:00.0是fpga的bus号,fpga是pcie设备)
# 07:00.0 Memory controller [0580]: Xilinx Corporation Device [10ee:7021]
# root@localhost:~# lspci -vvv -s 07:00.0 (devmem 0xfb800000 8)
# Region 0: Memory at fb800000 (32-bit, non-prefetchable) [size=4M]
"pcie":{
"X552":{ ######### device
"width":"x1",
"speed":"2.5GT/s",
"num":4
}
},
def _get_bus_dev_func(self, device): ######### device就是X552
bus_dev_func = []
ret, out = run_command("lspci | grep {}".format(device))
for line in out.splitlines():
if re.search("[0-9a-f]{2}\:[0-9a-f]{2}\.[0-9a-f]{1}", line):
bus_dev_func.append(re.findall(
"[0-9a-f]{2}\:[0-9a-f]{2}\.[0-9a-f]{1}", line)[0])
else:
bus_dev_func.append(None)
return bus_dev_func
def _get_device_conf(self, busid): ######### 遍历bus_dev_func中busid
ret, output = run_command(
"lspci -s {} -vvv | grep -i LnkSta | grep Speed".format(busid))
if ret or output == '':
return '', ''
else:
speed = output.strip(" \t\n").split(',')[0].split('Speed')[1].strip()
width = output.strip(" \t\n").split(',')[1].split('Width')[1].strip()
return speed, width
ethtool 网卡名 :查看网卡速率,全双工
ethtool -s ethX [speed 10|100|1000] [duplex half|full] [autoneg on|off]
设置网口速率 10/100/1000M 设置网口半/全双工 设置网口是否自协商
test_help_list = [
"Please check if psu status is correct",
"1.correct",
"2.wrong",]
for item in self.test_help_list:
self.logger.log_info(item, True)
choice = str(input("Please input 1 or 2: "))
value = re.compile(r'^[-+]?[0-9]+$')
result = value.match(choice)
if result:
if int(choice) == 1:
elif int(choice) == 2:
else:
def search_speed(self, speed):
time.sleep(5)
sta, out = run_command("ethtool eth0")
pattern = "\s+Speed: (.*)\s+"
result = re.findall(pattern, out)[0]
if result != speed:
return False
else:
return True
def rj45_led_test(self, also_print_console=False):
ret = E.EFAIL
self.logger.log_info("[RJ45 LED BLINK TEST]:", also_print_console)
speed_list = ["10", "100", "1000"]
for speed in speed_list:
cmd = "ethtool -s eth0 autoneg off speed {} duplex full"
if speed == "1000":
cmd = "ethtool -s eth0 autoneg on speed {} duplex full".format(
speed)
else:
cmd = "ethtool -s eth0 autoneg off speed {} duplex full".format(
speed)
run_command(cmd)
if not self.search_speed("{}Mb/s".format(speed)):
ret = E.EFAIL
self.fail_reason("set eth0 speed fail.")
self.logger.log_err("FAIL.", also_print_console)
return ret
self.logger.log_info(
"current eth0 speed is {}Mb/s, start ping test...".format(speed), also_print_console)
run_command("ping -c 20 -I eth0 {}".format(self.external_ip))
choice = input(
"please confirm rj45 led whether blink(1 is pass, 2 is fail):")
if int(choice) == 2:
ret = E.ELED14010
self.fail_reason.append("rj45 led fault")
self.logger.log_err("FAIL.", also_print_console)
return ret
elif int(choice) == 1:
continue
def search_dir_by_name(self, name, dir):
result = []
try:
files = os.listdir(dir)
for file in files:
if name in file:
result.append(os.path.join(dir, file))
except Exception as e:
self.logger.append(str(e))
return result
def get_usb_location(self):
dir = "/sys/block/"
spect = "sd"
usbpath = []
result = self.search_dir_by_name(spect, dir)
if len(result) <= 0:
self.fail_reason.append("no usb found")
return E.EUSB1000, usbpath
for item in result:
with open(os.path.join(item, "removable")) as fd:
value = fd.read()
if value.strip() == "1":
usbpath.append(item)
if not usbpath:
self.fail_reason.append("no usb found")
return E.EUSB1000, usbpath
return E.OK, usbpath
def get_usb_info(self, path): # path是get_usb_locationde得到
size_file = os.path.join(path, "size")
model_file = os.path.join(path, "device/model")
try:
with open(size_file) as f, open(model_file) as f1:
value = f.read()
value1 = f1.read()
if value.strip() == '0':
return E.EUSB1007,{}
return E.OK, {
"dev": os.path.basename(path), "size": round(
float(value) * 512 / 1024 / 1024 / 1024, 1), "model": value1}
except IOError as e:
return E.EIO, str(e)
def usb_stress(self, usb_dev, also_print_console=False):
ret = E.EFAIL
cmd = "fdisk -l |grep %s|grep 'Disk' -v|awk '{print $1}'" % usb_dev
fdisk_ret, fdisk_log = run_command(cmd)
usb_list = fdisk_log.splitlines()
if not usb_list:
usb_list.append("/dev/" + usb_dev)
for usb_disk in usb_list:
self.logger.log_info("[USB STRESS TEST]:", also_print_console)
ret, log = run_command("mount -v | grep /mnt/usb")
if log != '':
run_command("umount /mnt/usb/")
run_command("rm -rf /mnt/usb/")
mount_ret, mount_log = run_command("mkdir -p /mnt/usb && mount {} /mnt/usb/".format(usb_disk))
if mount_ret:
self.fail_reason.append("mount usb failed")
ret = E.EUSB1001
run_command("rm -rf /mnt/usb/")
return ret
else:
test_stress_cmd = "dd if={} of={}4G.txt bs=4k count=1048570".format(
usb_disk, "/mnt/usb/")
waiting = pit_util_common.waiting(self.logger,"usbstress testing...")
stress_ret, stress_log = run_command(test_stress_cmd)
waiting.stop("usbstress test stop!")
if stress_ret:
self.fail_reason.append("stress test failed!")
ret = E.EUSB1006
else:
self.logger.log_info("stress test success.", also_print_console)
ret = E.OK
run_command("rm -rf /mnt/usb/4G.txt")
run_command("umount /mnt/usb/")
run_command("rm -rf /mnt/usb/")
for key, value in self.monitor_log.items():
log = remote.command("ls {}".format(value))
size = remote.command("ls -al {} | awk '{{print $5}}'".format(value))
if "No such file or directory" not in log and int(size) > 0:
log_data = remote.command("log=$(tail -n 1 {} | cut -d ' ' -f 1); date -d $log +%s".format(value))
sys_data = remote.command("date +%s")
if int(sys_data) - int(log_data) >= (self.monitor_log_day *60 * 60 * 24) :
ret = E.EFAIL
self.fail_reason.append("{} monitor log abnormal, not update for more than {} days".format(key, self.monitor_log_day))
data_log = remote.command("date")
err_log = remote.command("tail -n 20 {}".format(value))
self.logger.log_err("bmc system data : {}\n{}:\n{}".format(data_log, value, err_log), also_print_console)
else:
self.logger.log_info("{} monitor log normal, update for less than {} days".format(key, self.monitor_log_day), also_print_console)
else:
ret = E.EFAIL
self.fail_reason.append("{} monitor log not exist or file is empty".format(key))
bmc_sys_data = remote.command("date +%s")
bmc_date_string = remote.command("hwclock | awk '{print $1, $2, $3, $4, $5}'")
bmc_date_obj = datetime.datetime.strptime(bmc_date_string.strip(), "%a %b %d %H:%M:%S %Y")
bmc_rtc_data = int(time.mktime(bmc_date_obj.timetuple()))
if abs(int(bmc_sys_data) - bmc_rtc_data) > 5:
ret = E.EFAIL
self.fail_reason.append("bmc sys time is {}s and bmc rtc time is is {}s, the difference is over 5 seconds.".format(bmc_sys_data, bmc_rtc_data))
elif abs(int(time.time()) - int(bmc_sys_data)) > 5:
ret = E.EFAIL
self.fail_reason.append("cpu sys time is {}s and bmc sys time is is {}s, the difference is over 5 seconds.".format(int(time.time()), bmc_sys_data))
else:
self.logger.log_info("bmc sys time ({}s) and bmc rtc time ({}s) and cpu sys time ({}s) is normal, the difference is less than 5 seconds.".format(bmc_sys_data, bmc_rtc_data , int(time.time())), also_print_console)
import serial
serial_rx_buffer_get_time = 0.5
serial_rx_buffer_wait_time = 0.1
class SerialClient():
def __init__(self, logger,cycle_time):
self.ser = None
logger = logger
self.cycle_time = cycle_time
# minicom -D /sys_switch/fpga/bridge4_3/carrier1_2/node4-3.1.2.1 (通过SDK配置出此节点,下面dev就是此节点路径,一般是/dev/ttyUSB0这样设备文件) -b 115200
def serial_login(self, dev, baud, timeout):
self.dev = dev
self.ser = serial.Serial(dev, baud, timeout=timeout)
self.ser.reset_input_buffer()
self.ser.reset_output_buffer()
self.ser.write("\r\n\r\n".encode("utf-8"))
time.sleep(0.5)
output = ''
init_output = ''
count = 15
while count > 0:
count -= 1
try:
buffer_size = self.ser.inWaiting()
if buffer_size > 0:
init_output = self.ser.read(buffer_size).decode("utf-8")
output += init_output
if "console" in output:
logger.info(f"{dev} init serial pass")
return True
except:
time.sleep(0.5)
continue
time.sleep(0.5)
else:
logger.info(f"{dev} init serial failed")
return False
# 登陆后su root ifconfig eth0 (下面command)
def execute_some_command_wait(self, command, timeout=1, check_res=None):
# self.ser.write(b'\x03') # ctrl + c
# time.sleep(0.2) # 11520 个字节发送
self.ser.flush()
cmd = "\r\n" + command + "\r\n"
self.ser.write(cmd.encode("utf-8"))
# self.ser.flush()
output_list = []
output = ''
time_count = 0
while self.ser.inWaiting() == 0:
if float(time_count) > float(timeout):
logger.error(f"[timeout] : cmd : {cmd}" ,True)
return None
time_count += serial_rx_buffer_wait_time
time.sleep(serial_rx_buffer_wait_time)
# while self.ser.inWaiting() > 0:
while float(time_count) <= float(timeout):
# logger.info(f"byte_size: {self.ser.inWaiting()}")
try:
output = self.ser.read_all().decode("utf-8")
# logger.info(f"[{self.dev} result]: {str(output).strip()} ")
output_list.append(output)
if re.findall(r"192\.168\.101\.\d+\s+Bcast", output) != []:
break
except:
time_count += serial_rx_buffer_get_time
time.sleep(serial_rx_buffer_get_time)
continue
time_count += serial_rx_buffer_get_time
time.sleep(serial_rx_buffer_get_time)
if output_list == [] and check_res:
logger.error(f"get {cmd} result is None" ,True)
return "".join(output_list).strip(cmd)