paswd_key = '.*assword.*'  匹配Password

ssh_newkey = '.*(yes/no).*' 匹配 Are you sure you want to continue connecting (yes/no)

  1. #!/usr/bin/python2.7
  2. import pexpect
  3. import os, sys, getpass
  4. def ssh_command(user, host, password, command):
  5. ssh_newkey = '.*(yes/no).*'
  6. passwd_key = '.*assword.*'
  7. child = pexpect.spawn('ssh -l %s %s %s' %(user, host, command))
  8. child.logfile = sys.stdout
  9. i = child.expect([pexpect.TIMEOUT, ssh_newkey, passwd_key])
  10. if i == 0: #timeout
  11. print child.before
  12. print "Error time out"
  13. print child.after
  14. return None
  15. if i ==1 :
  16. child.sendline('yes')
  17. i = child.expect([pexpect.TIMEOUT, passwd_key])
  18. if i == 0:
  19. print child.before
  20. print 'time out ERROR'
  21. print child.after
  22. return None
  23. child.sendline(password)
  24. return child
  25. def scp2(ip, user, passwd, dst_path, filename):
  26. passwd_key = '.*assword.*'
  27. if os.path.isdir(filename):
  28. cmdline = 'scp -r %s %s@%s:%s' % (filename, user, ip, dst_path)
  29. else:
  30. cmdline = 'scp %s %s@%s:%s' % (filename, user, ip, dst_path)
  31. try:
  32. child = pexpect.spawn(cmdline)
  33. child.expect(passwd_key)
  34. child.sendline(passwd)
  35. child.expect(pexpect.EOF)
  36. #child.interact()
  37. #child.read()
  38. #child.expect('$')
  39. print "uploading"
  40. except:
  41. print "upload faild!"
  42. def main():
  43. host = raw_input('Hostname:')
  44. user = raw_input('User:')
  45. password = getpass.getpass()
  46. command = raw_input('Command:')
  47. child = ssh_command(user, host, password, command)
  48. child.expect(pexpect.EOF)
  49. print child.before
  50. if __name__ == "__main__":
  51. main()

Python pexpec 解决scp ssh的更多相关文章

  1. 解决 scp 和rsync 同步失败【rsync error: unexplained error (code 255) at io.c(226) [sender=3.1.2]】

    解决 scp 和rsync 同步失败 报错信息截图: 解决问题的步骤: 1.检查对方的scp和rsync 是否能使用. rsync 在使用的时候,需要客户端和服务端都有rsync工具.scp 和 rs ...

  2. 深入super,看Python如何解决钻石继承难题 【转】

    原文地址 http://www.cnblogs.com/testview/p/4651198.html 1.   Python的继承以及调用父类成员 python子类调用父类成员有2种方法,分别是普通 ...

  3. paip.python错误解决24

    paip.python错误解决 作者Attilax 艾龙, EMAIL:1466519819@qq.com 来源:attilax的专栏 地址:http://blog.csdn.net/attilax ...

  4. paip.python错误解决23

    paip.python错误解决 作者Attilax 艾龙, EMAIL:1466519819@qq.com 来源:attilax的专栏 地址:http://blog.csdn.net/attilax ...

  5. 无需输入密码的scp/ssh/rsync操作方法

    一般使用scp/ssh/rsync传输文件时,都需要输入密码.下面是免密码传输文件的方法. 假设要在两台主机之间传送文件,host_src & host_dst.host_src是文件源地址所 ...

  6. 深入super,看Python如何解决钻石继承难题

    1.   Python的继承以及调用父类成员 python子类调用父类成员有2种方法,分别是普通方法和super方法 假设Base是基类 class Base(object): def __init_ ...

  7. (转)python通过paramiko实现,ssh功能

    python通过paramiko实现,ssh功能 1 import paramiko 2 3 ssh =paramiko.SSHClient()#创建一个SSH连接对象 4 ssh.set_missi ...

  8. shell脚本中解决SCP命令需要输入密码的问题

    使用密钥文件.       这里假设主机A(192.168.100.3)用来获到主机B(192.168.100.4)的文件.   在主机A上执行如下命令来生成配对密钥: ssh-keygen -t r ...

  9. (转载)深入super,看Python如何解决钻石继承难题

    1.   Python的继承以及调用父类成员 python子类调用父类成员有2种方法,分别是普通方法和super方法 假设Base是基类 class Base(object): def __init_ ...

随机推荐

  1. [ionic开源项目教程] - 第13讲 Service层优化,提取公用Service,以及生活和农业两大模块的实现

    关注微信订阅号:TongeBlog,可查看[ionic开源项目]全套教程. 这一讲主要实现生活和农业两大模块的实现,在这个过程中,对service层提取出一个公用的BaseService. 这一讲分为 ...

  2. jQuery中filter(),not(),split()的用法

    filter(),not(): <script type="text/javascript"> $(document).ready(function() { //输出 ...

  3. UVA 489 Hangman Judge (字符匹配)

    题意:给一个字符串A,只含小写字符数个.再给一个字符串B,含小写字符数个.规则如下: 1.字符串B从左至右逐个字符遍历,对于每个字符,如果该字符在A中存在,将A中所有该字符删掉,若不存在,则错误次数+ ...

  4. 流程引擎的API和服务基础

    RepositoryService :  管理和控制 发布包 和 流程定义(包含了一个流程每个环节的结构和行为) 的操作 除此之外,服务可以 查询引擎中的发布包和流程定义. 暂停或激活发布包,对应全部 ...

  5. C# 创建系统服务并定时执行【转载】

    [转载]http://www.cnblogs.com/hfzsjz/archive/2011/01/07/1929898.html C# 创建系统服务并定时执行 1.新建项目 --> Windo ...

  6. 剑指offer—第二章算法之二分查找(旋转数组的最小值)

    旋转数组的最小数字 题目:把一个数组最开始的若干元素搬到数组的末尾,我们称之为数组的旋转.输入一个递增排序的数组的一个旋转,输出旋转数组的最小元素.例如:数组{3,4,5,1,2}为{1,2,3,4, ...

  7. 为什么要用Java泛型

    啥是泛型? 泛型(generic)是指参数化类型的能力.可以定义带泛型类型的类或方法,随后编译器会用具体的类型来代替它. 举个栗子 上述代码在编译期没有问题,但在运行期,将会报错.就是因为List的a ...

  8. 【转】uboot移植(一)BootLoader基本概念

    原文网址:http://blog.chinaunix.net/uid-25445243-id-3869348.html 一.BootLoader简介1.1.嵌入式Linux软件结构与分布 在一般情况下 ...

  9. SmartWeatherAPI_Lite_WebAPI C# 获取key加密

    中国气象局面向网络媒体.手机厂商.第三方气象服务机构等用户,通过 web 方式提供数据气象服务的官方载体. 在一周前已经申请到appid,但是苦于没有C#版的key 的算法,一直验证不通过,经过几天查 ...

  10. 实用js+css多级树形展开效果导航菜单

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...