Python pexpec to resolve SCP ssh

Source: Internet
Author: User

Paswd_key = '. *assword.* ' match password

Ssh_newkey = '. * (yes/no). * ' Match is sure you want to continue connecting (yes/no)

[Python]View Plaincopy
  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 '% (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%[email protected]%s:%s '% (filename, user, IP, dst_path)
  29. Else:
  30. CmdLine = ' scp%s%[email protected]%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 to resolve SCP ssh

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.