Example of transferring files based on SSH protocol

Source: Internet
Author: User

Using the Python module: paramiko,optparse

__author__ = ' Administrator '
Import Paramiko
Import Sys
From Optparse import Optionparser
Import OS
def exit_code (code):
Os.system (' pause ')
Sys.exit (Code)
def uploadfile (LOCALFILE,IP):
If not os.path.exists (LocalFile):
print '%s not exists! '% LocalFile
Exit_code (10)
Root_path = '/root/'
RemoteFile = Os.path.basename (LocalFile)
t = Paramiko. Transport ((ip,22))
T.connect (username= ' root ', password= ' Yunwei ')
SFTP = Paramiko. Sftpclient.from_transport (t)
Sftp.put (LocalFile, '%s%s '% (root_path,remotefile))
T.close ()
def main ():
Usage = "Usage:%prog [options] arg"
Parser = Optionparser (usage)
Parser.add_option ("-F", "--file", dest= "file",
help= "File path for transport")
Parser.add_option ("-V", "--verbose",
Action= "Store_true", dest= "verbose")
Parser.add_option ("-Q", "--quiet",
Action= "Store_false", dest= "verbose")
Parser.add_option ("-H", "--host", dest= ' IP ', help= "The IP address of remote Host")
(Options,args) = Parser.parse_args ()
If Options.verbose:
Print "Reading%s ..."% Options.file
If not options.file or not OPTIONS.IP:
Parser.error ("Incorrect number of argument!")
Exit_code (10)
UploadFile (OPTIONS.FILE,OPTIONS.IP)

if __name__ = = "__main__":
Main ()

Example of transferring files based on SSH protocol

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.