Linuxword Global
当前位置: VPS测评 > 用Telegram管理VPS

Telegram Bot是Telegram强大、好玩的重要原因之一,借助Bot的API,我们可以非常方便地编写机器人,帮助我们完成各种自动化操作。VPS就是Telegram Bot的应用平台之一,我们可以使用Shell、Python等脚本语言编写自动化机器人进行监测、爬虫等工作,并在需要的时候将消息推送给用户、甚至可以在聊天界面进行交互!下面是我在VPS上部署的5个Telegram机器人脚本。谁登陆了VPS?SSH登陆通知

telegram-sshd-monitor

telegram-sshd-monitor

当有人通过SSH登陆VPS时,自动给用户发送一条通知,包含登陆的IP、地址、时间等信息,以便快速识别未授权的访问。
将以下脚本保存在/etc/profile.d/目录,并添加可执行权限,注意修改用户ID和KEY,每次用户登陆时该脚本将被调用并发送一条Telegram通知。

 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#!/usr/bin/env bash
# This script is called on SSH login by /etc/profile.d/sshd_telegram.sh
# Modified from https://github.com/MyTheValentinus/ssh-login-alert-telegram
# Your USERID or Channel ID to display alert and key, we recommend you create new bot with @BotFather on Telegram
USERID=(chat ID here)
KEY="bot key here"
for i in "${USERID[@]}"
do
URL="https://api.telegram.org/${KEY}/sendMessage"
DATE="$(date "+%Y-%m-%d %H:%M:%S")"
if [ -n "$SSH_CLIENT" ]; then
	CLIENT_IP=$(echo $SSH_CLIENT | awk '{print $1}')
	SRV_HOSTNAME=$(hostname -f)
	#SRV_IP=$(hostname -I | awk '{print $1}')
	IPINFO="https://ipinfo.io/${CLIENT_IP}"
	IPQUERY=$(curl -s --connect-timeout 2 https://ipinfo.io/${CLIENT_IP})
	IPCITY=$(echo ${IPQUERY} | cut -d "," -f2 | cut -d "\"" -f4)
	IPREGION=$(echo ${IPQUERY} | cut -d "," -f3 | cut -d "\"" -f4)
	IPCOUNTRY=$(echo ${IPQUERY} | cut -d "," -f4 | cut -d "\"" -f4)
	IPORG=$(echo ${IPQUERY} |rev | cut -d "," -f1 | rev | cut -d "\"" -f4)
	TEXT="SSH Login(${SRV_HOSTNAME}):
	User: *${USER}*
	Date: ${DATE}
	Client: [${CLIENT_IP}](${IPINFO}) - ${IPCITY}, ${IPREGION}, ${IPCOUNTRY}, ${IPORG}"
	curl -s -d "chat_id=$i&text=${TEXT}&disable_web_page_preview=true&parse_mode=markdown" $URL > /dev/null
fi
done

我的VPS被墙了吗?被墙状态监测

telegram-gfw-monitor

telegram-gfw-monitor

前段时间很多境外VPS的IP都被禁了,有传言说隔几个月会自动解封,但到底多久或是永久都没人知道。最笨的办法就是每隔一段时间手动检测一下,这么无趣的工作就交给脚本来帮我们自动监测吧,让Telegram第一时间把好消息告诉我们。
原理很简单,如果VPS能访问国内网站就说明IP是通的,将下面脚本添加到crontab定时执行,比如2分钟检查一次*/2 * * * * /opt/scripts/gfw_monitor.sh。

 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#!/bin/bash
name=s2.shuyz.com
server=baidu.com
online=/tmp/online
offline=/tmp/offline
api_key=bot key here
chat_id=chat ID here
ping=$(ping -c 5 -W 1 ${server} | grep '100% packet loss')
if [ -z "$ping" ]; then
	rm -rf $offline
    # if never online then log and notify
    if [ ! -f $online ]; then
        touch $online 
		curl -s -X POST https://api.telegram.org/$api_key/sendMessage -d chat_id=$chat_id -d text="GFW Monitor - OK:%0A$name is not blocked by GFW, $server is accessible." > /dev/null
	fi
else
	rm -rf $online
	 if [ ! -f $offline ]; then
        touch $offline  
		curl -s -X POST https://api.telegram.org/$api_key/sendMessage -d chat_id=$chat_id -d text="GFW Monitor - BLOCKED:%0A$name is blocked by GFW, $server is not accessible." > /dev/null
	fi
fi
exit 0

带宽硬盘用了多少?VPS状态日报

telegram-vps-daily-report

telegram-vps-daily-report

想知道VPS的带宽、硬盘、内存、CPU的使用量,又不想麻烦登陆VPS去看?写个脚本让Telegram定时汇报吧。
因为带宽信息无法在VPS上本地获取,我们可以通过VPS的管理API拿到这些信息,我的VPS是SolusVM管理面板的,脚本如下:

 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#!/bin/bash
vm_key=SolusVM Key
vm_hash=SolusVM hash
vm_addr=https://manage.myvpssolusvm.com
api_key=bot key here
chat_id=chat ID here
# VPS status monitor to telegram
# Version
ver=$(lsb_release -d)
# Node
node=$(uname -n)
# Kernel
kern=$(uname -r)
# Bandwidth
bwarr=($(curl -s --connect-timeout 5 $vm_addr/api/client/command.php?key=$vm_key\&hash=$vm_hash\&action=info\&bw=true | grep -oP '\<bw\>(.)*\<\/bw\>' | cut -d ">" -f2 | cut -d "<" -f1 | tr "," "\n"))
# Uptime
upth=$(uptime |  cut -d',' -f1)
# Disk
dfh=($(df -h | grep xvda ))
# Memory
freeh=($(free -h | grep Mem ))
# Top Processes
toph=$(ps -eo pmem,pcpu,cmd | sort -k 1 -nr | head -5)
# All
report="VPS STATUS(${node}): \
%0A \
${ver} \
%0A Kernel: ${kern} \
%0A \
%0A UPTIME:  ${upth} \
%0A DISK:  Total: ${dfh[1]} Used:${dfh[2]} Avail:${dfh[3]} Used%:${dfh[4]} \
%0A BANDWIDTH:  Total: $(echo "scale=2; ${bwarr[0]}/1024/1024/1024/1024" | bc -l)TB Used: $(echo "scale=3; ${bwarr[1]}/1024/1024/1024" | bc -l)GB Avail: $(echo "scale=2; ${bwarr[2]}/1024/1024/1024/1024" | bc -l)TB  Used%: $(echo "scale=3; 100*${bwarr[1]}/${bwarr[0]}" | bc -l) % \
%0A MEMORY:  Total: ${freeh[1]} Used:${freeh[2]} Avail:${freeh[3]} \
%0A \
%0A ----------------------------- \
%0A TOP Processes(pmem,pcpu,cmd): \
%0A ${toph}"
curl -s -X POST https://api.telegram.org/$api_key/sendMessage -d chat_id=$chat_id -d text="$report" > /dev/null
exit 0

CertBot证书更新状态报告

telegram-certbot-report

telegram-certbot-report

为了安全,现在很多网站都上https了,我用的是免费的Let's Encrypt的证书,需要3个月更新一次。当然我不可能数着日子去手动更新,写个脚本定时执行就行了,并通过Telagram发送更新通知。
Let's Encrypt有个官方的CertBot脚本,添加个crontab任务在每周四的三点执行下面的脚本0 4 * * 3 /opt/scripts/su_certbot.sh:

 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#!/bin/bash
# Update let's encrypt
# 2019/6/5
api_key=bot key here
chat_id=chat ID here
result=$(/opt/certbot-auto renew --renew-hook "systemctl restart nginx & systemctl restart v2ray")
$result=$(python -c "import urllib, sys; print urllib.quote(sys.argv[1])" "$result")
curl -s -X POST https://api.telegram.org/$api_key/sendMessage -d chat_id=$chat_id -d \
	text="Let's Encrypt Certbot(s2.shuyz.com): \
	%0A \
	${result}"  > /dev/null
exit 0

把Telegram当网盘,VPS文件自动备份

telegram-baskup-data

telegram-baskup-data

官方宣称Telegram的云端存储空间是无限的,虽然我们不会真的把它当网盘存储大文件,但是备份服务器的一些小文件还是绰绰有余的,我把一些重要的配置文件定时保存在Telegram上,以防服务器出现硬件故障导致文件丢失。

 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#!/bin/bash
# Backup important files to telegram
# 2019/6/5
api_key=bot key here
chat_id=chat ID here
fList=$(tr '\n' ' ' < /opt/scripts/backuplist)
fName=/tmp/s2_backup_$(date +"%FT%H%M%S").tar.gz
tar -czvPf  $fName $fList
curl -F chat_id=$chat_id -F document=@"${fName}" https://api.telegram.org/$api_key/sendDocument  > /dev/null
rm -rf $fName
exit 0

上面的脚本配合添加一个备份列表文件/opt/scripts/backuplist,将需要备份的文件/文件夹逐行添加:

 
 
1
2
3
4
5
6
7
8
9
10
/etc/sysctl.conf
/etc/sysctl.d
/etc/sysconfig/iptables
/etc/sysconfig/ip6tables
/etc/systemd/system/*.service
/etc/ssh/sshd_config
/home/lance/.vimrc
/home/lance/.bashrc
/home/lance/.gitconfig
/home/lance/.ssh

用Telegram管理VPS:我的5个Telegram机器人脚本 https://www.shuyz.com/posts/5-telegram-bot-script-for-vps-management/ 作者Lance Liao发布于2019-07-03更新于2022-01-28许可协议CC BY-NC-ND 4.0

「梦想一旦被付诸行动,就会变得神圣,如果觉得我的文章对您有用,请帮助本站成长」

赞(0) 打赏
一分也是爱

支付宝扫一扫打赏

微信扫一扫打赏

标签:

上一篇:

下一篇:

相关推荐

博客简介

本站CDN采用VmShell免费提供离中国大陆最近的香港CMI高速网络做支撑,ToToTel打造全球最快速的边沿网络支撑服务,具体详情请见 :https://vmshell.com/ 以及 https://tototel.com/,网站所有的文件和内容禁止大陆网站搬迁复制,谢谢,VPS营销投稿邮箱: [email protected],我们免费帮大家发布,不收取任何费用,请提供完整测试文稿!

精彩评论

友情链接

他们同样是一群网虫,却不是每天泡在网上游走在淘宝和网游之间、刷着本来就快要透支的信用卡。他们或许没有踏出国门一步,但同学却不局限在一国一校,而是遍及全球!申请交换友链

站点统计

  • 文章总数: 2310 篇
  • 草稿数目: 12 篇
  • 分类数目: 6 个
  • 独立页面: 0 个
  • 评论总数: 2 条
  • 链接总数: 0 个
  • 标签总数: 5847 个
  • 注册用户: 139 人
  • 访问总量: 8,671,288 次
  • 最近更新: 2024年4月24日