Browse Source

初始化仓库,上传初版文件

master
luzt98 12 months ago
commit
f34792c223
  1. 44
      pingctrl.sh
  2. 31
      readme.md
  3. 62
      remote-reboot.py

44
pingctrl.sh

@ -0,0 +1,44 @@
#!/bin/sh
CheckIPAddr()
{
echo ${1} | grep "^[0-9]\{1,3\}\.\([0-9]\{1,3\}\.\)\{2\}[0-9]\{1,3\}$" > /dev/null;
if [ $? -ne 0 ] ;then
return 1
fi
ipaddr=${1}
a=$( echo ${ipaddr} | awk -F . '{print $1}' )
b=$( echo ${ipaddr} | awk -F . '{print $2}' )
c=$( echo ${ipaddr} | awk -F . '{print $3}' )
d=$( echo ${ipaddr} | awk -F . '{print $4}' )
for num in ${a} ${b} ${c} ${d} ; do
if [ ${num} -gt 255 ] || [ ${num} -lt 0 ]; then
return 1
fi
done
return 0
}
main()
{
if [ $# -lt 4 ];then
echo "Usage: ./pingctrl.sh <ip> <count> <interval> <timeout>"
return 1
fi
CheckIPAddr ${1}
if [ $? -ne 0 ];then
echo 'the IP address is illegal'
return 1
fi
while :
do
ping $1 -c $2 -W $4 > /dev/null;
if [ $? -ne 0 ];then
echo "host are not reachable"
reboot
break;
fi
sleep $3
done
}
main $@

31
readme.md

@ -0,0 +1,31 @@
# TOOLS
保存一些杂项,系统调试脚本
## 1.pingctrl.sh
功能:循环Ping给定主机ip,若出现ping失败则立即重启系统。
用法: pingctrl.sh `<ip>` `<count>` `<interval>` `<timeout>`
参数说明:
ip: 要ping的远程主机ip
count: 每次要Ping的次数,只要有一次成功就视为ping成功
interval: 每次ping的时间间隔(单位:秒)
timeout: 超时时间(单位:秒)
## 2.remote-reboot.py
功能:使用telnet登录远程主机并发送一条reboot命令,连接成功返回0,失败返回-1
用法: python3 remote-reboot.py `<ip> <port> <username> <passwd>`
参数说明:
ip: 连接的远程主机ip 默认192.168.10.100
port: telnet连接端口号 默认 23
username: 登录用户名 默认 root
passwd: 密码 默认 root
脚本未对参数进行校验,传参时可以只传入ip和端口号,此时脚本会用默认的用户密码进行连接。但如果需要修改用户名密码,无论ip和端口号是否是默认值,都需要手动传入。

62
remote-reboot.py

@ -0,0 +1,62 @@
import telnetlib
import sys
HOST = "192.168.10.100"
PORT = 23
USERNAME = "root"
PASSWORD = "root"
def main(argv):
host = HOST
port = PORT
username = USERNAME
passwd = PASSWORD
if len(argv) > 2 :
host = argv[1]
port = argv[2]
if len(argv) > 4:
username = str(argv[3])
passwd = str(argv[4])
# 创建 Telnet 对象并连接远程主机
tn = telnetlib.Telnet(host, port)
print(tn.host)
# 匹配登录提示并发送用户名
tn.read_until(b"login: ")
tn.write(username.encode('ascii') + b"\r\n")
# 匹配密码提示并发送密码
tn.read_until(b"Password: ")
tn.write(passwd.encode('ascii') + b"\r\n")
# 匹配登录后的提示符
prompt = "#"
match_index, _, _ = tn.expect([prompt.encode('ascii')], timeout=5)
# 判断是否成功匹配
if match_index != -1:
print("登录成功!")
else:
print("登录失败!")
return -1
# 连接成功后,可以继续与远程主机进行交互
# 例如,发送命令并接收输出
tn.write(b"reboot\r\n")
try:
command_output = b""
while True:
line = tn.read_until(b"\n")
command_output += line
if b"expected_output" in line:
break
print(command_output.decode("utf-8"))
except Exception as e:
print(e)
# 关闭 Telnet 连接
tn.close()
return 0
if __name__ == "__main__":
main(sys.argv)
Loading…
Cancel
Save