#!/usr/bin/env python
# coding=utf-8
import paramikoimport sys
####定义私钥key 的位置
pkey_file = '/usr/local/shell/id_rsa'
def sftp(username,port,host,pkey_file,localefile,remotefile):
key = paramiko.RSAKey.from_private_key_file(pkey_file)
t = paramiko.Transport((hostname, port))
t.connect(username=username,pkey=key)
sftp = paramiko.SFTPClient.from_transport(t)
sftp.put(localfile,remotefile)
sftp.close()
t.close
def ssh (hostname,port,username,pkey_file,cmd):
key = paramiko.RSAKey.from_private_key_file(pkey_file)
s = paramiko.SSHClient()
s.load_system_host_keys()
s.set_missing_host_key_policy(paramiko.AutoAddPolicy())
s.connect(hostname,port,username,pkey=key)
stdin,stdout,stderr = s.exec_command(cmd)
for std in stdout.readlines():
print std,
s.close()
转载于:https://my.oschina.net/u/142602/blog/191822