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)
相关文章
暂无评论...