关于python:GCC支持Host发起的时区同步

5次阅读

共计 3164 个字符,预计需要花费 8 分钟才能阅读完成。

1. 工作形容

 须要创立一个脚本,在 Windows 主机时区发生变化时,通过执行脚本,同步时区变动到 Linux 服务器上。

2. 设计思路

  • 应用 xml 文件配置服务器相干参数。
  • 应用 plink 将生成的管制命令推送到服务器上。

3. 原理

  • tzselect 命令无奈批改时区,仅给出时区的城市表示法
  • 默认状况下状况下,tz 属性是空,这时候是靠 /etc/localtime 文件来确定的时区。而此文件通常又是一个到 /usr/share/zoneinfo/ 下各种时区文件的软连贯。通过批改 /etc/localtime 指向的软连贯,进而批改零碎的时区。例如:
cp /us/share/zoneinfo/Etc/GMT-8 /etc/localtime
  • 须要 root 权限执行上述操作,故须要事后批改权限限度
    sudo chmod 777 /etc/localtime

4. 相干程序

1. /Program/CCBConfiguration.xml 是配置文件

  • LoginUser 登录 Linux 的用户名
  • Password 登录 Linux 的用户明码
  • HostName Linux 设施 IP 地址
  • 其余项暂未应用

    <Document>
    <LoginUser>***</LoginUser>
    <Password>***</Password>
    <HostName>*.*.*.*</HostName>
    <UploadPath>pass</UploadPath>
    <InstallParameter>pass</InstallParameter>
    <InstallLogPath>pass</InstallLogPath>
    </Document>

2. zone_change.py

from __future__ import division
import sys
import time
import os
import glob
import string
import xml.dom.minidom as minidom

REMOTE_TARGET_USER = ''REMOTE_TARGET_PWD =''
REMOTE_TARGET_HOST = ''REMOTE_TARGET_UPLOAD_PATH =''
REMOTE_TARGET_INSTALL_PARAMETER = ''INSTALL_LOG_PATH =''
zone_path = '/usr/share/zoneinfo/Etc/'


def get_attrvalue(node, attrname):
    return node.getAttribute(attrname) if node else ''


def get_nodevalue(node, index=0):
    return node.childNodes[index].nodeValue if node else ''


def get_xmlnode(node, name):
    return node.getElementsByTagName(name) if node else []


def xml_to_string(filename='user.xml'):
    doc = minidom.parse(filename)
    return doc.toxml('UTF-8')


# -------------------------------------------------------------------------------------------
def read_configxml(text):
    global REMOTE_TARGET_HOST
    global REMOTE_TARGET_UPLOAD_PATH
    global REMOTE_TARGET_USER
    global REMOTE_TARGET_PWD
    global INSTALL_LOG_PATH
    global REMOTE_TARGET_INSTALL_PARAMETER

    try:
        dom = minidom.parse(text)
        root = dom.documentElement
        node = get_xmlnode(root, 'LoginUser')
        REMOTE_TARGET_USER = str(get_nodevalue(node[0]).encode('utf-8', 'ignore'))
        node = get_xmlnode(root, 'Password')
        REMOTE_TARGET_PWD = str(get_nodevalue(node[0]).encode('utf-8', 'ignore'))
        node = get_xmlnode(root, 'HostName')
        REMOTE_TARGET_HOST = str(get_nodevalue(node[0]).encode('utf-8', 'ignore'))
        node = get_xmlnode(root, 'UploadPath')
        REMOTE_TARGET_UPLOAD_PATH = str(get_nodevalue(node[0]).encode('utf-8', 'ignore'))
        node = get_xmlnode(root, 'InstallParameter')
        REMOTE_TARGET_INSTALL_PARAMETER = str(get_nodevalue(node[0]).encode('utf-8', 'ignore'))
        node = get_xmlnode(root, 'InstallLogPath')
        INSTALL_LOG_PATH = str(get_nodevalue(node[0]).encode('utf-8', 'ignore'))
        print(REMOTE_TARGET_USER)

    except AttributeError as e:
        print("config file may lack necessary attribute, please check!\n", e)
        sys.exit(10)  # load install config file error


def change_time_zone(path):
    msg = "Start change time zone"
    print(path)
    #tmpStr = REMOTE_TARGET_USER + "@" + REMOTE_TARGET_HOST + "-pw" + REMOTE_TARGET_PWD
    tmpStr = REMOTE_TARGET_HOST[2:len(REMOTE_TARGET_HOST)-1] + '-l' + REMOTE_TARGET_USER[2:len(REMOTE_TARGET_USER)-1]\
             + '-pw' + REMOTE_TARGET_PWD[2:len(REMOTE_TARGET_PWD)-1]
    cmd = "plink.exe -ssh" + tmpStr + "cp" + path + "/etc/localtime"
    print(cmd)
    existStr = os.popen(cmd)


read_configxml('CCBConfiguration.xml')
offset_second = time.timezone
offset_hour = divmod(offset_second, 3600)
if offset_hour[1] == 0:
    offset = str(offset_hour[0])
else:
    offset = str(offset_second / 3600)
if offset_hour[0] < 0:
    linux_zone = 'GMT' + str(offset)
else:
    linux_zone = 'GMT+' + str(offset)
print(linux_zone)
zone_path = zone_path + linux_zone
print(zone_path)
change_time_zone(zone_path)
正文完
 0