一、 会话对象
打开一个 CRT终端,就是一个 session 对象。session 对象拥有进行服务器的断开(Disconnect)和连接(Connect)操作的方法(函数),同时 session 对象的 Connected 属性可以判断当前会话是否已连接。
注:如果服务器掉电重启,由于笔者的服务器掉电重启的过程需要2到3分钟,因此重启的这段时间内必须使用 sleep 函数等待,之后才能进行服务器的连接(Connect),否则脚本将会报错。
二、示例:
下面的例子演示了,如何使用 ipmitool 命令让服务器掉电重启,并且 CRT 脚本进行服务器重新连接后继续执行脚本,的一个场景。
脚本文件名: test.js
# $language = "JScript"
var gLog_time = "";
var gScript_name = "";
var gRemoteHostip = "";
var gLog_file = "";
var timer_sleep_sec = 1000;
var timer_sleep_min = 60 * 1000;
var timer_sleep_hour= 60 * 60 * 1000;
var forReading = 1;
var forWriting = 2;
var forAppending = 8;
var UPGRADE = 2; //升级
var EXIT = 1;
var TRUE = 0;
var FALSE = -1;
var BUTTON_OK = 0; // OK button only
var BUTTON_CANCEL = 1; // OK and Cancel buttons
var BUTTON_ABORTRETRYIGNORE = 2; // Abort, Retry, and Ignore buttons
var BUTTON_YESNOCANCEL = 3; // Yes, No, and Cancel buttons
var BUTTON_YESNO = 4; // Yes and No buttons
var BUTTON_RETRYCANCEL = 5; // Retry and Cancel buttons
var ICON_STOP = 16; // Critical message; displays STOP icon.
var ICON_QUESTION = 32; // Warning query; displays '?' icon.
var ICON_WARN = 48; // Warning message; displays '!' icon.
var ICON_INFO = 64; // Information message; displays 'i' icon.
function get_secure_session()
{
crt.Screen.Send("\n");
crt.Sleep(300);
crt.Screen.Clear();
// 获取当前时间
crt.Screen.Send("sleep 1; echo `date +%Y-%m-%d_%H-%M-%S` | awk '{printf(\"%s\", $0)} END{print \"INFO_END\"}' \n");
crt.Sleep(300);
crt.Screen.Clear();
gLog_time = crt.Screen.ReadString("INFO_END", 3);
crt.Sleep(timer_sleep_sec);
crt.Screen.Send("\n");
//var ip = crt.Session.LocalAddress;
//crt.Screen.Send("# ip: "+ip+" \n");
gRemoteHostip = crt.Session.RemoteAddress;
crt.Screen.Send("# remoteip: "+gRemoteHostip+" \n");
// 获取当前脚本文件名
var name = script_name = crt.ScriptFullName.split("\\");
gScript_name = name.pop();
crt.Screen.Send("# ScriptName: "+gScript_name+" \n");
gLog_file = gRemoteHostip+"_"+gScript_name+"_"+gLog_time+".log"; //日志记录名字
return 0;
}
function process_log_open()
{
/*************************************************************************************************/
//Log(start[, append[, raw]])
crt.session.Log(false); //先关闭日志记录
crt.session.LogFileName = gLog_file;
crt.session.Log(true, true); //打开日志记录
//crt.Screen.Synchronous = true; //屏显同步 注:不要使用该设置,会影响 crt.Screen.Clear() 清屏函数的使用
crt.Screen.Send("# LogName: "+gLog_file+" \n");
return 0;
}
function power_cycle()
{
crt.Screen.Send("\n");
crt.Screen.Send("# Power Cycle process will take for 150 seconds! \n");
crt.Sleep(timer_sleep_sec);
crt.Screen.Send(" sleep 1;ipmitool chassis power cycle \n");
if (0 == crt.Screen.WaitforStrings("Chassis Power Control: Cycle", 2))
{
crt.Dialog.MessageBox("Power error: ipmitool command is invalid!!!", " 错误 ", ICON_STOP);
return -1;
}
crt.session.Disconnect();
crt.Sleep(150 * timer_sleep_sec);
while(true)
{
crt.session.Connect("/s " +gRemoteHostip);
crt.Sleep(timer_sleep_sec);
if (crt.Session.Connected)
{
process_log_open();
break;
}
}
crt.Sleep(timer_sleep_sec);
crt.Screen.Send("\n");
crt.Screen.Send("# Please quit this script,and execute test script! \n");
return 0;
}
function ftestOkResultlogo(){
crt.Sleep(timer_sleep_sec);
crt.Screen.Send("\n");
crt.Screen.Send("################ ######## #### ######\n");
crt.Screen.Send("################ ###### ###### #### ###### \n");
crt.Screen.Send(" #### #### #### #### ###### \n");
crt.Screen.Send(" #### #### ###### ###### ########## \n");
crt.Screen.Send(" #### #### ###### ###### ########## \n");
crt.Screen.Send(" #### ###### ###### ############ \n");
crt.Screen.Send(" #### ###### ###### #### ###### \n");
crt.Screen.Send(" #### #### #### #### ###### \n");
crt.Screen.Send(" #### #### ###### ###### #### ##### \n");
crt.Screen.Send(" #### #### ######## #### ######\n");
crt.Screen.Send("\n");
}
function main()
{
get_secure_session();
process_log_open();
ftestOkResultlogo();
power_cycle();
}
日志文件名:192.168.89.100_test.js_2022-05-09_20-55-13.log
[root@localhost ~]#
[root@localhost ~]# # remoteip: 192.168.89.100
# LogName: 192.168.89.100_test.js[root@localhost ~]# # ScriptName: test.js
[root@localhost ~]# # LogName: 192.168.89.100_test.js_2022-05-09_20-55-13.log
[root@localhost ~]#
[root@localhost ~]# ################ ######## #### ######
[root@localhost ~]# ################ ###### ###### #### ######
[root@localhost ~]# #### #### #### #### ######
[root@localhost ~]# #### #### ###### ###### ##########
[root@localhost ~]# #### #### ###### ###### ##########
[root@localhost ~]# #### ###### ###### ############
[root@localhost ~]# #### ###### ###### #### ######
[root@localhost ~]# #### #### #### #### ######
[root@localhost ~]# #### #### ###### ###### #### #####
[root@localhost ~]# #### #### ######## #### ######
[root@localhost ~]#
[root@localhost ~]#
[root@localhost ~]# # Power Cycle process will take for 150 seconds!
[root@localhost ~]# sleep 1;ipmitool chassis power cycle
Last login: Mon May 9 19:45:19 2022 from 192.168.79.64
ABRT has detected 2 problem(s). For more info run: abrt-cli list --since 1652096719
[root@localhost ~]# # LogName: 192.168.89.100_test.js_2022-05-09_20-55-13.log
[root@localhost ~]#
[root@localhost ~]# # Please quit this script,and execute test script!