(http://2012.aliyun.com/works/finalist 阿里云开发者大赛43号作品)
(一)需求描述
1. 远程Windows服务器[10.34.130.62]的配置文件"C:\Program Files\ECSTOOL\Scripts\config.txt"内容为:
[sample]
path1 = C:\Program Files\ECSTOOL\Scripts
path2 = D:\Program Files\ECSTOOL\Scripts
path3 = C:\Program Files\ECSTOOL\Scripts
path4 = D:\Program Files\ECSTOOL\Scripts
......
需要备份原来文件,并更改配置为:
[sample]
path1 = C:\Program Files\ECSTOOL\Scripts
path2 = D:\ETC
pathX = C:\Program Files\ECSTOOL\Scripts
pathY = D:\Program Files\ECSTOOL\Scripts
......
2. 远程Linux服务器[10.34.130.44]的配置文件"/home/config.txt"有同样的需求。
(二)实现流程(其中一种)
【Windows】:
1. 10.34.130.62执行dos命令复制"C:\Program Files\ECSTOOL\Scripts\config.txt"为"C:\Program Files\ECSTOOL\Scripts\config.txt.bak"
2. 10.34.130.62替换config.txt配置字段"path3 ="为"pathX =",替换配置字段"path4 ="为"pathY ="
3. 10.34.130.62修改config.txt配置字段"path2 ="右边值为"D:\ETC"
【Linux】:
1. 本机从10.34.130.44下载文件"/home/config.txt"到"D:\"
2. 本机替换config.txt配置字段"path3 ="为"pathX =",替换配置字段"path4 ="为"pathY ="
3. 本机修改config.txt配置字段"path2 ="右边值为"D:\ETC"
4. 10.34.130.44重命名"/home/config.txt"为"/home/config.txt.bak"
5. 本机上传文件"D:\config.txt"到 10.34.130.44"/home"目录下
* 其他方法扩展,上传修改脚本+调用执行脚本,dos批量和shell批量命令修改,……
(三)Lua脚本实现(基于http://blog.csdn.net/w565911788/article/details/8127968)
x = 0
--脚本入口和出口函数EnterBegin()
function EnterBegin()
--连接到服务器 10.34.130.62和10.34.130.44
ExecCommand("127.0.0.1:connect_one_server{@10.34.130.62@}{@10.34.130.44@}")
sleep(1000)
ExecCommand("127.0.0.1:get_connect_servers{@all@}")
----------------------Linux------------------------------
--1. 从 10.34.130.44下载文件"/home/config.txt"到"D:\"
ExecCommand("127.0.0.1: download_linux_file{@10.34.130.44|/home/config.txt|D:\\@}")
--等待完成下载
sleep(1000)
--ExecCommand("127.0.0.1: check_windows_file{@D:\config.txt|1|10@}")
--2. 替换config.txt配置字段"path3 ="为"pathX =",替换配置字段"path4 ="为"pathY ="
ExecCommand("127.0.0.1:replace_text{@D:\\config.txt|path3 =|pathX =@}{@D:\\config.txt|path4 =|pathY =@}")
sleep(1000)
--3. 修改config.txt配置字段"path2 ="右边值为"D:\ETC"
ExecCommand("127.0.0.1:replace_right{@D:\\config.txt|path2 =|D:\\ETC@}")
sleep(1000)
--4. 重命名"/home/config.txt"为"/home/config.txt.bak"
ExecCommand("10.34.130.44:linux_shell{@mv /home/config.txt /home/config.txt.bak@}")
--5. 上传文件"D:\config.txt"到 10.34.130.44"/home"目录下
ExecCommand("127.0.0.1: send_linux_file{@D:\\config.txt>10.34.130.44;/home?@}")
x = sleep(1000)
-----------------------------windows-----------------------
--1. 10.34.130.62执行dos命令复制"C:\Program Files\ECSTOOL\Scripts\config.txt"
--为"C:\Program Files\ECSTOOL\Scripts\config.txt.bak"
ExecCommand("10.34.130.62:win_cmd{@copy \"C:\\Program Files\\ECSTOOL\\Scripts\\config.txt\" \"C:\\Program Files\\ECSTOOL\\Scripts\\config.txt.bak\"@}")
--2. 10.34.130.62替换config.txt配置字段"path3 ="为"pathX =",替换配置字段"path4 ="为"pathY ="
ExecCommand("10.34.130.62:replace_text{@C:\\Program Files\\ECSTOOL\\Scripts\\config.txt|path3 =|pathX =@}{@C:\\Program Files\\ECSTOOL\\Scripts\\config.txt|path4 =|pathY =@}")
--3. 10.34.130.62修改config.txt配置字段"path2 ="右边值为"D:\ETC"
ExecCommand("10.34.130.62:replace_right{@C:\\Program Files\\ECSTOOL\\Scripts\\config.txt|path2 =|D:\\ETC@}")
return x
end
function sleep (a)
local b = 1000000*a
for i=0,b,1 do
a = i
end
return a
end
return x
(四)执行结果截图
【Windows-10.34.130.62】
【Linux-10.34.130.44】
【Localhost-D盘】
【Localhost-OUTPUT】