SecureCRT 是一款广泛使用的终端仿真程序,支持多种脚本语言,如VBScript、Python 和 Perl,用户可以编写脚本来自动化常见任务,如登录、配置和文件传输。以下是关于 Debian SecureCRT 脚本编写的基础信息:
time
、os
、csv
等。以下是一个使用 Python 编写的简单示例脚本,用于自动登录到多个设备并执行命令:
# $language = "python"
# $interface = "1.0"
import time
import os
import csv
user = 'admin'
passwd = 'huawei@123'
def log_name():
current_time = time.strftime('%Y-%m-%d-%H-%M-%S', time.localtime(time.time()))
return current_time
def main():
crt.Screen.Synchronous = False
with open('ip.txt', 'r') as rfile:
host_list = rfile.readlines()
for host in host_list:
ip = host.split()[0]
cmd = f'/SSH2 /L {user} /PASSWORD {passwd} /C 3DES /M MD5 {ip}'
crt.Session.ConnectInTab(cmd)
time.sleep(1)
crt.Screen.Send('\r')
crt.Screen.Send('\r')
crt.Screen.WaitForString('>')
current_name = log_name()
time.sleep(1)
crt.Session.LogFileName = f"{current_name}_{ip}_{current_time}_log.txt"
crt.Session.Log(True)
time.sleep(1)
crt.Screen.Send('\r')
crt.Screen.WaitForString('>')
crt.Screen.Send('display cur \r')
while True:
crt.Screen.WaitForStrings([" ---- More ----", '<bj>"])
waitindex = crt.Screen.MatchIndex
if waitindex == 1:
crt.Dialog.MessageBox(' ---- More ----')
time.sleep(1)
crt.Screen.Send(' ')
elif waitindex == 2:
crt.Screen.Send('\r')
break
crt.Screen.Send('\r')
crt.Screen.Send('\r')
crt.Screen.WaitForString('>')
crt.Screen.Send('display ip inter brie \r')
print
函数输出变量值,检查脚本执行流程。通过掌握这些基础知识和技巧,您可以更有效地使用 SecureCRT 进行自动化任务,提高工作效率。