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