1.构建JS登录脚本

2020-04-01  本文已影响0人  90后社会你老叔

xshell 允许集成 js py vbs 脚本
转载请注明出处:https://blog.csdn.net/xuezoutianya/article/details/81605604

在编写xshell脚本的过程中用到最多的就是自动输入,自动捕获,延时等语句

自动输入
以自动输入xyz为例

自动输入的语句:xsh.Screen.Send("xyz");

当然,如果你输入的是一条命令,还需要下面这一行输入回车

输入回车的语句:xsh.Screen.Send(String.fromCharCode(13));

自动捕获
以linux系统为例,一般程序执行的打印数据位于倒数第二行,如下图所示

/* 字符串处理 */

var ScreenRow, ReadLine, Items;

/* 读取倒数第二行,长度为40个字符 */

ScreenRow = xsh.Screen.CurrentRow - 1;

ReadLine = xsh.Screen.Get(ScreenRow, 1, ScreenRow, 40);

延时
以等待1s为例

延时语句:xsh.Session.Sleep(1000);

其他
打开新会话:xsh.Session.Open(string);

对话框提醒:xsh.Dialog.MsgBox(string);

设置日志路径:xsh.Session.LogFilePath = string;

开始记录日志:xsh.Session.StartLog();

清屏函数:xsh.Screen.Clear();

等待输入:xsh.Screen.WaitForString(string);

示例
本文以一个自动测试脚本为例,定时向/tmp/test文件写入数据,然后回读打印,截获回读打印的值进行分析

/* 测试函数 /
function test()
{
/
发送echo 112233 > /tmp/testfile */
xsh.Screen.Send("echo 112233 > /tmp/testfile");
xsh.Screen.Send(String.fromCharCode(13));

/* 发送cat /tmp/testfile */
xsh.Screen.Send("cat /tmp/testfile");
xsh.Screen.Send(String.fromCharCode(13));

/* 字符串处理 */
var ScreenRow, ReadLine, Items;

/* 读取末行的40个字符 */
ScreenRow = xsh.Screen.CurrentRow - 1;
ReadLine = xsh.Screen.Get(ScreenRow, 1, ScreenRow, 40);
/* 如果读取到的字符不是112233 */
if(ReadLine != "112233")
{
    /* 会话框打印实际的字符串 */
    xsh.Dialog.MsgBox(ReadLine);
}

}

/* 主函数 /
function Main()
{
/
打开会话,根据实际的会话路径修改 */
xsh.Session.Open("C:\Users\Administrator\Documents\NetSarang Computer\6\Xshell\Sessions\ubuntu.xsh");
xsh.Screen.Synchronous = true;

/* 开始记录日志 */
xsh.Session.LogFilePath = "C:\Users\Administrator\Documents\NetSarang Computer\6\Xshell\Logs\example.log";
xsh.Session.StartLog();

/* 等待输入start */

// xsh.Screen.WaitForString("start");

/* 发送rm -rf /tmp/testfile */
xsh.Screen.Send("rm -rf /tmp/testfile");
/* 发送回车 */
xsh.Screen.Send(String.fromCharCode(13));

/* 发送touch /tmp/testfile */
xsh.Screen.Send("touch /tmp/testfile");
xsh.Screen.Send(String.fromCharCode(13));

/* 测试100次 */
for(var i = 1; i < 100; i++)
{
    test();
    xsh.Session.Sleep(500);
}

/* 清屏 */

// xsh.Screen.Clear();
}
运行脚本的操作:

实际执行结果如下:
转载请注明出处:https://blog.csdn.net/xuezoutianya/article/details/81605604

在编写xshell脚本的过程中用到最多的就是自动输入,自动捕获,延时等语句

自动输入
以自动输入xyz为例

自动输入的语句:xsh.Screen.Send("xyz");

当然,如果你输入的是一条命令,还需要下面这一行输入回车

输入回车的语句:xsh.Screen.Send(String.fromCharCode(13));

自动捕获
以linux系统为例,一般程序执行的打印数据位于倒数第二行,如下图所示

/* 字符串处理 */

var ScreenRow, ReadLine, Items;

/* 读取倒数第二行,长度为40个字符 */

ScreenRow = xsh.Screen.CurrentRow - 1;

ReadLine = xsh.Screen.Get(ScreenRow, 1, ScreenRow, 40);

延时
以等待1s为例

延时语句:xsh.Session.Sleep(1000);

其他
打开新会话:xsh.Session.Open(string);

对话框提醒:xsh.Dialog.MsgBox(string);

设置日志路径:xsh.Session.LogFilePath = string;

开始记录日志:xsh.Session.StartLog();

清屏函数:xsh.Screen.Clear();

等待输入:xsh.Screen.WaitForString(string);

示例
本文以一个自动测试脚本为例,定时向/tmp/test文件写入数据,然后回读打印,截获回读打印的值进行分析

/* 测试函数 /
function test()
{
/
发送echo 112233 > /tmp/testfile */
xsh.Screen.Send("echo 112233 > /tmp/testfile");
xsh.Screen.Send(String.fromCharCode(13));

/* 发送cat /tmp/testfile */
xsh.Screen.Send("cat /tmp/testfile");
xsh.Screen.Send(String.fromCharCode(13));

/* 字符串处理 */
var ScreenRow, ReadLine, Items;

/* 读取末行的40个字符 */
ScreenRow = xsh.Screen.CurrentRow - 1;
ReadLine = xsh.Screen.Get(ScreenRow, 1, ScreenRow, 40);
/* 如果读取到的字符不是112233 */
if(ReadLine != "112233")
{
    /* 会话框打印实际的字符串 */
    xsh.Dialog.MsgBox(ReadLine);
}

}

/* 主函数 /
function Main()
{
/
打开会话,根据实际的会话路径修改 */
xsh.Session.Open("C:\Users\Administrator\Documents\NetSarang Computer\6\Xshell\Sessions\ubuntu.xsh");
xsh.Screen.Synchronous = true;

/* 开始记录日志 */
xsh.Session.LogFilePath = "C:\Users\Administrator\Documents\NetSarang Computer\6\Xshell\Logs\example.log";
xsh.Session.StartLog();

/* 等待输入start */

// xsh.Screen.WaitForString("start");

/* 发送rm -rf /tmp/testfile */
xsh.Screen.Send("rm -rf /tmp/testfile");
/* 发送回车 */
xsh.Screen.Send(String.fromCharCode(13));

/* 发送touch /tmp/testfile */
xsh.Screen.Send("touch /tmp/testfile");
xsh.Screen.Send(String.fromCharCode(13));

/* 测试100次 */
for(var i = 1; i < 100; i++)
{
    test();
    xsh.Session.Sleep(500);
}

/* 清屏 */

// xsh.Screen.Clear();
}
运行脚本的操作:

实际执行结果如下:

image.png
上一篇 下一篇

猜你喜欢

热点阅读