Remote replication with Python (SCP + expect)

Source: Internet
Author: User

SCP is very powerful, but requires manual input of password, of course, you can save the public key in the remote host's ~/.ssh directory, and then do not enter password, but this needs to be configured.

Use Sshpass may enter password in command, but need to be installed with "sudo apt-get install Sshpass"

If you do not want to use the above two methods, you can write scripts with expect can help us to automatically interact

Although Python also offers pexpect modules, since expect is simple, why not just use Os.system () to do it?

Here is the class I wrote that implements remote replication

[HTML]View Plaincopy
  1. Class Remoteshell:
  2. def __init__ (self, host, user, PWD):
  3. self.host = host
  4. self.user = user
  5. self.pwd = pwd
  6. def put (self, Local_path, Remote_path):
  7. Scp_put = " '
  8. Spawn SCP%s%[email protected]%s:%s
  9. Expect "(yes/no)?" {
  10. Send "yes\r"
  11. Expect "Password:"
  12. Send "%s\r"
  13. } "Password:" {send "%s\r"}
  14. Expect EOF
  15. Exit ""
  16. Os.system ("Echo '%s ' > scp_put.cmd"% (scp_put% (Os.path.expanduser (Local_path), Self.user, Self.host, Remote_ Path, self.pwd, Self.pwd )))
  17. Os.system (' expect Scp_put.cmd ')
  18. Os.system (' rm scp_put.cmd ')


But found every time the files are not copied, I want to see what expect actually do, fortunately expect provide the-D parameter to give more information.

The last discovery is that the file was copied too large, expect timed out

Add "Set Timeout-1" to the script before it's OK.

[HTML]View Plaincopy
    1. Scp_put = " '
    2. Set Timeout-1
    3. Spawn SCP%s%[email protected]%s:%s
    4. Expect "(yes/no)?" {
    5. Send "yes\r"
    6. Expect "Password:"
    7. Send "%s\r"
    8. } "Password:" {send "%s\r"}
    9. Expect EOF
    10. Exit ""


Summarize

1) Expect each statement is executed sequentially

[HTML]View Plaincopy

Because the SCP may return first (yes/no)? Return password:, may also return directly password:, consider the order relation, the above statement hierarchy relation actually is as follows:

[HTML]View Plaincopy
    1. Expect "(yes/no)?" {Send "yes\r"
    2. Expect "Password:"
    3. Send "%s\r"
    4. }
    5. "Password:" {send "%s\r"}


2) whenever the Spawn program has output time, expect will go to match, if not match, and so on the next time there is output, again to perform the current expect, until the timeout (I use expect-d to track, the conclusion); Of course, you can set no timeout "set timeout- 1 "

3) If expect exits, the program spawn by it will be killed.

4) At the end of the spawn, it will be detected by expect to the standard output EOF, just as the expect script exits the time.

For SCP, 100% can be detected first because the SCP outputs the replication progress and then detects the EOF

[HTML]View Plaincopy
    1. Expect "100%%"
    2. Expect EOF

5) Expect is part of the match, so don't worry about yourself not knowing the full output of the program

Remote replication with Python (SCP + expect)

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.