|
马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有帐号?立即注册
x
小知识:CentOS来自于RedHatEnterpriseLinux依照开放源代码规定释出的源代码所编译而成。
用python完成长途上岸主机履行敕令或经由过程sftp上传下载文件,有个很好的模块paramiko模块来演示这些功效,应用起来很便利,人人可进修一下。写了几个小法式,用于解释此模块的应用办法。
1:衔接长途linux主机并履行敕令
- #!/usr/bin/envpythonimportparamikohostname=192.168.0.102username=rootpassword=abcport=22paramiko.util.log_to_file(paramiko.log)s=paramiko.SSHClient()s.set_missing_host_key_policy(paramiko.AutoAddPolicy())s.connect(hostname=hostname,port=port,username=username,password=password)stdin,stdout,stderr=s.exec_command(free;df-h)printstdout.read()s.close()
复制代码
履行成果以下:
- totalusedfreesharedbufferscachedMem:20749402057420175200424161867968-/+buffers/cache:1470361927904Swap:20964722402096232FilesystemSizeUsedAvailUse%Mountedon/dev/sda130G12G17G42%/none1014M01014M0%/dev/shm/dev/sda32.0G289M1.6G16%/var/dev/sdb1135G14G115G11%/data/dev/sdc1135G127G880M100%/data1/dev/sdd1135G99G30G78%/data2
复制代码
2:衔接长途linux主机上传下载文件(paramiko模块是用SFTP协定来完成的)
- #!/usr/bin/envpythonimportparamiko,datetime,oshostname=192.168.0.102username=rootpassword=abc123port=22local_dir=/tmp/remote_dir=/tmp/test/try:t=paramiko.Transport((hostname,port))t.connect(username=username,password=password)sftp=paramiko.SFTPClient.from_transport(t)#files=sftp.listdir(dir_path)files=sftp.listdir(remote_dir)forfinfiles:printprint#########################################printBeginningtodownloadfilefrom%s%s%(hostname,datetime.datetime.now())printDownloadingfile:,os.path.join(remote_dir,f)sftp.get(os.path.join(remote_dir,f),os.path.join(local_dir,f))#下载#sftp.put(os.path.join(local_dir,f),os.path.join(remote_dir,f))#上传printDownloadfilesuccess%s%datetime.datetime.now()printprint##########################################t.close()exceptException:print"connecterror!"
复制代码
履行成果:
- #########################################Beginningtodownloadfilefrom192.168.0.1022012-11-0515:49:01.334686Downloadingfile:/tmp/test/wgetrcDownloadfilesuccess2012-11-0515:49:05.955184###################################################################################Beginningtodownloadfilefrom192.168.0.1022012-11-0515:49:05.955342Downloadingfile:/tmp/test/xinetd.confDownloadfilesuccess2012-11-0515:49:10.929568###################################################################################Beginningtodownloadfilefrom192.168.0.1022012-11-0515:49:10.929740Downloadingfile:/tmp/test/warnquota.confDownloadfilesuccess2011-12-0515:49:14.213570##########################################
复制代码
还有很多多少用法,详细的可以看官方文档:http://www.lag.net/paramiko/docs/
小知识:对CentOS提供支持将是OpenLogic首次支持一个完整的Linux操作系统。 |
|