#!/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的更多相关文章

  1. python之pexpect模块

    最近在看<Python自动化运维技术与最佳实战>这本书,学到了一个运维中用到的模块:pexpect 下面是其定义: Pexpect 是一个用来启动子程序并对其进行自动控制的 Python ...

  2. python的pexpect模块

    Pexpect 是 Don Libes 的 Expect 语言的一个 Python 实现,是一个用来启动子程序,并使用正则表达式对程序输出做出特定响应,以此实现与其自动交互的 Python 模块. P ...

  3. 8.python 系统批量运维管理器之pexpect模块

    小插曲 前几节讲了paramiko模块,但是pexpect模块的功能几乎跟paramiko一样,先来分析一下: 1.各自介绍 pexpect是一个通过启动子程序,使用正则表达式对程序输出做出特定响应, ...

  4. Python中的Pexpect模块的简单使用

    Pexpect 是一个用来启动子程序并对其进行自动控制的 Python 模块. Pexpect 可以用来和像 ssh.ftp.passwd.telnet 等命令行程序进行自动交互.以下所有代码都是在K ...

  5. python的pexpect详解

    Pexpect 是一个用来启动子程序并对其进行自动控制的纯 Python 模块. Pexpect 可以用来和像 ssh.ftp.passwd.telnet 等命令行程序进行自动交互.继第一部分< ...

  6. python的库有多少个?python有多少个模块?

    这里列举了大概500个左右的库: !   Chardet字符编码探测器,可以自动检测文本.网页.xml的编码. colorama主要用来给文本添加各种颜色,并且非常简单易用. Prettytable主 ...

  7. Pexpect模块的安装

    Pexpect模块的安装 下载地址:https://pypi.python.org/pypi/pexpect/ 解压后在目录下运行:python ./setup.py install (必须是root ...

  8. 正则表达式与Python中re模块的使用

    正则表达式与Python中re模块的使用 最近做了点爬虫,正则表达式使用的非常多,用Python做的话会用到re模块. 本文总结一下正则表达式与re模块的基础与使用. 另外,给大家介绍一个在线测试正则 ...

  9. python之常用模块4

    pyinotify模块 pip3 install pyinotify pyinotify提供的事件: 事件标志 事件含义 IN_ACCESS 被监控项目或者被监控目录中的文件被访问,比如一个文件被读取 ...

随机推荐

  1. [转]应聘Java,jsp,j2ee软件工程师笔试中可能出现的问题

    相信大家应聘的时候第一关就是笔试,即使是一位很有经验的工程师也不一定能够顺利通过,笔试不好,可能就无法进行面试,即使能够进行面试,考官对你的印象也不会很好,问的问题也会很多,估计很难吃的消,当然如果你 ...

  2. 312. Burst Balloons

    题目: Given n balloons, indexed from 0 to n-1. Each balloon is painted with a number on it represented ...

  3. c++ 字符串函数用法举例

    1. substr() 2. replace() 例子:split() 字符串切割: substr 函数原型: , size_t n = npos ) const; 解释:抽取字符串中从pos(默认为 ...

  4. Can't find bundle for base name ClientMessages, locale zh_CN

    这是个关于JAVA国际化方面的语音包的问题. 提示这个错误信息就是说找不到代码里写的配置文件. 我这个错误发生在导入一个已经存在的项目时发生的. 解决办法:将配置文件*.properties所在的文件 ...

  5. Seajs demo

    index.html <!doctype html> <html lang="en"> <head> <meta charset=&quo ...

  6. ACM数据结构相关资料整理【未完成,待补充】

    在网上总是查不到很系统的练ACM需要学习的数据结构资料,于是参考看过的东西,自己整理了一份. 能力有限,欢迎大家指正补充. 分类主要参考<算法竞赛入门经典训练指南>(刘汝佳),山东大学数据 ...

  7. Android Touch(2)View.OnTouchEvent与View.OnTouchListener区别

    1,在官方文档 docs/reference/android/view/View.OnTouchListener.html 中对OnTouchListener的描述 Interface definit ...

  8. BZOJ 2956 模积和

    题目链接:http://61.187.179.132/JudgeOnline/problem.php?id=2956 题意:给出n和m.计算: 思路: i64 n,m; i64 cal(i64 m,i ...

  9. Unix/Linux 关机命令

    关机命令 AIX shutdown now Solaris init 5 Redhat shutdown now Centos shutdown now

  10. 谈谈Perforce

    实习就要结束了,收获之一就是学会了使用Perforce! Perforce SCM System是一款构建于可伸缩客户/服务器结构之上的软件配置管理工具.仅仅应用 TCP/IP,开发人员就能够通过多种 ...