当前位置:   article > 正文

xshell自动化脚本_xshell脚本

xshell脚本

xshell具有脚本功能,可以实现很多自动化的操作。
在这里插入图片描述

从xshell的官方手册,可以知道xshell的脚本分为3类:
在这里插入图片描述

xsh.Session

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

函数

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

变量

名称类型说明
ConnectedBOOL检查当前会话是否连接。
LocalAddressBSTR导入本地地址。
PathBSTR导入当前会话文件的路径。
RemoteAddressBSTR导入远程地址。
RemotePortlong导入远程端口号。
LoggingBOOL检查当前会话是否记录日志。
LogFilePathBSTR​​保存为日志文件。

xsh.Screen

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

函数

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

变量

名称类型说明
CurrentColumnlong返回当前列数。
CurrentRowlong返回当前行数。
Columnslong返回与终端的列宽相同的列数。
Rowslong返回与终端的行高相同的行数。
SynchronousBOOL设置屏幕同步 (True:屏幕同步,false:屏幕不同步)​

xsh.Dialog

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

函数

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

按钮类型:

类型Button返回值
0OK1
1OK / Cancel1 / 2
2Abort / Retry / Ignore3 / 4 / 5
3Yes / No / Cancel6 / 7 / 2
4Yes / No6 / 7
5Retry / Cancel4 / 2
6Cancel / TryAgain / Continue2 / 10 / 11

实例

Sub Main
    Dim hostname, username, password
    hostname = 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 then
        xsh.Session.Open("ssh://" & username & ":" & password & "@" & hostname)
    End If
End Sub
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

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

实例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  
  • 1
  • 2
  • 3
  • 4
  • 5

实例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 = true

xsh.Screen.WaitForString "login: "
xsh.Screen.Send "username"
xsh.Screen.Send VbCr
xsh.Session.Sleep 100

xsh.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.StartLog

Dim waitStrs
waitStrs = Array(Chr(10), "username@password") ' make wait message as array

Dim row, screenrow, readline, itmes
row = 1

' *** Send Command ***
xsh.Screen.Send "cat /etc/passwd"
xsh.Screen.Send VbCr
xsh.Session.Sleep 100

Dim 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 If

screenrow = 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
Loop

wb.SaveAs("C:\chart.xls") ' save file path
wb.Close
app.Quit

Set ws = nothing
Set wb = nothing
Set app = nothing

xsh.Screen.Synchronous = false

xsh.Session.StopLog

End Sub
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76

实例3

Xshell打开多个session(会话)

Sub Main

Dim 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
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14

实例4

# import xsh.Session
# import xsh.Screen
# import xsh.Dialog

def get_current_row_info(num: int):
    """
    获取终端当前行num个字符
    """
    screenRow = xsh.Screen.CurrentRow
    line = xsh.Screen.Get(screenRow, 1, screenRow, num)
    return line


def 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 = True
    xsh.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")
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26

实例5

# import xsh.Session
# import xsh.Screen
# import xsh.Dialog
import re


def get_current_row_info(num: int):
    """
    获取终端当前行num个字符
    """
    screenRow = xsh.Screen.CurrentRow
    line = xsh.Screen.Get(screenRow, 1, screenRow, num)
    return line


def Main():
    # 在一个存在的会话中执行
    xsh.Screen.Synchronous = True
    xsh.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")
    

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37

实例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();
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/从前慢现在也慢/article/detail/197316
推荐阅读
  

闽ICP备14008679号