一、 会话对象
打开一个 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;
var BUTTON_CANCEL = 1;
var BUTTON_ABORTRETRYIGNORE = 2;
var BUTTON_YESNOCANCEL = 3;
var BUTTON_YESNO = 4;
var BUTTON_RETRYCANCEL = 5;
var ICON_STOP = 16;
var ICON_QUESTION = 32;
var ICON_WARN = 48;
var ICON_INFO = 64;
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");
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()
{
crt.session.Log(false);
crt.session.LogFileName = gLog_file;
crt.session.Log(true, true);
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 ~]
[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 ~]
[root@localhost ~]
[root@localhost ~]
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 ~]
[root@localhost ~]
[root@localhost ~]
|