dynamics 关联SecureCRT,以及SecureCRT基本设置Python小脚本

##一.修改dynagen.ini配置文件

  #1.注释掉原本telnet使用的putty

#telnet = start ../bin/putty/putty %h %p
 #2.添加SecureCRT启动信息

telnet = start C:\SecureCRT\SecureCRT.exe /SCRIPT C:\Users\dell\Desktop\Dynamips@EDURainbow\bin\dynagen/crt_py.py /T  /telnet %h %p

C:\SecureCRT\SecureCRT.exe  ->>SecureCRT所在位置

C:\Users\dell\Desktop\Dynamips@EDURainbow\bin\dynagen/crt_py.py  ->>python小脚本所在位置

/T  ->>在同一个标签下打开

python脚本:crt_py.py

# $language = "Python"
# $interface = "1.0"

#Connected to Dynamips VM "R1" (ID 0, type c7200) - Console port
def main():
	crt.Screen.Synchronous = True
        if crt.Screen.WaitForString("Console port"):
		crt.Screen.Send('\r')
	prompt=crt.Screen.Get(1,0,2,80)   #获取第一第二行数据,由于刚出来信息可能在第一行或第二行
	hostname=prompt.strip().split()[4].strip('"') #抽取hostname信息
	crt.Window.Caption= hostname
	if crt.Screen.WaitForString("[yes/no]:"):
		crt.Screen.Send("no\r")
		crt.Screen.Send("\r")
	while(1):
		crt.Sleep(5)
	        if crt.Screen.WaitForString("Router>"):
			crt.Screen.Send('\r')
			crt.Screen.Send('en\r')
			crt.Screen.Send('conf t\r')
			crt.Screen.Send('hostname %s\r' %hostname)
			crt.Screen.Send('no ip domain-lookup\r')
			crt.Screen.Send('line con 0\r')
			crt.Screen.Send('logg synchronous\r')
			crt.Screen.Send('no exec-timeout\r')
			crt.Screen.Send('exit\r')
			crt.Screen.Clear()
			break

main()

SecureCRT支持VBScript,而SecureCRT6.6版本才支持Python脚本,所以要想成功运行,需要查看自己的SecureCRT版本。

如果版本不满足,可更新,这里是我的网盘下载地址:点击打开链接

http://115.com/file/dplhs3qt#SecureCRT_6.7.3-x86.rar

怎样在Windows中自动登录一台服务器并执行相关工作呢?

###:以telnet方式:

新建一批处理文件:cs_telnet.bat

@echo off
start C:\SecureCRT\SecureCRT.exe /SCRIPT C:\Users\dell\Desktop\Dynamips@EDURainbow\cs.py 
新建一Python脚本:
# $language = "Python"
# $interface = "1.0"

def main():
    crt.Screen.Synchronous = True
    crt.Session.Connect("/TELNET 214.25.28.25 23") #23号端口

    crt.Screen.WaitForString("login:")
    crt.Screen.Send("veyun\r")
    crt.Screen.WaitForString("word:")
    crt.Screen.Send("XXXXXXXX\r")  #password
    crt.Screen.Send("ls\r")
    crt.Screen.Synchronous = False

main()
然后双击cs.bat即可登录并运行。

###以ssh登录:

将Python脚本修改为:

# $language = "python"
# $interface = "1.0"

def main():
    cmd = "/SSH2 /L %s /PASSWORD %s /C 3DES /M MD5 %s" % ('veyun', 'XXXXXX', '214.25.28.25')
    crt.Session.Connect(cmd)

main()













你可能感兴趣的:(python,脚本,cmd,VBScript,interface,dell)