Python paramiko模块
paramiko模块用于对远程服务器的操作。
一、Ubuntu 14.04下安装 paramiko包:
1、首先安装Crypto包:
sudo apt-get install python-crypto
测试安装:
python>>> import Crypto
2、安装paramiko包:
sudo apt-get install python-paramiko
测试安装:
python>>> import paramiko
二、在win7远程连接ubuntu获取ubuntu默认网关:(只需在win7安装paramiko)
#-*- coding:utf-8 -*- import paramiko ssh = paramiko.SSHClient() ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy()) ssh.connect("192.168.159.133",22,"luo","luoda") stdin,stdout,stderr = ssh.exec_command("route -n| awk '{print $1,$2}'") for i in stdout.readlines(): item = i.split() if item[0]=="0.0.0.0" and len(item)==2: print "Gateway:",item[1] ssh.close()
结果:
三、上传文件到远程
tsp = paramiko.Transport(("192.168.159.133",22)) tsp.connect(username="luo",password="luoda") sftp = paramiko.SFTPClient.from_transport(tsp) remotepath = '/home/luo/protocol.dict' sftp.put(localpath, remotepath) tsp.close()
remotepath 为绝对路径,当文件已经存在的时候,会覆盖原文件。
下载与上传相反,将localpath和remotepath换位即可