共计 6170 个字符,预计需要花费 16 分钟才能阅读完成。
jumpserver api
domain+/api/docs
python
import requests, json
jms_url = 'http://jumpserver.xixxxshu.com'
token = 'exxxxxxxxxxxxxxxxxxxxxxxxx'
headers = {
"Authorization": 'Token' + token,
"Content-Type": "application/json",
"Accept": "application/json",
'X-JMS-ORG': '4xxxxxxxxxxxxxxxxxxxxxxxxxxxx'
}
# 获取所有用户
def get_user_all():
url = jms_url + '/api/v1/users/users/'
response = requests.get(url, headers=headers)
user_list = json.loads(response.text)
count = 1
# print(user_list)
for i in user_list:
count += 1
print(i)
print(count)
#
# # 获取监控指标
# def get_prometheus_metric():
# url = jms_url + "/api/v1/prometheus/metrics/"
# response = requests.get(url, headers=headers)
# print(response.text)
# return response.text
# 获取所有资产节点
def get_node_all():
url = jms_url + "/api/v1/assets/nodes/"
response = requests.get(url, headers=headers)
node_list = json.loads(response.text)
count = 1
for i in node_list:
count += 1
print(i)
print(count)
return response.json()
# 查看以后 token(即 admin)的所有资产
def get_asset_all():
url = jms_url + "/api/v1/assets/assets/"
response = requests.get(url, headers=headers)
node_list = json.loads(response.text)
count = 1
for i in node_list:
count += 1
print(i)
print(count)
return response.json()
###################################################################################################
# 创立资产节点
def assets_nodes_create(node_name):
node_data = {"value": node_name}
url = jms_url + "/api/v1/assets/nodes/"
node_info = get_node_info(node_name)
if node_info: # 依据 node_name 去查问,如果查到了阐明曾经有了。print("{name} 已存在, id: {id}".format(name=node_name, id=node_info[0]["id"]))
else:
data = json.dumps(node_data)
resp = requests.post(url, headers=headers, data=data)
return resp.json()
def get_assets_list_by_ip(ip):
url = jms_url + "/api/v1/assets/assets/"
response = requests.get(url, headers=headers, params={"ip": ip})
return response.json()
# 查看资产节点信息
def get_node_info(node_name):
url = jms_url + "/api/v1/assets/nodes/"
response = requests.get(url, headers=headers, params={"value": node_name})
print(response.text)
return response.json()
def asset_create(ip, hostname, node_id, comment):
asset_Data = {
"ip": ip,
"hostname": hostname,
"platform": "Linux",
"protocol": "ssh",
"port": 22,
"is_active": True,
"nodes": [node_id],
"org_id": "4xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
"org_name": "Ownerx 辰",
"comment": comment
}
url = jms_url + "/api/v1/assets/assets/"
data = json.dumps(asset_Data)
response = requests.post(url, headers=headers, data=data)
print(json.loads(response.text))
# 运行创立服务器资产
def run_create_assets(node_name, project_name, ip, comment):
# 节点 id,无节点时创立节点
node_info = get_node_info(node_name)
if len(node_info) == 0:
node_id = assets_nodes_create(node_name)
else:
node_id = node_info[0]["id"]
# 治理用户 id
hostname = "{ip}_{project_name}".format(ip=ip, project_name=project_name)
# 查 IP, 创立资产
ip_info = get_assets_list_by_ip(ip)
if ip_info:
print("%s 已存在,nodes: %s" % (ip_info[0]["ip"], ip_info[0]["nodes"]))
else:
asset_create(ip, hostname, node_id, comment)
#run_create_assets("Ownerx 辰", "xxxx", "10xx.xx.xx9", "测试")
def get_org_info():
url = jms_url + "/api/v1/orgs/orgs/"
response = requests.get(url, headers=headers)
org_list = response.text
print(org_list)
for i in org_list.split("id"):
print(i)
return response.json()
def get_user_gorup():
url = jms_url + "/api/v1/xpack/cloud/accounts/"
response = requests.get(url, headers=headers)
for i in response.text.split(","):
print(i)
# print(response.text)
return response.json()
GO
package main
import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"log"
"net/http"
)
const (
JmsServerURL = "http://juxxxxxxxxxxxxxxngshu.com"
JMSToken = "exxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxb"
ORG = "4xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx96" // ORG myself
)
type JumpServerCreateJson struct {Ip interface{} `json:"ip"`
Hostname interface{} `json:"hostname"`
Platform string `json:"platform"`
Protocol string `json:"protocol"`
Port int `json:"port"`
IsActive interface{} `json:"is_active"`
Nodes []interface{} `json:"nodes"`
OrgId string `json:"org_id"`
OrgName string `json:"org_name"`
Comment interface{} `json:"comment"`}
func GetOrgList(jmsurl, token string) {
url := jmsurl + "/api/v1/orgs/orgs/"
client := &http.Client{}
req, err := http.NewRequest("GET", url, nil)
req.Header.Add("Authorization", "Token"+token)
req.Header.Add("X-JMS-ORG", ORG)
resp, err := client.Do(req)
if err != nil {log.Fatal(err)
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {log.Fatal(err)
}
fmt.Println(string(body))
}
func GetNodeInfo(jmsUrl string, token string, nodeName string, org string) {
uri := jmsUrl + "/api/v1/assets/nodes/?value=" + nodeName
client := &http.Client{}
req, err := http.NewRequest("GET", uri, nil)
req.Header.Add("Authorization", "Token"+token)
req.Header.Add("X-JMS-ORG", org)
resp, err := client.Do(req)
if err != nil {log.Fatal(err)
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {log.Fatal(err)
}
fmt.Println(string(body))
}
func GetAssetsInfo(jmsUrl string, token string, ip string) {
uri := jmsUrl + "/api/v1/assets/assets/?ip=" + ip
client := &http.Client{}
req, err := http.NewRequest("GET", uri, nil)
req.Header.Add("Authorization", "Token"+token)
req.Header.Add("X-JMS-ORG", ORG)
resp, err := client.Do(req)
if err != nil {log.Fatal(err)
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {log.Fatal(err)
}
fmt.Println(string(body))
}
func CreateAsset(jmsUrl string, token string, ip string, homeName string, comment string, platform string, nodeId string, orgId string, orgName string) {
bodyJson := `{
"ip": "10.2x3.1x.2x1",
"hostname": "hostname",
"platform": "Linux",
"protocol": "ssh",
"port": 22,
"is_active": True,
"nodes": ["node_id"],
"org_id": "4xxxxxxxxxxxxxxxxxxxxxx6",
"org_name": "Ownxxxx",
"comment": "comment"
}`
var j JumpServerCreateJson
err := json.Unmarshal([]byte(bodyJson), &j)
if err != nil {fmt.Println("err is %v:", err)
}
j.Ip = ip
j.OrgId = orgId
j.Comment = comment
j.Hostname = homeName
j.OrgName = orgName
j.Nodes = []interface{}{nodeId}
if platform == "Linux" {fmt.Println("Linux ,body Wie ist")
} else {
j.Port = 3389
j.Protocol = "rdp"
j.Platform = "Windows"
fmt.Println("Windows ,body change")
}
data, err := json.Marshal(j)
if err != nil {fmt.Println("err was %v", err)
}
fmt.Println(string(data))
reader := bytes.NewReader(data)
uri := jmsUrl + "/api/v1/assets/assets/"
client := &http.Client{}
req, err := http.NewRequest("POST", uri, reader)
req.Header.Add("Authorization", "Token"+token)
req.Header.Add("X-JMS-ORG", orgId)
resp, err := client.Do(req)
if err != nil {log.Fatal(err)
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {log.Fatal(err)
}
fmt.Println(string(body))
}
func main() {//GetOrgList(JmsServerURL, JMSToken)
//GetNodeInfo(JmsServerURL, JMSToken, "Ownxxx", "1xxxxxxxxxxxxxxxxxxxxxxxxxxxxe")
//GetAssetsInfo(JmsServerURL, JMSToken, "10.2x.1x1.xx9")
}
正文完