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
- Class Remoteshell:
- def __init__ (self, host, user, PWD):
- self.host = host
- self.user = user
- self.pwd = pwd
- def put (self, Local_path, Remote_path):
- Scp_put = " '
- Spawn SCP%s%[email protected]%s:%s
- Expect "(yes/no)?" {
- Send "yes\r"
- Expect "Password:"
- Send "%s\r"
- } "Password:" {send "%s\r"}
- Expect EOF
- Exit ""
- Os.system ("Echo '%s ' > scp_put.cmd"% (scp_put% (Os.path.expanduser (Local_path), Self.user, Self.host, Remote_ Path, self.pwd, Self.pwd )))
- Os.system (' expect Scp_put.cmd ')
- 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
- Scp_put = " '
- Set Timeout-1
- Spawn SCP%s%[email protected]%s:%s
- Expect "(yes/no)?" {
- Send "yes\r"
- Expect "Password:"
- Send "%s\r"
- } "Password:" {send "%s\r"}
- Expect EOF
- 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
- Expect "(yes/no)?" {Send "yes\r"
- Expect "Password:"
- Send "%s\r"
- }
- "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
- Expect "100%%"
- 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)