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. Android Studio插件

    awesome-android-ui(UI范例库) https://github.com/wasabeef/awesome-android-ui/tree/master/pages LMBanners ...

  2. 51nod1394 差和问题

    我只会用线段树写...不喜欢树状数组..其实跑的也不算慢?然后各种*的时候忘了longlong一直WA...药丸! 而且我不怎么会用map离散化...那么就sort+unique #include&l ...

  3. Windows 8获取开发者账户

    使用PowerShell获取开发者账户,可以在本地调试Metro APP C:\PS> Show-WindowsDeveloperLicenseRegistration //安装licence ...

  4. mysql-主从复制(二)

    1)主服务器上开启binlog服务器 log-bin=mysql-bin 2)用户授权(并不是privileges授权!!!!),正确有从服务器授权如下 grant replication slave ...

  5. VFL示例

    •[cancelButton(72)]-12-[acceptButton(50)] •取消按钮宽72point,accept按钮宽50point,它们之间间距12point • •[wideView( ...

  6. Android 系统属性

    /************************************************************************ * Android 系统属性 * 说明: * 由于需 ...

  7. 判断https

    判断https <script> if(window.location.protocol=='https:'){ window.location.href='http://e.abchin ...

  8. ora-0000 normal跟/etc/hosts有关

    当hosts文件配置错误时,用sqlplus登录后startup nomount,就会报错ORA-00000 [oracle11g@testdb2 dbs]$ sqlplus "/ as s ...

  9. 怎样学法学?——民法学泰斗王利明教授的演讲 z

    今晚我讲“怎样学习法律”,但不是讲一般的学习法学的方法,而是主要从法学.法律的特征讲起.(因为)我们学习任何东西,都首先要搞清楚我们的学习对象有什么特征.性质. 我们要了解法律.法学本身的性质,要了解 ...

  10. C++ 虚拟继承

    1.为什么要引入虚拟继承 虚拟继承是多重继承中特有的概念.虚拟基类是为解决多重继承而出现的.如:类D继承自类B1.B2,而类B1.B2都继 承自类A,因此在类D中两次出现类A中的变量和函数.为了节省内 ...