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. HDU 2062 Subset sequence

    我是把它当做一道数学题来做的. 这篇题解写的有点啰嗦,但是是我最原始的思维过程. 对于一个集合An= { 1, 2, …, n },在n比较小的情况下,在纸上按字典顺序把所有子集排列一下. 以n=3, ...

  2. Nginx安全配置

    nginx本身不能处理PHP,它只是个web服务器,当接收到请求后,如果是php请求,则发给php解释器处理,并把结果返回给客户端.nginx一般是把请求发fastcgi管理进程处理,fastcgi管 ...

  3. [转载]charisma-master 加载慢的原因及解决方法

    [我的总结] 原文中指出的地址有的已经转换,因为版本问题. 所以根据2014年11月获取的charisma-master版本,应做以下更改: 1.charisma-app.css 这个文件中的外链字体 ...

  4. iOS基础CGAffineTransform的简单使用

    CoreGraphics框架中的CGAffineTransform类可用于设定UIView的transform属性,控制视图的缩放.旋转和平移操作: 另称放射变换矩阵,可参照线性代数的矩阵实现方式0. ...

  5. 【JavaScript学习笔记】if使用

    <html> <body> <script language="JavaScript"> var a=4; var b=2; if(a==3) ...

  6. 【英语】Bingo口语笔记(73) - 以tly,tely结尾的误读

  7. 【Python】类和对象、继承、使用文件、存储、异常、标准库(不懂)

    当你调用这个对象的方法MyObject.method(arg1, arg2)的时候,这会由Python自动转为MyClass.method(MyObject, arg1, arg2)——这就是self ...

  8. 关闭iptables(Centos)

    由于搭建了CDH-Hadoop,方便起见,事先关闭了防火墙: services iptables stop; chkconfig iptables off; services ip6tables st ...

  9. centos6 下安装xfce+vnc

    CentOS 安装图形界面的过程,简单记录一下.这里提供了两种图形界面的安装,分别是CentOS自带的gnome桌面及轻巧的xfce.据测试,我的精简版CentOS 6 64位系统安装gnome需要下 ...

  10. Ajax请求内嵌套Ajax请求的方法

    前段时间做项目,需要把全国省市的两个XML文件整合成一个JSON格式的数据,手写的话觉得数据太多了,而且容易出错,于是就想到了用Ajax嵌套的方法来解决,就想平时用Ajax的方法直接嵌套,都会先读出外 ...