Python 官方文档:入门教程 => 点击学习
# coding: utf8import re, sysimport pexpect# enable/disable debug modeDEBUG = Falsedef telnet_login(ip, pwd, cmd, ps):
# coding: utf8
import re, sys
import pexpect
# enable/disable debug mode
DEBUG = False
def telnet_login(ip, pwd, cmd, ps):
child = pexpect.spawn('telnet %s' % ip)
# 是否启用调试模式, 默认位False,在上面设置DEBUG=True后开启调试模式
if DEBUG:
print '[' + '-' * 30 + "DEBUG INFO START" + '-' * 30 + "]\n"
child.logfile_read = sys.stdout # telnet输出至标准输出
child.expect('(?i)PassWord: ', timeout=2) # 匹配Password: ,注意问号后有空格
child.send(pwd + '\r') # 这里要输入密码+回车(\r),不要用sendline方法
child.expect('Please press ENTER.\r\n', timeout=1)
child.send('\r') # 根据上面提示,按回车后继续
child.expect(ps[0], timeout=2) # 匹配第1提示符
child.send('system-view' + '\r') # 进入system-view视图
child.expect(ps[1], timeout=2) # 匹配第2个提示符
output = ""
out=""
for tcmd in cmd:
child.send(tcmd + '\r') # 执行命令
child.expect(tcmd + '\r') # 匹配命令回显
child.expect(ps[1], timeout=2) # 匹配命令执行完提示符
out = child.before # 捕获命令的输出结果
if out != '':
out = re.sub('.*\[.*', '', out) # 处理输出结果的尾部提示符
out = re.sub('\015', '', out) # 处理输出结果的尾部^M(实际上是回车符)
output += "\n" + tcmd + "执行结果: \n"+"\n".join([j.strip() for j in out.split('\n') if j != '']) # 删除命令输出中的多余空行和行首尾空格
return child, output
if __name__ == '__main__':
host = "192.168.*.*"
password = "######"
command = ['dis ip', 'dis arp'] # 执行命令列表
prompt = ['\)\>', '\)\]'] # 提示符
c, cmdstdout = telnet_login(host, password, command, prompt)
# debug开启后, 也可以把命令输出结果写入文件
if DEBUG:
with open("/tmp/telnet_output.txt", "w") as f:
f.write(cmdstdout)
print '\n\n[' + '-' * 30 + "DEBUG INFO END" + '-' * 30 + "]\n"
print '[' + '-' * 30 + "telnet command output" + '-' * 23 + "]\n"
print cmdstdout # 打印命令执行结果
c.close(force=True)
## 注释: 其中 c 和 cmdstdout 是用来接收 telnet_login 函数返回 的 child 和 output
--结束END--
本文标题: python跟H3C 5024E交换机交
本文链接: https://www.lsjlt.com/news/189180.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
下载Word文档到电脑,方便收藏和打印~
2024-03-01
2024-03-01
2024-03-01
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
一口价域名售卖能注册吗?域名是网站的标识,简短且易于记忆,为在线用户提供了访问我们网站的简单路径。一口价是在域名交易中一种常见的模式,而这种通常是针对已经被注册的域名转售给其他人的一种方式。
一口价域名买卖的过程通常包括以下几个步骤:
1.寻找:买家需要在域名售卖平台上找到心仪的一口价域名。平台通常会为每个可售的域名提供详细的描述,包括价格、年龄、流
443px" 443px) https://www.west.cn/docs/wp-content/uploads/2024/04/SEO图片294.jpg https://www.west.cn/docs/wp-content/uploads/2024/04/SEO图片294-768x413.jpg 域名售卖 域名一口价售卖 游戏音频 赋值/切片 框架优势 评估指南 项目规模
0