Debian 上 SecureCRT 脚本执行方法
一 前置准备
二 在 SecureCRT 中直接运行脚本
三 连接建立后自动执行脚本
四 最小可用脚本示例
# $language = "Python"
# $interface = "1.0"
import SecureCRT
def main():
host = "192.0.2.10"
user = "admin"
pwd = "P@ssw0rd"
# 建立 SSH2 连接
crt.Session.Connect(f"/SSH2 {user}@{host}")
# 等待密码提示并输入
crt.Screen.WaitForString("password:")
crt.Screen.Send(f"{pwd}\r")
# 等待命令提示符(示例为 $ 或 #)
prompt = "$"
crt.Screen.WaitForString(prompt)
# 执行命令
crt.Screen.Send("uname -a\r")
crt.Screen.WaitForString(prompt)
# 可选:保存会话日志
# crt.Session.LogFileName = f"/tmp/{host}_{crt.Version}_session.log"
# crt.Session.Log(True)
main()
# $language = "Python"
# $interface = "1.0"
import SecureCRT
def main():
prompt = "#"
crt.Screen.Send("cat /etc/os-release\r")
crt.Screen.WaitForString(prompt)
out = crt.Screen.ReadString(prompt)
print("=== OS Release ===")
print(out.strip())
main()
五 常见问题与排错