意见箱
恒创运营部门将仔细参阅您的意见和建议,必要时将通过预留邮箱与您保持联络。感谢您的支持!
意见/建议
提交建议

GCC支持Host发起的时区同步

来源:恒创科技 编辑:恒创科技编辑部
2023-12-30 17:24:59
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/localtime4.相关程序

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


GCC支持Host发起的时区同步

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)
上一篇: Python入门系列(三)一学就会-基础数据类型 下一篇: Django4.0 RestFramework 序列器使用