pexpect &&pxssh
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
pexpect &&pxssh的更多相关文章
- 系统批量运维管理器pexpect详解
一.pexpect介绍 pexpect可以理解成Linux下的expect的Python封装,通过pexpect我们可以实现对ssh.ftp.passwd.telnet等命令进行自动交互,而无需人工干 ...
- 8.python 系统批量运维管理器之pexpect模块
小插曲 前几节讲了paramiko模块,但是pexpect模块的功能几乎跟paramiko一样,先来分析一下: 1.各自介绍 pexpect是一个通过启动子程序,使用正则表达式对程序输出做出特定响应, ...
- Python模块之pexpect
一.pexpect模块介绍 Pexpect使Python成为控制其他应用程序的更好工具.可以理解为Linux下的expect的Python封装,通过pexpect我们可以实现对ssh,ftp,pass ...
- Python SSH登陆--pexpect,pxssh
from pexpect import pxssh host = '192.168.80.139'user = 'allen'password = 'allen'command = 'df -h' d ...
- pexpect的pxssh类实现远程操作
#!/usr/bin/pythonimport pexpectfrom pexpect import pxssh import getpasstry: s=pxssh.pxssh() hostname ...
- 探索 Pexpect,第 1 部分:剖析 Pexpect
Pexpect 是一个用来启动子程序并对其进行自动控制的 Python 模块. Pexpect 可以用来和像 ssh.ftp.passwd.telnet 等命令行程序进行自动交互.本文章介绍 Pexp ...
- python之pexpect模块
最近在看<Python自动化运维技术与最佳实战>这本书,学到了一个运维中用到的模块:pexpect 下面是其定义: Pexpect 是一个用来启动子程序并对其进行自动控制的 Python ...
- Python模块之pxssh
pxssh模块用于在python中ssh远程连接,执行命令,返回结果,但注意不支持Windows系统 #!/usr/bin/env python #-*- coding:utf-8 -*- from ...
- pexpect 初坑
通过一个很坑的任务,最近认识了一个新坑: pexpect .其实基本的用法并不难,不过,我还是跟以前一样,把几个基本函数的坑蹚了个遍.有感而发,记录一下. 首先简单的介绍一下这个坑,哦不对,这个库.这 ...
随机推荐
- 操作系统 - epoll中的ET和LT
参考 https://www.quora.com/What-is-meant-by-edge-triggering-and-level-triggering http://man7.org/linux ...
- Linux06——安装JDK、Tomcat、Eclipse
一.安装JDK(具体解压命令在Linux02中) ①将JDK解压到opt目录下(opt就是文件夹) ②配置环境变量 vim /etc/profile JAVA_HOME=/opt/jdk1.8.0 ...
- bugku getshell
http://123.206.87.240:8002/web9/ 该题是walf严格匹配,通过修改Content-type后字母的大小写可以绕过检测, 然后还有,后缀黑名单检测和类型检测,逐个绕过,如 ...
- AcWing 849. Dijkstra求最短路 I 朴素 邻接矩阵 稠密图
//朴素Dijkstra 边权都是正数 稠密图:点和边差的比较多 #include<cstring> #include<iostream> #include<algori ...
- JVM工具使用和Linux-top命令解析
top 命令 top 命令查看现在线程占用资料的情况. 第三行,cpu状态信息,具体属性说明如下: us — 用户空间占用CPU的百分比.(重要) sy — 内核空间占用CPU的百分比. ni — 改 ...
- 2019冬季PAT甲级第四题
#define HAVE_STRUCT_TIMESPEC #include<bits/stdc++.h> using namespace std; typedef struct node{ ...
- Pascal 错误代码及含义
DOS 错误代码:1无效DoS功能号 2文件末找到 3路径未找到 4打开文件过多 5禁止文件存取 6无效文件句柄 12无效文件存取代码 15无效驱动器号 16不能删除当前日录 17不能跨驱动器改文件名 ...
- Django 生成数据库表时的报错TypeError: __init__() missing 1 required positional argument: 'on_delete'
原因及解决办法: https://www.cnblogs.com/phyger/p/8035253.html
- Arctic Network(洛谷)--北极通讯网络(loj)
洛谷传送门 loj传送门 一道蛮基础的最小生成树的题 题意也没绕什么圈子 只是叙述的有点累赘而已(loj上是这样的 也就读入加建边需要稍稍稍多想一下下 对于我这么一个蒟蒻 这是一道很好的板子题 (洛谷 ...
- linux下安装mongo数据库存
https://www.runoob.com/mongodb/mongodb-linux-install.html 安装完后,要重启一下,否则无法运行./mongod 下载完安装包,并解压 tgz(以 ...