Hi,大家好,我是编程小6,很荣幸遇见你,我把这些年在开发过程中遇到的问题或想法写出来,今天说一说js脚本自动化_xshell执行sh脚本,希望能够帮助你!!!。
前景提要,公司四十多台服务器,当每台服务器都需要升级更新的时候,一台一台的登录实在是不符合一名程序员的作风。于是乎就写的一个 js 自动化脚本,自动操作 xshell。
我刚开始使用的是不是正版的,crt
跑脚本没问题,xshell
跑脚本就老报错,一气之下就重新装了免费正版(学生版)的 xshell
,下载地址:www.netsarang.com/zh/all-down…
xshell
和 xftp
的下载地址 //固定格式,所有命令写在 Main() 方法中
function Main(){
// xsh就是 xshell 对象
xsh.Screen.Send("ls");
// 发送特殊字符要使用 ACSII 编码
xsh.Screen.Send(String.fromCharCode(13));
//延时,防止命令没执行完
xsh.Session.Sleep(500);
}
一些常用的操作 xshell 命令:
打开新会话: xsh.Session.Open(string);
对话框提醒: xsh.Dialog.MsgBox(string);
设置日志路径: xsh.Session.LogFilePath = string;
开始记录日志: xsh.Session.StartLog();
清屏函数: xsh.Screen.Clear();
等待输入: xsh.Screen.WaitForString(string);
输入回车: xsh.Screen.Send(String.fromCharCode(13));
延时,单位ms: xsh.Session.Sleep(500);
function Main(){
var pwds = ["123","456","789","101"];
for (var i = 0; i < 4; i++) {
var pwd = pwds[i];
xsh.Screen.Send("test");
xsh.Screen.Send(String.fromCharCode(13));
xsh.Session.Sleep(2000);
xsh.Screen.Send(pwd);
xsh.Screen.Send(String.fromCharCode(13));
xsh.Session.Sleep(1000);
//执行一些操作
xsh.Screen.Send("sudo -s");
xsh.Screen.Send(String.fromCharCode(13));
xsh.Session.Sleep(500);
xsh.Screen.Send(pwd);
xsh.Screen.Send(String.fromCharCode(13));
xsh.Session.Sleep(500);
xsh.Screen.Send("yum -y update nss nss-sysinit nss-tools openldap bind-export-libs bind-libs-lite bind-license");
xsh.Screen.Send(String.fromCharCode(13));
xsh.Session.Sleep(15000);
//退出
xsh.Screen.Send("exit");
xsh.Screen.Send(String.fromCharCode(13));
xsh.Session.Sleep(500);
xsh.Screen.Send("exit");
xsh.Screen.Send(String.fromCharCode(13));
xsh.Session.Sleep(500);
}
}
xshell 运行js脚本操作:点击 工具栏目 --> 脚本 --> 运行