SecureCrt -Python脚本,自动化的SSH2选项卡未响应要发送的命令



我正在修改Python中的脚本以在Securecrt 8.5.2中运行,以备份我负责的某些Cisco ASR9K设备的运行config,但该脚本似乎结束了突然在第二个成功的SSH2 Hop(第二个选项卡(之后,并且不会发送我脚本脚本的命令(此特定示例中的退出(,这是我拥有的代码,因为我说的是Vandyke页面的修改版本,打开SSH2。

一个重要的一件事是,我必须标记每个路由器的每个会话,因为它不允许直接从Active Cli 中直接进行SSH,因此我必须即兴创作并实施此功能"在标签中连接",我怀疑SecureCrt不知道它是否在我打开的新标签中,因此它不知道该发送命令。

我正在使用30行,但似乎没有任何效果。我正在更改预期的文本,但似乎无法识别正确的选项卡或不读取正确的选项卡。

个人背景:Python语言的完整初学者。

# $language = "python"
# $interface = "1.0"
# Connect to an SSH server using the SSH2 protocol. Specify the
# username and password and hostname on the command line as well as
# some SSH2 protocol specific options.
host = "X.X.X.a"
host2 = "X.X.X.b"
def main():
    crt.Screen.Synchronous = True
    # Prompt for a username and password instead of embedding it in a script...
    #
    usr = crt.Dialog.Prompt("Enter the user name for" + host, "Username", "", True)
    passwd = crt.Dialog.Prompt("Enter TACACS+ for" + host, "Login", "", True)
    # Build a command-line string to pass to the Connect method.
    cmd = "/SSH2 /L %s /PASSWORD %s /C AES-128-CTR /M SHA1 %s" % (usr, passwd, host)
    crt.Session.Connect(cmd)
    crt.Screen.WaitForString("X.X.X.a#")
    crt.Screen.Send("copy running-config tftp:r")
    crt.Screen.WaitForString("Host name or IP address (control-c to abort): []?")
    crt.Screen.Send("tftpserver.comr") 
    crt.Screen.WaitForString("Destination file name (control-c to abort): [running-config]?")
    crt.Screen.Send("X.X.X.a_running_configr")
    crt.Screen.WaitForString("X.X.X.a")
    cmd2 = "/SSH2 /L %s /PASSWORD %s /C AES-128 /M SHA1 %s" % (usr, passwd, host2)
    crt.Session.ConnectInTab(cmd2)
    crt.Screen.WaitForString("X.X.X.b#")
    crt.Screen.Send("exitr")

main()

crt.session.connectintab(cmd2(

它连接到新选项卡中的设备,但我期望的是,脚本将继续执行与host1(x.x.x.x.a(所做的相同的操作,并通过ssh2 tab,将相同的无聊的东西发送到host2(x.x.x.b(,并继续进行迭代过程,直到我为所有需要的设备执行此操作。

感谢您阅读我。

好吧,甚至不容易的是解决这个非常优势的代码或脚本,但这并不是什么问题当我没有所有命令时,为了将光标放在新选项卡上时,必须先解散上一个会话。

解决方案?

crt.session.disconnect((

最新更新