ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

简单介绍python连接telnet和ssh的两种方式

2021-11-09 02:31:28  阅读:173  来源: 互联网

标签:__ handle stdout python telnet sleep ssh time logging


本文主要介绍了python连接telnet和ssh的两种方式,文中通过示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
Telnet 连接方式
#!/usr/bin/env python
# coding=utf-8
  
import time
import telnetlib
import 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.argv
del buf[0]
str1 = ' '.join(buf)
print(str1)
#执行命令行参数给出的命令
stdin,stdout,stderr = client.exec_command(str1)
#time.sleep(1)
print(stdout.read())

到此这篇关于详解python连接telnet和ssh的两种方式的文章就介绍到这了。

本文地址:https://www.linuxprobe.com/python-telnet-ssh.html

标签:__,handle,stdout,python,telnet,sleep,ssh,time,logging
来源: https://www.cnblogs.com/cainiaoyige1/p/15527073.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有