#!/bin/bash
curl -s -XPOST -H 'Content-Type: application/json-rpc' -d "
{
\"jsonrpc\": \"2.0\",
\"method\": \"user.login\",
\"params\": {
\"user\": \"xxxxxx\",
\"password\": \"xxxxxx\"
},
\"id\": 1,
\"auth\": null
} " http://192.10.10.xxx/api_jsonrpc.php
注意:json格式数据需要把引号显示出来,所以在shell中双引号及其他特殊符号要加反斜杠'\'转义。另外在shell脚本中注释不能和json格式数据写在同一行,否则注释会包含在json数据中会报错无效的json格式。
例:
#!/bin/bash
#####获取用户验证令牌######
result=`curl -s -XPOST -H 'Content-Type: application/json-rpc' -d "
{
\"jsonrpc\": \"2.0\",
\"method\": \"user.login\",
\"params\": {
\"user\": \"xxxxx\",
\"password\": \"xxxxxx\"
},
\"id\": 1,
\"auth\": null
} " http://192.10.xxxxx/api_jsonrpc.php`
auth=`echo $result | awk -F, '{print $2}'| awk -F: '{print $2}'`
#### 创建主机Host_test1, 添加到主机组Linux_Biz3 id为17, 挂载Linux模板 id为10355, 创建测试主机宏####
curl -s -XPOST -H 'Content-Type: application/json-rpc' -d "
{
\"jsonrpc\": \"2.0\",
\"method\": \"host.create\",
\"params\": {
\"host\": \"Host_test1\",
\"interfaces\": [
{
\"type\": 1,
\"main\": 1,
\"useip\": 1,
\"ip\": \"192.10.xxxx\",
\"dns\": \"\",
\"port\": \"10050\"
}
],
\"groups\": [
{
\"groupid\": \"17\"
}
],
\"templates\": [
{
\"templateid\": \"10355\"
}
],
\"macros\": [
{
\"macro\": \"{\$TEST1}\",
\"value\": \"OK\"
},
{
\"macro\": \"{\$TEST2}\",
\"value\": \"yes\",
\"description\": \"It is a test.\"
}
]
},
\"auth\": ${auth},
\"id\": 2
}" http://192.10.xxxx/api_jsonrpc.php