python 3.6

pip  install pexpect

#!/usr/bin/python3
import os
import sys
curPath = os.path.abspath(os.path.dirname(__file__))
rootPath = os.path.split(curPath)[0]
sys.path.append(rootPath)
import platform
from src import logutils
log=logutils.logger("app",rootstdout=True,handlerList=['I','E']) if platform.system() =="Windows":
raise SystemError('Windows目前不支持 pxssh')
elif platform.system() =="Linux":
try:
from pexpect import pxssh except Exception:
raise ImportError('Windows目前不支持 pxssh') def px_ssh(host,user,pwd):
px=pxssh.pxssh()
px.login(server=host,username=user,password=pwd)
px.sendline("echo ''|telnet 192.168.81.129 8882 ")
px.prompt()
log.info(deal_std(px.before))
px.logout() def deal_std(res):
std=''
if type(res).__name__=='list':
return ''.join(res)
elif type(res).__name__=="bytes":
std=res.decode("utf-8")
return std
elif type(res).__name__ == "str":
return res
for i in res:
output=i.decode("utf-8")
std +=output
return std

  

[root@hostuser src]# vi pexpect_ssh.py
[root@hostuser src]# python3 pexpect_ssh.py
[INFO]2019-05-15 00:23:30 Wed --app-- pexpect_ssh.py:
df -m &&ps -ef|grep java
Filesystem 1M-blocks Used Available Use% Mounted on
/dev/mapper/centos-root 27627 8618 19009 32% /
devtmpfs 894 0 894 0% /dev
tmpfs 910 1 910 1% /dev/shm
tmpfs 910 11 900 2% /run
tmpfs 910 0 910 0% /sys/fs/cgroup
/dev/sda1 1014 232 783 23% /boot
tmpfs 182 1 182 1% /run/user/42
tmpfs 182 0 182 0% /run/user/0
root 21286 21238 0 00:23 pts/2 00:00:00 grep --color=auto java [root@hostuser src]# cat pexpect_ssh.py

最新文章

  1. Hadoop运维
  2. [Machine-Learning] matlab 矩阵常见基本操作
  3. [No000013]在Office中关闭自动拼写检查和自动语法检查
  4. Java for LeetCode 149 Max Points on a Line
  5. rac安装oem
  6. 计算 MD5值
  7. Semaphore的介绍和使用
  8. FLASH图片上传功能—从百度编辑器UEditor里面提取出来
  9. ISG2015
  10. Linux的kobject机制
  11. mysql外键
  12. Golang http 服务器
  13. ASP.NET Core 2.0: 二. 开发环境
  14. 正确的git开发流程
  15. 爬虫系列----scrapy爬取网页初始
  16. 跨域两种解决方案CORS以及JSONP
  17. python 3 实现文件下载的方法总结
  18. 【转】Spring Boot 构建应用——快速构建 Spring Boot 应用
  19. 快速排序 之添加复合插入排序和原始序列取中值左pivot
  20. 【LeetCode每天一题】Merge Intervals(合并区间)

热门文章

  1. css transform 2D3D转换
  2. 项目git的步骤,将项目托管到码云
  3. Jquery获取html参数, jquery.params.js 获取参数
  4. springboot 整合GuavaCache缓存
  5. django view 视图控制之数据返回的视图函数
  6. HBase 2.1.3 集群 web 报错InvalidProtocolBufferException 解决方法
  7. C语言程序设计(二)
  8. java.lang.String和java.util.NClob互相转换
  9. 部署etcd中使用ansible进行变量初始化
  10. Library management system design requirements