xshell具有脚本功能,可以实现很多自动化的操作。

从xshell的官方手册,可以知道xshell的脚本分为3类:

xsh.Session

下面的函数或变量在xshell会话中使用,使用的时候要指定会话一起使用,比如使用Sleep()函数,要这样使用:xsh.Session.Sleep(1000)

函数

返回值 函数 参数 说明
Void Open(LPCTSTR lpszSession) lpszSession:字符串,指Xshell会话路径或Xshell使用的URL类型。 打开新会话或URL。需要把 /s选项置于字符串的前端。
例如要打开A.xsh会话使用‘/s $PATH/A.xsh’
Void Close() 关闭当前连接的会话。
Void Sleep(long timeout) Timeout:毫秒单位的时间值 Xshell按照指定时间进行待机。
Void LogFilePath(LPCTSTR lpszNewFilePath) lpszNewFilePath:包括路径在内的文件名 指定日志文件。
void StartLog() 开始会话的日志记录。日志将被保存到LogFilePath()指定的路径。如果没有指定日志文件路径则使用默认路径。
void StopLog() 停止日志记录。

变量

名称 类型 说明
Connected BOOL 检查当前会话是否连接。
LocalAddress BSTR 导入本地地址。
Path BSTR 导入当前会话文件的路径。
RemoteAddress BSTR 导入远程地址。
RemotePort long 导入远程端口号。
Logging BOOL 检查当前会话是否记录日志。
LogFilePath BSTR​​ 保存为日志文件。

xsh.Screen

下面的函数和变量在处理xshell终端屏幕的时候使用,使用的时候要配合xsh.Screen一起使用,比如要使用Clear()函数,要这样使用:xsh.Screen.Clear

函数

返回值 函数 参数 说明
void Clear() 清除终端屏幕。
void Send(LPCTSTR lpszStrToSend) lpszStrToSend:用户拟要发送的字符串 向终端发送消息。
BSTR Get(long nBegRow, long nBegCol, long nEndRow, long nEndCol) nBegRow:终端的行起始位置
nBegCol:终端的列起始位置
nEndRow:终端的行末端位置
nEndCol:终端的列末端位置
读入终端规定区域的字符串并返回读取值。
void WaitForString(LPCTSTR lpszString) lpszString:终端中打印的字符串 等待终端打印lpszString字符串。
Long WaitForStrings(VARIANT FAR* strArray, long nTimeout) strArray:终端中打印的字符串
nTimeout:等候时间
返回值:发现的字符数
等待某消息直到超时。

变量

名称 类型 说明
CurrentColumn long 返回当前列数。
CurrentRow long 返回当前行数。
Columns long 返回与终端的列宽相同的列数。
Rows long 返回与终端的行高相同的行数。
Synchronous BOOL 设置屏幕同步 (True:屏幕同步,false:屏幕不同步)​

xsh.Dialog

使用的时候要配合xsh.Dialog一起使用,比如要使用MsgBox()函数,要这样使用:xsh.Dialog.MsgBox()

函数

返回值 函数 参数 说明
Long MsgBox(LPCTSTR lpszMsg) LpszMsg:想要发送的字符串 打开一个消息框
string Prompt(LPCTSTR lpszMessage, LPCTSTR lpszTitle, LPCTSTR lpszDefault, BOOL bHidden) lpszMessage:在对话框上显示的字符串。
lpszTitle:在对话框标题栏显示的字符串。
lpszDefault:在对话框输入框中初始显示的字符串。
bHidden:如果设置为True,输入会被隐藏 (e.g. *****)
作用:返回用户在对话框中的输入。
返回值:用户在对话框中的输入。
int MessageBox(LPCTSTR lpszMessage, LPCTSTR lpszTitle, int nType) lpszMessage:在消息框中显示的字符串。
lpszTitle:在消息框标题栏显示的字符串。
nType:按钮类型,参考下面的表。
作用:按照用户选择的按钮类型显示消息框并返回相应的值。
返回值:参考下面的表。

按钮类型:

类型 Button 返回值
0 OK 1
1 OK / Cancel 1 / 2
2 Abort / Retry / Ignore 3 / 4 / 5
3 Yes / No / Cancel 6 / 7 / 2
4 Yes / No 6 / 7
5 Retry / Cancel 4 / 2
6 Cancel / TryAgain / Continue 2 / 10 / 11

实例

Sub MainDim hostname, username, passwordhostname = xsh.Dialog.Prompt ("Insert Hostname", "Prompt Dialog", "hostname", 0)username = xsh.Dialog.Prompt ("Username", "Prompt Dialog", "", 0)password = xsh.Dialog.Prompt ("Password", "Prompt Dialog", "", 1)if xsh.Dialog.MessageBox("Connect to " & hostname & " server", "MessageBox",1) = 1 thenxsh.Session.Open("ssh://" & username & ":" & password & "@" & hostname)End If
End Sub

注:以下实例来自网上,仅供自己编程参考,不保证直接能用。

实例1

Sub main  xsh.Screen.Synchronous = True    ' 使窗口显示与当前输出同步  xsh.Screen.WaitForString "Reboot now? (y/n)"    ' 等待目标设备发回 "Reboot now? (y/n)" 字符串  xsh.Screen.Send "y" & VbCr    ' 输入字符 y 并回车
End Sub

实例2

Sub Main' *** Connect the session ***
xsh.Session.Open "ssh://192.168.1.17"
' "/s C:\Users\Administor\AppData\Roaming\NetSarang\Xshell\Sessions\example.xsh"xsh.Screen.Synchronous = truexsh.Screen.WaitForString "login: "
xsh.Screen.Send "username"
xsh.Screen.Send VbCr
xsh.Session.Sleep 100xsh.Screen.WaitForString "Password: "
xsh.Screen.Send "password"
xsh.Screen.Send VbCr
xsh.Session.Sleep 100' *** Wait for Prompt Message ***
xsh.Screen.WaitForString "username@password"' *** Set File Format ***
Dim app, wb, ws
Set app= CreateObject("Excel.Application")
Set wb = app.Workbooks.Add
set ws = wb.Worksheets(1)xsh.Session.LogFilePath = "c:\example.log"
xsh.Session.StartLogDim waitStrs
waitStrs = Array(Chr(10), "username@password") ' make wait message as arrayDim row, screenrow, readline, itmes
row = 1' *** Send Command ***
xsh.Screen.Send "cat /etc/passwd"
xsh.Screen.Send VbCr
xsh.Session.Sleep 100Dim result' *** Read Data and Save it as an EXCEL File ***
Do
While true
result = xsh.Screen.WaitForStrings(waitStrs, 1000)If result = 2 Then
Exit Do
End Ifscreenrow = xsh.Screen.CurrentRow - 1
readline = xsh.Screen.Get(screenrow, 1, screenrow, 40)
items= Split(readline, ":", -1)ws.Cells(row,1).Value = items(0)
ws.Cells(row,2).Value = items(2)row = row + 1
Wend
Loopwb.SaveAs("C:\chart.xls") ' save file path
wb.Close
app.QuitSet ws = nothing
Set wb = nothing
Set app = nothingxsh.Screen.Synchronous = falsexsh.Session.StopLogEnd Sub

实例3

Xshell打开多个session(会话)

Sub MainDim username, password
username = "用户名"
password = "密码"
Dim Hosts(主机名的个数减去1)
hosts(0) = "主机名1"
hosts(1) = " 主机名2 "
hosts(n-1) = " 主机名n"
For Each HostStr In Hosts
xsh.Session.Open ("ssh://" & username & ":" & password & "@" & HostStr)
xsh.Session.Sleep(50)
Next
End Sub

实例4

# import xsh.Session
# import xsh.Screen
# import xsh.Dialogdef get_current_row_info(num: int):"""获取终端当前行num个字符"""screenRow = xsh.Screen.CurrentRowline = xsh.Screen.Get(screenRow, 1, screenRow, num)return linedef Main():# 打开一个已经存在的会话# xsh.Session.Open("ssh://user:pass_word@192.168.31.52:22")xsh.Session.Open("G:\\user\\Documents\\NetSarang Computer\\7\\Xshell\\Sessions\\192.168.31.52.xsh")xsh.Screen.Synchronous = Truexsh.Session.Sleep(1000)# 切换到root用户xsh.Screen.Send("su root\r")xsh.Session.Sleep(100)line = get_current_row_info(30)if "Password" in line:xsh.Screen.Send("paas_word\n")

实例5

# import xsh.Session
# import xsh.Screen
# import xsh.Dialog
import redef get_current_row_info(num: int):"""获取终端当前行num个字符"""screenRow = xsh.Screen.CurrentRowline = xsh.Screen.Get(screenRow, 1, screenRow, num)return linedef Main():# 在一个存在的会话中执行xsh.Screen.Synchronous = Truexsh.Session.Sleep(1000)xsh.Screen.Send("ssh zzb@192.168.31.52\r")xsh.Session.Sleep(100)# 当第一次登录时,会验证初始ssh连接line = get_current_row_info(100)if re.search("yes/no", line):xsh.Screen.Send("yes\r")xsh.Session.Sleep(100)# 终端出现提示输入密码时,输入密码line = get_current_row_info(100)if re.search("paasword", line, flags=re.I):xsh.Screen.Send("yes\r")if "password" in line:xsh.Screen.Send("paas_word\r")

实例6

/* 测试函数 */
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();
}

xshell自动化脚本相关推荐

  1. python自动化办公脚本下载-python自动化脚本

    广告关闭 腾讯云双11爆品提前享,精选热门产品助力上云,云服务器首年88元起,买的越多返的越多,最高满返5000元! 运维自动化,已经成为运维必不可少的一部分,下面附上自己写的监控nginx_stat ...

  2. python_selenium之第一个自动化脚本

    python_selenium之第一个自动化脚本 上一节介绍了xpath的使用,接下来完成第一个自动化脚本 一.步骤: 1. 这里使用火狐浏览器,首先打开火狐浏览器 2. 使浏览器窗口最大化 3.输入 ...

  3. xshell添加脚本

    ##### xshell添加脚本 ``` 属性 连接 - 用户身份验证 - 登陆脚本 - 添加 等待:[usmshell]$ 发送:open 212 //212是指188那台机器的ID 再添加一个 等 ...

  4. lua自动化脚本关于文本读写和特殊不可见字符转换的处理

    lua自动化脚本关于文本读写和特殊不可见字符转换的处理 因为lua在读写文本中可能还会碰到一些比较变态的不可见字符,%s," "这些转换不了,所以需要做特殊的处理. 参考菜鸟教程的 ...

  5. 2.4 编写第一个自动化脚本

    编写一个简单的自动化脚本 1 #coding=utf-8 2 from Selenium import webdriver 3 driver = webdriver.Firefox() 4 drive ...

  6. Jenkins构建自动化脚本执行无界面解决方法

    场景: jenkins构建selenium自动化用例的时候,会有jenkins自带服务后台运行自动化脚本,可无界面运行IE.Chrome.Firefox. 然而运行IE浏览器时候(IE比较特殊),Je ...

  7. unittest+discover批量处理自动化脚本

    自动化的目的就是批量执行脚本代替手动测试的繁重劳动,单个脚本的执行较为容易,但是事实上自动化脚本时很多的,因此需要批量执行,这个时候需要用到unittest模块里的discover方法进行多个自动化脚 ...

  8. 什么是python自动化脚本_Python自动化

    龙源期刊网 http://www.qikan.com.cn Python 自动化 作者:张焱 来源:<电子技术与软件工程> 2017 年第 22 期 随着人工智能的发展,机器代替人工,已经 ...

  9. crt脚本怎么添加等待时间_secureCRT自动化脚本(之定时任务)

    有时候需要每天定时拉取服务器上的日志文件到跳板机,而又苦于没有权限操作crontab命令,怎么办呢??? 一些windows远程登录软件(Putty,cygwin,secureCRT--)可以帮忙哦~ ...

最新文章

  1. C++ STL: lower_bound 和 upper_bound
  2. 讯飞输入法皮肤制作_手机输入法哪家强:百度、搜狗、讯飞输入法对比评测
  3. c语言会出现fullgc,以上述代码为基础,在发生过一次FullGC后,上述代码在He
  4. java中使用json以及所导入的包
  5. mysql中int型的zerofill参数
  6. ie代理配置错误_电缆厂组织结构图的7个常犯错误!
  7. Proxmark3 Easy Gui 4.0 5.0 5.1全卡克隆已解密的IC卡
  8. 浅谈测试职业的发展空间
  9. 图像全参考客观评价算法比较
  10. 【blog】用emoji-java解决Emoji存储MySQL乱码问题
  11. FRR BGP协议分析4 -- 路由更新(1)
  12. 计算机为啥启用不了网络发现,Windows7系统无法启用网络发现怎么解决?
  13. DRAM原理-Storage Cell
  14. 国风·召南·野有死麕
  15. PostgreSQL 11 与 pgAdmin4 在 Windows 平台上的安装和使用
  16. python爬虫爬取豆瓣图书
  17. 关于抖音年前活动的需求与思考
  18. finalcut剪切快捷键_final cut pro怎么用快捷键把时间线上的素材移动到入点或剪辑点...
  19. 百度云 php api接口调用 签名计算
  20. ①、学习spring cloud之HTML 简介

热门文章

  1. python管道界面_python中管道用法入门实例
  2. python字符串转归类_pandas剪切:如何将分类标签转换为字符串(否则无法导出到Excel)?
  3. 骑士人才linux伪静态,骑士人才CMS伪静态规则
  4. 新上市Lighthouse专用芯片TS3633规格介绍
  5. 这份宝典火了,小哥学后加薪30W+
  6. 中科大“量子鹊桥”登Nature封面,量子牛郎和织女没见面就能纠缠了
  7. 你们AI圈儿,已经引起了罗马教皇的警惕
  8. 一口气发布1008种机器翻译模型,GitHub最火NLP项目大更新:涵盖140种语言组合
  9. 百度AI“杀入”新领域,我举双手支持!
  10. VUE 动态给对象增加属性,并触发视图更新。