来自:#
如果你想对n台服务器进行批量管理,从而提高效率,本文或许对你有些帮助
如果大家有不明白的地方或其它问题,可以联系我,互相交流,共同进步,我的qq群:17489917
SecureCRT是一款终端仿真程序,支持ssh、Telnet和rlogin协议,可以在windows平台通过它对unix、linux进行访问,相比同类软件,它的强大之处就在于支持vbs、js等脚本,利用脚本可实现批量操作,如:获取服务器相关信息、部署软件、修改密码、升级软件、上传/下载、修改文件等等,还能生成excel报表
最大的优点,就是无需在服务器端安装agent,这是其他软件无法与之相媲美的,当初在给工行做项目的时候,在3000多台服务器上部署软件,用的就是SecureCRT脚本
下面分享一个例子:该脚本用vbs语言编写,脚本会读取iplist文件,通过ssh方式连接服务器,在centos5.5_64bit系统上自动安装lrzsz软件,脚本对目标服务器进行了判断,如已经安装lrzsz软件则退出,否则执行安装程序。
SecureCRT-vbs脚本使用说明
一.脚本运行环境
1. 需要在客户端安装SecrueCRT软件
二.脚本使用方法
1.打开SecureCRT,不连接任何会话
2.选择脚本菜单----执行,选择你要运行的脚本,
如弹出下面的提示,选择接受并保存。
3.脚本执行过程中,最好不要进行其他操作,脚本执行完后会自动退出
三.自动安装脚本参数介绍:
outputpath = ".\install-log.txt" '输出文件,脚本运行后会自动创建
iplistpath = ".\ip.txt" 'ip列表文件,需提前创建,每行有2列,用1个空格作为分隔符,首尾不能有多余的空格或其他字符,iplist格式如下:
192.168.0.1 123456
192.168.0.2 abcd1234
loginuser = "root" '登陆用户
productpath = "/usr/local/src" '产品路径,可自行定义
Ssh、Main部分都已写成函数,一般情况下无需更改。你需要的是维护脚本具体执行相关功能部门的函数即可。
- # $language = "VBScript"
- # $interface = "1.0"
- ' Written by Peng Xiaofeng
- ' @ 201107
- ' QQ:5876006
- Const ForReading = 1
- Const ForWriting = 2
- Dim fso, outputpath , iplistpath , file, iplist, Str, StrIp, StrPwd
- Dim loginuser
- Dim index , index1 , n ,productpath
- '======================path and some parameters ========================================
- outputpath = ".\install-log.txt" '输出文件
- iplistpath = ".\ip.txt" 'ip列表文件,每行有2列,用1个空格作为分隔符,首尾不能有多余的空格
- loginuser = "root" '登陆用户
- productpath = "/usr/local/src" '产品路径
- '=======================define end=================================================
- '=======================open file==================================================
- Set fso = CreateObject("Scripting.FileSystemObject")
- Set file = fso.OpenTextFile(outputpath , ForWriting, True) ' result file path
- Set iplist = fso.OpenTextFile(iplistpath , ForReading, False) ' ip-list file path
- '========================end open==================================================
- '========================================ssh start=====================================
- Sub Ssh(ip)
- crt.Screen.Synchronous = True
- On Error Resume Next
-
- cmd = "/ssh2 /L " & loginuser &" /PASSWORD " & StrPwd & " /C 3DES " & ip
- crt.Session.Connect cmd
-
- If Err.Number <> 0 Then
- file.Write " Can't connect 22 port:" & vbCr
- index = 99
- crt.Session.Disconnect
- Else
- Do
- index = crt.Screen.WaitForStrings("#" , ">" , 10)
- Select Case index
- Case "0"
- file.Write "login fail:" & loginuser
- index = 99
- Exit Do
- Case "1"
- index = 100
- Exit Do
- Case "2"
- index = 100
- Exit Do
- End Select
- Loop
- If index <> 100 Then
- crt.Session.Disconnect
- End If
- End If
- End Sub
- '=========================================ssh end======================================
- '========================================install begin==================================
- Sub Install
- crt.Sleep 1000
- crt.Screen.Send "cd " & productpath & vbCr
- crt.Screen.Send "LANG=en_US.UTF-8;export LANG" & vbCr
- crt.Sleep 1000
- crt.Screen.Send "LC_ALL=en_US.UTF-8;export LC_ALL" & vbCr
- crt.Sleep 1000
- crt.Screen.Synchronous = False
- crt.Screen.Send "uname -rsm" & vbCr '判断系统版本
- Do
- index1 = crt.Screen.WaitForStrings("Linux 2.6.18-194.el5 x86_64", "Linux 2.6.9-55.ELsmp i686", "Linux 2.6.18-194.el5 i686", "Linux 2.6.18-164.ESX x86_64", 5)
- Select Case index1
- Case "0"
- Exit Do
- Case "1"
- 'CENTOS5.5 64bit
- crt.Screen.Send "wget ftp://ftp.sunet.se/pub/Linux/distributions/centos/5.6/os/x86_64/CentOS/lrzsz-0.12.20-22.1.x86_64.rpm" & vbCr
- crt.Sleep 12000
- crt.Screen.Send "ls" & vbCr
- crt.sleep 1000
- Exit Do
- Case "2"
- 'RHEL4U4 32bit
- crt.Screen.Send "ls" & vbCr
- crt.sleep 1000
- Exit Do
- Case "3"
- 'CENTOS5.5 32bit
- crt.Screen.Send "ls" & vbCr
- crt.sleep 1000
- Exit Do
- Case "4"
- 'ESX 4.1 64bit
- crt.Screen.Send "ls" & vbCr
- crt.sleep 1000
- Exit Do
- End Select
- Loop
- If index1 = 0 Then
- file.Write " time out! : Cann't confirm os version! "
- Else
- crt.Screen.Send "rpm -qa|grep rzsz" & vbCr '判断lrzsz是否安装
- If crt.Screen.WaitForString("lrzsz", 5) = True Then
- file.Write " lrzsz Exists "
- Else
- crt.Screen.Send "ls" & vbCr
- crt.sleep 1000
- crt.Screen.Send "rpm -ivh lrzsz-0.12.20-22.1.x86_64.rpm" & vbCr '执行安装命令
- crt.sleep 3000
- crt.Screen.Send "ls /usr/bin/rz" & vbCr
- If crt.Screen.WaitForString("/usr/bin/rz", 5) = True Then '判断是否安装成功
- file.Write "lrzsz install success" '结果写进输出文件中
- crt.Screen.Send "rm -f lrzsz-0.12.20-22.1.x86_64.rpm" & vbCr '删除安装程序
- crt.sleep 1000
- Else
- file.Write "lrzsz install fail"
- crt.sleep 1000
- End If
- End If
- End If
- crt.Screen.Send "exit" & vbCr
- End Sub
- '======================================install end====================================
- '=========================================Main begin==================================
- Sub Main
- Do While iplist.AtEndOfStream <> True
- '逐行读取iplist文件,分别赋予2个变量StrIp和StrPwd
- Str = iplist.Readline
- n = InStr(Str, " ")
- StrIp = Left(Str, n)
- n = Len(Str) - n
- StrPwd = Right(Str, n)
- '下面的把结果写到输出文件中
- file.Write "Ip:" & StrIp & "-"
- Ssh StrIp
-
- If index = 99 Then
- crt.Sleep 1000
- Else
- crt.Screen.Send "id " & vbCr
- If crt.Screen.WaitForString("uid=0", 3) = True Then
- crt.Sleep 1000
- Install '执行install过程
- End If
- End If
- crt.Session.Disconnect
- file.Write vbCrLf
- Loop
- End Sub
- '=======================================Main end========================================
附件 SecureCRT脚本.zip (35.12 KB)
阅读(4710) | 评论(0) | 转发(0) |