pyhon项目之后pexpect使用
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使用的更多相关文章
- python cmd 启动python项目报错:no module named “xxx”
场景:使用pycharm编辑器启动pyhon项目时可以启动,但使用cmd启动时,会报:no module named “xxx”的错误,此时,有两种情况: 1.no module named “xxx ...
- Python:渗透测试开源项目
Python:渗透测试开源项目[源码值得精读] sql注入工具:sqlmap DNS安全监测:DNSRecon 暴力破解测试工具:patator XSS漏洞利用工具:XSSer Web服务器压力测试工 ...
- Python:渗透测试开源项目【源码值得精读】
sql注入工具:sqlmap DNS安全监测:DNSRecon 暴力破解测试工具:patator XSS漏洞利用工具:XSSer Web服务器压力测试工具:HULK SSL安全扫描器:SSLyze 网 ...
- Pyhon 日志管理 -- logging
Pyhon 日志管理 -- logging 一直觉得运行程序是能打印日志是一个神奇的事情,不懂日志产生的原理,后来听说Pyhton 有一个logging模块,So,要好好研究一下. 先贴出代码,看看她 ...
- Jenkins-slave分布式环境构建与并行WebUi自动化测试项目
前言 之前搭建过selenium grid的分布式环境,今天我们再来搭建一次Jenkins的分布式环境:jenkins-slave Jenkins的Master-Slave分布式架构主要是为了解决Je ...
- Python+Django+ansible playbook自动化运维项目实战☝☝☝
Python+Django+ansible playbook自动化运维项目实战☝☝☝ 一.入门引导 DevOPSDevOps(英文Development和Operations的组合)是一组过程.方法 ...
- Github开源人脸识别项目face_recognition
Github开源人脸识别项目face_recognition 原文:https://www.jianshu.com/p/0b37452be63e 译者注: 本项目face_recognition是一个 ...
- Fis3前端工程化之项目实战
Fis3项目 项目目录结构: E:. │ .gitignore │ fis-conf.js │ index.html │ package.json │ README.md │ ├─material │ ...
- 【原】Android热更新开源项目Tinker源码解析系列之三:so热更新
本系列将从以下三个方面对Tinker进行源码解析: Android热更新开源项目Tinker源码解析系列之一:Dex热更新 Android热更新开源项目Tinker源码解析系列之二:资源文件热更新 A ...
随机推荐
- 图解微信小程序---实现行的删除和增加操作
图解微信小程序之实现行的删除和增加操作 代码笔记部分 第一步:在项目的app.json中创建一个新的页面(页面名称英文,可自定义) 第二步:在创建的新页面中编写页面(注意bindtap属性值,因为是我 ...
- WPF 高级篇 MVVM 附加属性
原文:WPF 高级篇 MVVM 附加属性 WPF 特性之一 附加属性 在本文里实现文本框内容的验证 public class TextBoxHelper:DependencyObject { publ ...
- 【spring】spring retry介绍
一.为什么需要重试? 我们知道只要是网络请求都有失败的情况,这个时候增加retry机制是必要的.而spring全家桶中就有这么一套机制. 二.spring retry spring系列的spring ...
- java request.getInputStream中文乱码解决方案
请求时要指定为UTF-8,中文码码完美解决 /** * * 得到请求body字符串,一般用于content-type:application/json * */ public static Strin ...
- vue展示md文件,前端读取展示markdown文件
方案1:每次都需要重新打包,每次修改都需要build 直接使用require + v-html: 核心代码如下: 1. 首先需要添加MD文件的loader就是 markdown-loader npm ...
- LINUX中ORACLE 11.2.0.1 升级到11.2.0.4
11.2.0.4补丁号13390677,共7个文件,分别是 其中1&2是db,3是grid,4是client,5是gateways,6是example,7是deinstall 上传安装介质并解 ...
- SpringBoot 定时任务篇
一. 基于注解@Scheduled默认为单线程,开启多个任务时,任务的执行时机会受上一个任务执行时间的影响. 1.创建定时器 使用SpringBoot基于注解来创建定时任务非常简单,只需几行代码便可完 ...
- MySQL多实例安装、配置、启动(四)
一.规划信息 系统信息: cat /etc/redhat-release CentOS Linux release (Core) # uname -r -.el7.x86_64 数据库规划 PORT: ...
- python数据类型之二
列表 list #定义:[]内可以有多个任意类型的值,逗号分隔 基本语法 # 定义:[]内可以有多个任意类型的值,逗号分隔 # my_girl_friends=['alex','wupeiqi','y ...
- Natas25-writeup
前言 题目链接: http://natas25.natas.labs.overthewire.org 做这一题花了一些时间,也是由于自己知识点掌握不足,所以分享下解题过程. 题目分析 首先,登录后看到 ...