python连接telnet和ssh的两种方式是什么
发表于:2024-10-01 作者:千家信息网编辑
千家信息网最后更新 2024年10月01日,本篇内容主要讲解"python连接telnet和ssh的两种方式是什么",感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习"python连接telnet和ssh的
千家信息网最后更新 2024年10月01日python连接telnet和ssh的两种方式是什么
本篇内容主要讲解"python连接telnet和ssh的两种方式是什么",感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习"python连接telnet和ssh的两种方式是什么"吧!
Telnet 连接方式
#!/usr/bin/env python# coding=utf-8 import timeimport telnetlibimport logging __author__ = 'Evan' save_log_path = 'result.txt'file_mode = 'a+'format_info = '%(asctime)s - %(filename)s[line:%(lineno)d] - %(levelname)s: %(message)s' logger = logging.getLogger(__name__)logger.setLevel(logging.DEBUG) # 添加记录 记录器功能fh = logging.FileHandler(save_log_path, mode=file_mode)fh.setLevel(logging.DEBUG)fh.setFormatter(logging.Formatter(format_info))logger.addHandler(fh)# 增加显示 记录器功能ch = logging.StreamHandler()ch.setLevel(logging.DEBUG)ch.setFormatter(logging.Formatter(format_info))logger.addHandler(ch) def telnet_handle(host='', port=''): handle = telnetlib.Telnet(host, port, timeout=10) handle.set_debuglevel(2) # Display connect info (send command & received info) logger.debug('Connect host: {} port: {} successful'.format(host, port)) try: #获取登录提示'login:' 后输入密码。 handle.read_until('login:', timeout=5) #发送命令 登录,用户名:admin 密码:admin handle.write('admin\n') #用户名 #如果有输入密码的提示符可以打开这一条,并修正确的密码提示符 #handle.read_until('输入密码提示符', timeout=5) time.sleep(1) handle.write('admin\n') #密码 time.sleep(1) handle.write('en\n') #执行指令 time.sleep(1) handle.write('sys\n') #执行指令 time.sleep(1) handle.write('display running-config\n') #执行指令 time.sleep(1) handle.write('show stack\n') #执行指令 time.sleep(1) #读取所有信息 result = handle.read_very_eager() logger.info('Received info: {}'.format(result)) finally: handle.close() if __name__ == '__main__': telnet_handle(host='192.168.10.1', port='23')
ssh连接方式
#!/usr/bin/env python# coding=utf-8 import paramiko,sys,time client = paramiko.SSHClient()client.set_missing_host_key_policy(paramiko.AutoAddPolicy())#连接SSH服务器client.connect("192.168.10.1",22,"admin","admin")#执行命令的方式一 连接linux发送固定指令stdin,stdout,stderr = client.exec_command("whoami")time.sleep(2)print(stdout.read())stdin,stdout,stderr = client.exec_command("cat /root/lzhi/c_call_python.txt")print(stdout.read())stdin,stdout,stderr = client.exec_command("ls")print(stdout.read())stdin,stdout,stderr = client.exec_command("ls -la")print(stdout.read()) #执行命令的方式二 获取命令行参数,并且删除参数1.保留需要执行的命令buf = sys.argvdel buf[0]str1 = ' '.join(buf)print(str1)#执行命令行参数给出的命令stdin,stdout,stderr = client.exec_command(str1)#time.sleep(1)print(stdout.read())
到此,相信大家对"python连接telnet和ssh的两种方式是什么"有了更深的了解,不妨来实际操作一番吧!这里是网站,更多相关内容可以进入相关频道进行查询,关注我们,继续学习!
方式
命令
密码
指令
提示
参数
提示符
输入
内容
功能
用户
用户名
记录器
学习
登录
实用
更深
信息
兴趣
实用性
数据库的安全要保护哪些东西
数据库安全各自的含义是什么
生产安全数据库录入
数据库的安全性及管理
数据库安全策略包含哪些
海淀数据库安全审计系统
建立农村房屋安全信息数据库
易用的数据库客户端支持安全管理
连接数据库失败ssl安全错误
数据库的锁怎样保障安全
论文网络安全的总结
遗留数据库的坏处
软件开发公司推销
在酒店电脑使用网络安全吗
石油大亨 无法连接服务器
深圳微商软件开发设计
手机总是显示服务器开小差
云计算与网络安全的区别
数据库配置 c
表格中修改全部相同数据库
英魂之刃服务器为什么卡顿
南京恒策网络技术有限公司官网
济南国尚网络技术公司
软件开发 专业特长
江苏pdu服务器电源可以定制吗
游戏手机制作软件开发
我的世界服务器4g运行内存够吗
添加数据库用户
429网络安全日 百度
我们该如何来保护网络安全
网络安全电子期刊
邯郸项目软件开发定做
宝山区网络技术咨询公司
身份认证是网络安全的一种技术吗
開源 数据库监控工具
为什么学校近期要强调网络安全
北京服务器机柜哪种好
链猫重庆网络技术有限公司
AMD服务器级cpu
防火墙连接服务器