# 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')
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路径错误。
# 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)
如下没有-H指定header,但是默认附带3个header。
index-win.exe测试程序主要监听88端口并能将请求内容
打印出来。
如下会自动多出content-两个请求头。
如下以json形式提交替代表单形式提交。
# auto-ip
# !/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin:/usr/local/bin
log="/var/log/auto-ip.log"
ip a > $log
date >> $log
sshpass -p 1 scp -o StrictHostKeyChecking=no $log [email protected]:/home_a/li/ip/s3ip-openbmc-bmc #(重命名)
sleep 10
# setup-auto-ip.sh
echo "Starting auto-ip daemon..."
runsv /etc/sv/auto-ip > /dev/null 2>&1 &
echo "Started auto-ip daemon"
# run-auto-ip.sh
exec /usr/local/bin/auto-ip
// auto-ip.bb
SUMMARY = "auto-ip"
DESCRIPTION = "auto-ip"
SECTION = "base"
PR = "r1"
LICENSE = "GPLv2"
LIC_FILES_CHKSUM = "file://COPYING;md5=94d55d512a9ba36caa9b7df079bae19f"
SRC_URI = "file://COPYING \
file://auto-ip \
file://run-auto-ip.sh \
file://setup-auto-ip.sh \
"
S = "${WORKDIR}"
binfiles = "auto-ip \
"
pkgdir = "auto-ip"
DEPENDS += "update-rc.d-native"
RDEPENDS_${PN} += "bash"
install_sysv() {
install -d ${D}${sysconfdir}/init.d
install -d ${D}${sysconfdir}/rcS.d
install -d ${D}${sysconfdir}/sv
install -d ${D}${sysconfdir}/sv/auto-ip
install -d ${D}${sysconfdir}/auto-ip
install -m 755 setup-auto-ip.sh ${D}${sysconfdir}/init.d/setup-auto-ip.sh
install -m 755 run-auto-ip.sh ${D}${sysconfdir}/sv/auto-ip/run
update-rc.d -r ${D} setup-auto-ip.sh start 91 5 .
}
do_install() {
dst="${D}/usr/local/fbpackages/${pkgdir}"
bin="${D}/usr/local/bin"
install -d $dst
install -d $bin
for f in ${binfiles}; do
install -m 755 $f ${dst}/$f
ln -snf ../fbpackages/${pkgdir}/$f ${bin}/$f
done
install_sysv
}
FBPACKAGEDIR = "${prefix}/local/fbpackages"
FILES_${PN} = "${FBPACKAGEDIR}/auto-ip ${prefix}/local/bin ${sysconfdir}"
# /mnt/data/etc/rc.local中
#!/bin/bash
/usr/local/bin/set_fan_speed.sh 30
# pi上 /etc/rc.local
_IP=$(hostname -I) || true
if [ "$_IP" ]; then
printf "My IP address is %s\n" "$_IP"
fi
sh /boot/autorun.sh &
if [ -x /var/log/rc.local ]; then # -x是否是可执行文件
/var/log/rc.local
fi
exit 0
# /boot/autorun.sh
sleep 30
ifconfig > /boot/autoboot.log
# /var/log/rc.local
/usr/bin/python3 /var/log/client.py 10.75.92.228 pi1 &
# client.py
#!/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
# if __name__ == "__main__":
# a = get_self_ip()
# print(a)
# python client.py 10.75.92.230
if len(sys.argv) == 3:
server = sys.argv[1] # 10.75.92.230
client = sys.argv[2] # 服务端中的json文件中记录着的客户端的ip地址
else:
print ("usage")
#11111111111111111111111111111111111111111111111111111111111111111111111111111111
while True:
url_body = {}
url_body["slave"] = client
url_body["ip"] = get_self_ip() # python client.py 10.75.92.230 a
# print(url_body) # {'ip': '10.75.92.230', 'slave': 'a'}
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.230", "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
# client.py放在需要得到ip的机器上,下行bmc是标识这机器。服务端和客户端都是230。
# root@diag-server:~# python3 client.py 10.75.92.230 bmc 必须先开启服务端且用python3
# {"result": "success", "data": {"slave": {"bmc": "10.75.92.230", "s3ip-openbmc-bmc": "10.75.92.109", "hollywood-bmc": "10.75.92.146",
# "server": "10.75.92.228", "s3ip-bsp-bmc": "10.75.92.96", "pi1": "10.75.92.53", "pi2": "10.75.92.152", "pi3": "10.75.92.51"}}}
# root@diag-server:~# curl -s http://10.75.92.230:8080/api/help | python -m json.tool
# {
# "data": {
# "slave": {
# "bmc": "10.75.92.230",
# "hollywood-bmc": "10.75.92.146",
# "pi1": "10.75.92.53",
# "pi2": "10.75.92.152",
# "pi3": "10.75.92.51",
# "s3ip-bsp-bmc": "10.75.92.96",
# "s3ip-openbmc-bmc": "10.75.92.109",
# "server": "10.75.92.228"
# }
# },
# "result": "success"
# }
# server.py
#!/usr/bin/python3
# -*- coding: utf-8 -*-
from aiohttp import web
import json
import requests
import json
import sys
PATH = "./usr_slave.json" #bmc镜像会刷到master或slave,这里获取的是slaver的bmc的ip
def parse_config_file():
with open(PATH,'r') as load_f:
load_dict = json.load(load_f)
return load_dict
# if __name__ == "__main__":
# a = parse_config_file()
# print(a)
# root@diag-server:# python server.py
# {u'slave': {u's3ip-openbmc-bmc': u'10.75.92.109', u'hollywood-bmc': u'10.75.92.146', u'server': u'10.75.92.228',
# u's3ip-bsp-bmc': u'10.75.92.96', u'pi1': u'10.75.92.53', u'pi2': u'10.75.92.152', u'pi3': u'10.75.92.51'}}
def req_post_json(str_url, json_data):
headers = {'Content-Type': 'application/json'}
response = requests.post(url=str_url, headers=headers, data=json.dumps(json_data))
return response.json()
# client.py 即 curl中 -d data = {'slave' : 'bmc', 'ip' : '10.75.85.34'}
def add_slave(data):
configs = parse_config_file()
if not configs:
configs = {}
configs['slave'] = {}
configs['slave'][data['slave']] = data['ip'] # configs['slave'][bmc]
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}
#1111111111111111111111111111111111111111111111111111111111111111111111111111111
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
// usr_slave.json,其中a是python3 client.py 10.75.92.230 a 多出
{
"slave": {
"bmc": "10.75.92.230",
"s3ip-openbmc-bmc": "10.75.92.109",
"hollywood-bmc": "10.75.92.146",
"server": "10.75.92.228",
"s3ip-bsp-bmc": "10.75.92.96",
"pi1": "10.75.92.53",
"pi2": "10.75.92.152",
"pi3": "10.75.92.51",
"a": "10.75.92.230",
}
}
# /mnt/data/etc/rc.local
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.228 s3ip-openbmc-bmc &
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
#include
#include
#include
#include
#include
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 [email protected] ,输入123456 , linux会调用pam与/etc/shadow比较,默认采用6即SHA512加解密
// cat a.c
#include
#include
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
#include
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
#include
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 *