redis 为桥 ,重启家里的centos

使用 supervisor 延迟10秒左右启动下面。用于根据远程 redis 上的key 来重启家里没法被外网访问到的机器。
(配合 ssh 转发 使用)

# -*- coding:utf-8 -*-

import redis
import sys
import os
import time

class Client:
    def __init__(self, host=None, port=None, password=None):
        self.host = host
        self.port = port
        self.password = password
        self.cache = {}

    def db(self, id):
        if isinstance(id, int) == False:
            return None


        ret = redis.Redis(host=self.host, port=self.port, db=id, password=self.password)
        self.cache[id] = ret
        return ret


args = sys.argv

host = None
port = 6379
pw = None
for arg in args:
    if arg.find("-h:") == 0:
        host = arg.split(":")[1]
    if arg.find("-p:") == 0 :
        port = int(arg.split(":")[1])
    if arg.find("-pw:")==0:
        pw = arg.split(":")[1]

client = Client(host=host,port=port,password = pw)
db = client.db(0)

hname = "reboot_cmd"
hkey = "home_centos"


def reboot():
    db.hset(hname,hkey,0)
    os.system("ifconfig")

while True:
    ret = db.hget(hname,hkey)
    if ret is not None and int(str(ret)) == 1:
        reboot()
    print("nothing to do")
    time.sleep(10)
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容