pyhon项目之后pexpect使用
1.安装
pip3.6 install pexpect

实例1 ssh 登陆linux 服务器,并且执行命令

#!/usr/bin/env python3.6
# -*- coding: utf-8 -*-
# @Time : 2019/10/24 17:33
# @Author : zoulixiang
# @Site :
# @File : ssh_login_password.py
# @Software: PyCharm
import pexpect

def login_ssh_passwd(port="",user="",host="",passwd="",command=""):
'''函数:用于实现pexepect实现ssh的自动化用户密码登陆'''

#print 'ssh -p %s %s@%s' %(port, user, host)
if port and user and host and passwd and command:
ssh = pexpect.spawn('ssh -p %s %s@%s %s' %(port, user, host, command))
i = ssh.expect(['password:', 'continue connecting (yes/no)?'], timeout=5)
if i == 0:
ssh.sendline(passwd)
elif i == 1:
ssh.sendline('yes\n')
ssh.expect('password: ')
ssh.sendline(passwd)

index = ssh.expect(["#",pexpect.EOF, pexpect.TIMEOUT])

if index == 0:
print("logging in as root!")
#执行命令
ssh.before,ssh.after
elif index == 1:
print("logging in as non-root!")
print(ssh.before)
elif index == 2:
print("logging process exit!")
elif index == 3:
print("logging timeout exit")
else:
print("Parameter error!")

def login_ssh_key(keyfile="",user="",host="",port="", command=""):
'''函数:用于实现pexepect实现ssh的自动化密钥登陆'''
if port and user and host and keyfile and command:
ssh = pexpect.spawn('ssh -i %s -p %s %s@%s %s '% (keyfile,port,user,host,command))
i = ssh.expect([pexpect.TIMEOUT,'continue connecting (yes/no)?'], timeout=5)
print("-"*10)
print(i)
if i == 0:
ssh.sendline('yes\n')
index = ssh.expect(["#", pexpect.EOF, pexpect.TIMEOUT])
else:
index = ssh.expect(["#", pexpect.EOF, pexpect.TIMEOUT])
print(index)

if index == 0:
#执行命令
ssh.before,ssh.after
print("logging in as root!")
elif index == 1:
print("logging in as non-root!")
elif index == 2:
print("logging process exit!")
elif index == 3:
print("logging timeout exit")
else:
print("Parameter error!")

def main():
'''主函数:实现两种f方法分别登陆'''
#command = "hostname"
#login_ssh_passwd(port='22', user='root',host='192.168.3.201',passwd='matrix@178',command='pwd')
login_ssh_key(keyfile="/root/.ssh/id_rsa",port='22',user='root',host='192.168.3.201', command="mkdir -p /root/test2222")

if __name__ == '__main__':
main()

pyhon项目之后pexpect使用的更多相关文章

  1. python cmd 启动python项目报错:no module named “xxx”

    场景:使用pycharm编辑器启动pyhon项目时可以启动,但使用cmd启动时,会报:no module named “xxx”的错误,此时,有两种情况: 1.no module named “xxx ...

  2. Python:渗透测试开源项目

    Python:渗透测试开源项目[源码值得精读] sql注入工具:sqlmap DNS安全监测:DNSRecon 暴力破解测试工具:patator XSS漏洞利用工具:XSSer Web服务器压力测试工 ...

  3. Python:渗透测试开源项目【源码值得精读】

    sql注入工具:sqlmap DNS安全监测:DNSRecon 暴力破解测试工具:patator XSS漏洞利用工具:XSSer Web服务器压力测试工具:HULK SSL安全扫描器:SSLyze 网 ...

  4. Pyhon 日志管理 -- logging

    Pyhon 日志管理 -- logging 一直觉得运行程序是能打印日志是一个神奇的事情,不懂日志产生的原理,后来听说Pyhton 有一个logging模块,So,要好好研究一下. 先贴出代码,看看她 ...

  5. Jenkins-slave分布式环境构建与并行WebUi自动化测试项目

    前言 之前搭建过selenium grid的分布式环境,今天我们再来搭建一次Jenkins的分布式环境:jenkins-slave Jenkins的Master-Slave分布式架构主要是为了解决Je ...

  6. Python+Django+ansible playbook自动化运维项目实战☝☝☝

    Python+Django+ansible playbook自动化运维项目实战☝☝☝  一.入门引导 DevOPSDevOps(英文Development和Operations的组合)是一组过程.方法 ...

  7. Github开源人脸识别项目face_recognition

    Github开源人脸识别项目face_recognition 原文:https://www.jianshu.com/p/0b37452be63e 译者注: 本项目face_recognition是一个 ...

  8. Fis3前端工程化之项目实战

    Fis3项目 项目目录结构: E:. │ .gitignore │ fis-conf.js │ index.html │ package.json │ README.md │ ├─material │ ...

  9. 【原】Android热更新开源项目Tinker源码解析系列之三:so热更新

    本系列将从以下三个方面对Tinker进行源码解析: Android热更新开源项目Tinker源码解析系列之一:Dex热更新 Android热更新开源项目Tinker源码解析系列之二:资源文件热更新 A ...

随机推荐

  1. SQL Server merge用法

    有两个表名:source 表和 target 表,并且要根据 source 表中匹配的值更新 target 表. 有三种情况: source 表有一些 target 表不存在的行.在这种情况下,需要将 ...

  2. 给定制的vuejs组件添加v-model双向绑定支持

    用过vuejs的前端工程师,对于v-model一定印象深刻.它向类似textarea,input等原生html原生添加双向数据绑定的能力非常方便.但是对于你的定制vue组件并不是能够直接应用v-mod ...

  3. 2.Shell脚本中的set指令,比如set -x 和 set -e

    set参数介绍 set指令能设置所使用shell的执行方式,可依照不同的需求来做设置 -a 标示已修改的变量,以供输出至环境变量. -b 使被中止的后台程序立刻回报执行状态. -C 转向所产生的文件无 ...

  4. 程序游戏推荐(C语言贪吃蛇,python天天酷跑(需要安装pygame),js是狠人就坚持30s)

    下面是下载位置,我把他们上传到我的文件下了. C语言贪吃蛇:https://files.cnblogs.com/files/ITXiaoAng/%E8%B4%AA%E5%90%83%E8%9B%87. ...

  5. 基于vue+springboot+docker网站搭建【七】制作后端spring-boot的docker镜像部署

    制作spring-boot的docker镜像并部署 一.下载后端项目:https://github.com/macrozheng/mall 二.修改mall-admin项目的配置文件 修改applic ...

  6. FreeMarker学习系列之一

    一. 基本概念 ${...}: FreeMarker将会输出真实的值来替换大括号内的表达式,这样的表达式插值 FTL 标签 (FreeMarker模板的语言标签): FTL标签和HTML标签有一些相似 ...

  7. 用JSON.stringify去实现深拷贝的弊端

    无法处理function啦还有RegExp啦,还有无法处理循环引用对象(这个我还没有test) 就前两点的话 var a={ a:, b:new RegExp('test'), c:function( ...

  8. 项目中 直接跳后台 不用输入/newadmin

  9. flink Iterate迭代基本概念

    基本概念:在流中创建“反馈(feedback)”循环,通过将一个算子的输出重定向到某个先前的算子.这对于定义不断更新模型的算法特别有用. 迭代的数据流向:DataStream → IterativeS ...

  10. while(n)什么意思?

    1.n是bool型变量时,就是代表n为true时运行循环(bool表示布尔型变量,也就是逻辑型变量的定义符,以英国数学家.布尔代数的奠基人乔治·布尔(George Boole)命名. bool类似于f ...