通过宝塔 API,可以完全控制宝塔 Linux 面板的所有功能,包括第三方插件应用功能,事实上,在用户登录面板
后使用的所有功能也是通过相同的接口对接的,这意味着,如果你熟悉使用浏览器调试器,就可以轻松对照宝塔 Linux
面板的操作参数完成一个第三方的前端对接,类似开发一个宝塔管理APP软件,等等..

利用API可邮箱推送短信推送信息,等等AAP客户端管控,扩展性高

简单实现图Python3

BTAPI

功能实现

宝塔后台开启API功能,并添加IP白名单,自己电脑IP
2313191.png

发送如下http请求,返回信息json格式
post_btapi_http.PNG

1-签名参数

所需签名参数


获取当前时间戳,MD5计算=当前时间戳 + MD5后得API密匙 = request_token得值
bt_apivalue.PNG

Python3实现
计算MD5
get_md5(传入要计算值)

def get_md5(value):
    m = hashlib.md5()
    m.update(value.encode('utf-8'))
    return m.hexdigest();

2-构造http包

http包


http_post.PNG

python3

def calculation_key():
    now_time = int(time.time())
    autograph_data = {
    'request_token':get_md5(str(now_time)+get_md5(bt_api_key)),
    'request_time':now_time,
    'table':"logs"
    }
    data = urllib.parse.urlencode(autograph_data).encode('utf-8')
    return data;

3-发送http请求,并保存返回Cookie

完整脚本


最后完整脚本
脚本下载:

import urllib.request,urllib.error,json,time,hashlib,http.cookiejar,sys



bt_api_key = "kL7JSdgAMwwvHXXjV2zMD6rgwKeF8CzD"
bt_api_panel = "http://188.8.8.5:2580"
http_config = "/system?action=GetSystemTotal"


def get_md5(value):
    m = hashlib.md5()
    m.update(value.encode('utf-8'))
    return m.hexdigest();


def calculation_key():
    now_time = int(time.time())
    autograph_data = {
    'request_token':get_md5(str(now_time)+get_md5(bt_api_key)),
    'request_time':now_time,
    'table':"logs"
    }
    #http_data = bytes(str(autograph_data),encoding='utf-8')
    data = urllib.parse.urlencode(autograph_data).encode('utf-8')
    return data;

def post_http_run(url_info):
    file_cookie = './'+get_md5(bt_api_panel)+'.cookie';
    headers = {
    'User-Agent': "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:89.0) Gecko/20100101 Firefox/89.0",
    'Content-Type':"application/x-www-form-urlencoded"}
    get_http_info = urllib.request.Request(url=bt_api_panel+url_info,headers=headers,data=calculation_key())
    cookie_obj = http.cookiejar.MozillaCookieJar(file_cookie)
    try:
        cookie_obj.load(file_cookie,ignore_discard=True,ignore_expires=True)
    except FileNotFoundError as e:
        print("创建Cookie,请再次运行")
        cookie_obj.save()
        sys.exit()
    else:
        try:
            handler = urllib.request.HTTPCookieProcessor(cookie_obj)
            opener = urllib.request.build_opener(handler)
            response = opener.open(get_http_info,timeout=5)
        except socket.timeout as e:
            print("\u005b\u002d\u005d\t\u670d\u52a1\u5668\u8fde\u63a5\u5931\u8d25\u002e\u002e")
            sys.exit()
        except TimeoutError as  e:
            print("\u005b\u002d\u005d\t\u9762\u677f\u0055\u0052\u006c\u8fde\u63a5\u8d85\u65f6\uff0c\u65e0\u54cd\u5e94")
            sys.exit()
        except urllib.error.URLError as e:
            print("\u005b\u002d\u005d\t\u9762\u677f\u0055\u0052\u006c\u8fde\u63a5\u8d85\u65f6\uff0c\u65e0\u54cd\u5e94")
            sys.exit()
        else:
            cookie_obj.save(ignore_discard=True, ignore_expires=True)
            result = response.read()
            if type(result) == bytes: result = result.decode('utf-8')
            return result;

if __name__ == '__main__':
    system_info = json.loads(post_http_run("/system?action=GetSystemTotal"))
    system_server = json.loads(post_http_run("/config?action=get_config"))
    print("\t\t\t\t获取信息")
    print("系统版本:",system_info['system'],"\n运行时间:",system_info['time'],"\n系统内存:",system_info['memTotal'],"MB/内存已使用",system_info['memRealUsed'],"MB","\nCPU数量:",system_info['cpuNum'],"\nCPU使用率:",system_info['cpuRealUsed'],"%")
    print("WEB软件:",system_server['webserver'],"\tWEB目录:",system_server['sites_path'],"\t备份目录:",system_server['backup_path'],"\n数据库Root密码:",system_server['mysql_root'])

最后修改:2022 年 01 月 14 日
如果觉得我的文章对你有用,请随意赞赏