0

我正在修改python中的脚本以在securecrt 8.5.2中运行,以备份我负责的一些cisco ASR9K设备的运行配置,但脚本似乎在第二次成功的ssh2跃点(第二个选项卡)之后突然结束并且不发送我编写的命令(此特定示例中的退出),这是我拥有的代码,正如我所说的那样,它是用于打开 ssh2 的 vandyke 页面的修改版本。

一件重要的事情是我必须标记每个单独路由器的每个会话,因为它不允许直接从活动 cli 执行 ssh,所以我不得不即兴创作并实现这个“在 TAB 中连接”,我怀疑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.com\r") 
    crt.Screen.WaitForString("Destination file name (control-c to abort): [running-config]?")
    crt.Screen.Send("X.X.X.a_running_config\r")
    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("exit\r")


main()

crt.Session.ConnectInTab(cmd2)

它在新选项卡中连接到设备,但我期望脚本将继续执行与主机 1(XXXa)相同的操作,并通过 ssh2 选项卡将相同的无聊内容发送到主机 2(XXXb),然后继续迭代过程,直到我为我需要的所有设备执行此操作。

谢谢你读我。

4

1 回答 1

0

好吧,解决这个非常次优的代码或脚本甚至都不是一件容易的事,但这并不是什么大问题,唯一的事情是当我输入所有命令时,我不得不取消连接到上一个会话,所以为了将光标放在新标签上,必须先断开上一个会话。

解决方案?

crt.Session.Disconnect()

于 2019-05-09T21:19:19.187 回答