[python]使用pexpect模块进行批量scp
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#wangxiaofei
#awcloud自动化测试
import time,os
import threading
import sys
threads = []
username = 'root'
passwd = 'www.awcloud.com'
ssh_ip = ['10.11.11.4',
'10.11.11.5',
'10.11.11.6',
'10.11.11.7'
]
try:
import pexpect
except ImportError:
print 'Can not found pexpect module,Now install it......'
os.system('yum install pexpect -y')
time.sleep(3)
check_pexpect=os.popen('rpm -qa | grep pexpect')
if 'pexpect' in check_pexpect.read():
print 'check pexpect module is exists,going on...'
else:
print 'check pexpect module is not exists,exit...'
sys.exit()
else:
import pexpect
def scp_file():
i=0
for i in range(len(ssh_ip)):
try:
scp_command=pexpect.spawn('scp '+scp_filename+' root@'+ssh_ip[i]+':/root/')
expect_result = scp_command.expect([r'assword:',r'yes/no'],timeout=30)
if expect_result == 0:
scp_command.sendline(passwd)
#这句话真的很神奇,如果不加这句话,程序会执行,但是不执行copy,请各路神人解释
scp_command.read()
elif expect_result == 1:
scp_command.sendline('yes')
scp_command.expect('assword:',timeout=30)
scp_command.sendline(passwd)
#don't delete this code,if your do then,the program will be faill.
scp_command.read()
#important
else:
print 'Unknow Result......'
except pexpect.EOF:
print 'Uploading Fail....'
print pexpect.EOF
except pexpect.TIMEOUR:
print 'Uploading time out.....'
time.sleep(2)
if '__name__' == '__main__':
scp_file()
[python]使用pexpect模块进行批量scp的更多相关文章
- python之pexpect模块
最近在看<Python自动化运维技术与最佳实战>这本书,学到了一个运维中用到的模块:pexpect 下面是其定义: Pexpect 是一个用来启动子程序并对其进行自动控制的 Python ...
- python的pexpect模块
Pexpect 是 Don Libes 的 Expect 语言的一个 Python 实现,是一个用来启动子程序,并使用正则表达式对程序输出做出特定响应,以此实现与其自动交互的 Python 模块. P ...
- 8.python 系统批量运维管理器之pexpect模块
小插曲 前几节讲了paramiko模块,但是pexpect模块的功能几乎跟paramiko一样,先来分析一下: 1.各自介绍 pexpect是一个通过启动子程序,使用正则表达式对程序输出做出特定响应, ...
- Python中的Pexpect模块的简单使用
Pexpect 是一个用来启动子程序并对其进行自动控制的 Python 模块. Pexpect 可以用来和像 ssh.ftp.passwd.telnet 等命令行程序进行自动交互.以下所有代码都是在K ...
- python的pexpect详解
Pexpect 是一个用来启动子程序并对其进行自动控制的纯 Python 模块. Pexpect 可以用来和像 ssh.ftp.passwd.telnet 等命令行程序进行自动交互.继第一部分< ...
- python的库有多少个?python有多少个模块?
这里列举了大概500个左右的库: ! Chardet字符编码探测器,可以自动检测文本.网页.xml的编码. colorama主要用来给文本添加各种颜色,并且非常简单易用. Prettytable主 ...
- Pexpect模块的安装
Pexpect模块的安装 下载地址:https://pypi.python.org/pypi/pexpect/ 解压后在目录下运行:python ./setup.py install (必须是root ...
- 正则表达式与Python中re模块的使用
正则表达式与Python中re模块的使用 最近做了点爬虫,正则表达式使用的非常多,用Python做的话会用到re模块. 本文总结一下正则表达式与re模块的基础与使用. 另外,给大家介绍一个在线测试正则 ...
- python之常用模块4
pyinotify模块 pip3 install pyinotify pyinotify提供的事件: 事件标志 事件含义 IN_ACCESS 被监控项目或者被监控目录中的文件被访问,比如一个文件被读取 ...
随机推荐
- html5 语音搜索
开始以为是接口什么的,原来这就是语言搜索. 只需要在input加上x-webkit-speech <input type="text" class="text&qu ...
- linux shell 命令学习(5) xxd- make a hexdump or do the reverse.
对于标准输入或者给定的文件,显示其16进制的内容.也可以反过来进行转换. xxd -h[elp] xxd [options] [infile [outfile]] xxd -r[evert] [opt ...
- mongo中查询Array类型的字段中元素个数
I have a MongoDB collection with documents in the following format: { "_id" : ObjectId(&qu ...
- PowerDesigner将PDM导出生成WORD文档
PowerDesigner将PDM导出生成WORD文档 环境 PowerDesigner15 1.点击Report Temlates 制作模板 2.如果没有模板,单击New图标创建.有直接双击进入. ...
- Python 时间戳与时间字符串互相转
#设a为字符串 import time a = "2011-09-28 10:00:00" #中间过程,一般都需要将字符串转化为时间数组 time.strptime(a,'%Y-% ...
- 第十篇 PO核心功能及流程详解
详见链接:http://bbs.erp100.com/thread-272866-1-1.html1. P2P lifecycleP2P是procure to pay的缩写,p2p循环值得就是采购到付 ...
- linux vi 编辑器命令
游标控制 h 游标向左移 j 游标向下移 k 游标向上移 l (or spacebar) 游标向右移 w 向前移动一个单词 b 向后移动一个单词 e 向前移动一个单词,且游标指向单词的末尾 ( 移到当 ...
- You can't specify target table 'charge' for update in FROM clause
mysql中不能这么用. (等待mysql升级吧)错误提示就是说,不能先select出同一表中的某些值,再update这个表(在同一语句中) 替 换方 案: create table tmp as s ...
- Nginx proxy_pass 加与不加 "/" 区别
下面四种情况分别用http://192.168.1.100/proxy/test.html 进行访问. 第一种: location /proxy/ { proxy_pass http://127.0 ...
- 使用tdcss.js轻松制作自己的style guide
http://jakobloekke.github.io/tdcss.js/ 在前端开发中,如果能够有一个style guide对于设计来说就显得专业稳定,一致性.在上述链接中,有一个tdcss.js ...