起因
server ip 固定時間會換,無法使用dns。看能不能有方法獲得server 最新ip
思路
1. 印出ip
2. 串slack
3. 排程
最終成果
ip.sh
MSG=`ifconfig | grep -E 'inet.[0-9]' | grep -v '127.0.0.1' |awk '{print $2}'` CHANNEL="#serverip" USERNAME="Server" EMOJI=":ghost:" PAYLOAD="payload={\"channel\": \"$CHANNEL\", \"username\": \"$USERNAME\", \"text\": \"$MSG\", \"icon_emoji\": \"$EMOJI\"}" HOOK=https://hooks.slack.com/services/XXXXXX/BOOOOOO/n2345566 curl -X POST --data-urlencode "$PAYLOAD" "$HOOK"
建立排程檔
com.postip.plist
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"> <plist version="1.0"> <dict> <key>Label</key> <string>com.postip.plist</string> <key>ProgramArguments</key> <array> <string>/path/ip.sh</string> </array> <key>StartInterval</key> <integer>3</integer> </dict> </plist>放進/Library/LaunchDaemons
cp com.postip.plist /Library/LaunchDaemons執行指令
launchctl load -w /Library/LaunchDaemons/com.postip.plistDisable
launchctl unload -w /Library/LaunchDaemons/com.postip.plist rm /Library/LaunchDaemons/com.postip.plist
reference
https://gist.github.com/madcoda/9a57530edfbe32a9df4ce113312fa768
https://splinter.com.au/using-launchd-to-run-a-script-every-5-mins-on/
http://linux-wiki.cn/wiki/zh-tw/Shell%E4%B8%AD%E8%8E%B7%E5%8F%96%E5%BD%93%E5%89%8DIP%E5%9C%B0%E5%9D%80
留言