python3实现scp命令-python执行scp命令拷贝文件及文件夹到远程主机的目录方法

  • Post author:
  • Post category:python


系统环境centos7

python2.7

先在操作系统安装expect

[root@V71 python]# vi 3s.py

#!/usr/bin/python

#coding:utf-8

import sys,re

import os

import subprocess

#scp file to remote node.

def scpFileToRemoteNode(user,ip,password,localsource,remotedest,port=22):

SCP_CMD_BASE = r”””

expect -c ”

set timeout 300 ;

spawn scp -P {port} -r {localsource} {username}@{host}:{remotedest} ;

expect *assword* {

{

{

{ send {password} }}}} ;

expect * ;

expect ;

expect eof

“””.format(username=user,password=password,host=ip,localsource=localsource,remotedest=remotedest,port=port)

SCP_CMD = SCP_CMD_BASE.format(localsource = localsource)

print “execute SCP_CMD: “,SCP_CMD

p = subprocess.Popen( SCP_CMD , stdout=subprocess.PIPE, s