首页 > 其他 > 详细

pexpect &&pxssh

时间:2019-05-15 01:17:56      阅读:186      评论:0      收藏:0      [点我收藏+]

python 3.6

pip  install pexpect

[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 
#!/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)
from pexpect import pxssh
from src import logutils
log=logutils.logger("app",rootstdout=True,handlerList=[‘I‘,‘E‘])
px=pxssh.pxssh()
def px_ssh(host,user,pwd,):
    px=pxssh.pxssh()
    px.login(server=host,username=user,password=pwd)
    px.sendline(‘df -m &&ps -ef|grep java‘)
    px.prompt()
    log.info(px.before.decode(‘utf-8‘))
    px.logout()

def del_std(res):
    std=‘‘
    for i  in res:

        output=i.decode("utf-8")

        std +=output
    return std
if __name__ == ‘__main__‘:
    px_ssh(‘192.168.81.129‘,‘root‘,‘admin‘)

  

pexpect &&pxssh

原文:https://www.cnblogs.com/SunshineKimi/p/10867043.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!