标签:
一、基于用户名密码认证SSH连接
#!/usr/bin/env python #coding:utf8 import paramiko ssh = paramiko.SSHClient()#创建SSH对象 ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())#允许连接不在know_hosts文件中的主机 ssh.connect(hostname=‘172.16.230.95‘,username=‘root‘,password=‘123456‘)#SSH连接远程服务器 stdin,stdout,stderr=ssh.exec_command(‘df -h‘)#执行的命令 result=stdout.read() print result #打印执行结果 ssh.close()#关闭SSH连接
二、公钥秘钥SSH连接
#!/usr/bin/env python #coding:utf8 import paramiko private_key=paramiko.RSAKey.from_private_key(‘/root/.ssh/id_rsa‘) ssh=paramiko.SSHClient() ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy()) ssh.connect(hostname=‘172.16.230.95‘,username=‘root‘,pkey=private_key)#SSH连接远程服务器 stdin,stdout,stderr=ssh.exec_command(‘df -h‘)#执行的命令 result=stdout.read() print result #打印执行结果 ssh.close()#关闭SSH连接
三、文件上传和下载
#!/usr/bin/env python #coding:utf8 import paramiko ‘‘‘ private_key = paramiko.RSAKey.from_private_key_file(‘/root/.ssh/id_rsa‘) transport = paramiko.Transport((‘172.16.230.95‘,22)) transport.connect(username=‘root‘,pkey=private_key) 公钥秘钥认证 ‘‘‘ transport = paramiko.Transport((‘172.16.230.95‘,22)) #用户名密码认证 transport.connect(username=‘root‘,password=‘123456‘) sftp = paramiko.SFTPClient.from_transport(transport) # 123.py 上传至服务器 /tmp/test.py sftp.put(‘/tmp/123.py‘, ‘/tmp/test.py‘) # 将remote_path 下载到本地 local_path sftp.get(‘remote_path‘, ‘local_path‘) transport.close()
四、一个完整的例子
#!/usr/bin/env python #coding:utf8 import paramiko import threading import time import sys class Server_Run(threading.Thread): def __init__(self,argv,ip): threading.Thread.__init__(self) self.username=argv[‘username‘] self.password=argv[‘password‘] if argv.get(‘local_file‘) and argv.get(‘remote_file‘): self.local_file=argv[‘local_file‘]#本地文件 self.remote_file=argv[‘remote_file‘]#远程文件 self.motion=argv[‘motion‘]#执行的动作 put还是get else: self.motion=‘‘ if argv.get(‘cmd‘): self.cmd=argv[‘cmd‘]#命令 else: self.cmd=‘‘ self.group=argv[‘group‘]#服务组 self.server_ip=ip#服务器IP def conns(self): self.transport = paramiko.Transport((self.server_ip,22)) self.transport.connect(username=self.username,password=self.password) self.ssh = paramiko.SSHClient() self.ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy()) self.ssh._transport = self.transport #使用反射解析字符串并调用方法 def argv_parse(self): if hasattr(self,self.motion): func=getattr(self,self.motion) func() #执行远端命令 def ssh_cmd(self): stdin, stdout, stderr = self.ssh.exec_command(self.cmd) data=stdout.read() if data: print "\033[32;1m%s execution [%s] command to succeed\033[0m"%(self.server_ip,self.cmd) print "[%s]----result----:\n%s"%(self.server_ip,data) if stderr: print ("\033[31;1m %s\033[0m"%stderr.read()) #文件上传 def put(self): sftp = paramiko.SFTPClient.from_transport(self.transport) sftp.put(self.local_file, self.remote_file) #文件下载 def get(self): sftp = paramiko.SFTPClient.from_transport(self.transport) sftp.get(self.remote_file, self.local_file) #重写run方法 def run(self): paramiko.util.log_to_file(‘paramiko.log‘) try: self.conns() except paramiko.SSHException,AttributeError : print ‘[%s] connection timeout‘%self.server_ip sys.exit() if self.cmd: self.ssh_cmd() self.argv_parse() self.conn_close() #关闭连接 def conn_close(self): self.transport.close() if __name__==‘__main__‘: server_ip=[‘172.16.230.95‘] response={ ‘username‘:‘root‘,‘password‘:‘password‘, ‘local_file‘:‘/Users/xym/install.log‘, ‘remote_file‘:‘/root/install.log‘, ‘cmd‘:‘df -h‘, ‘group‘:‘web‘, ‘motion‘:‘get‘, } for ip in server_ip:#循环IP server=Server_Run(response,ip)#实例化类把argv和IP传入 server.start()#根据IP个数开启并执行多线程
标签:
原文地址:http://www.cnblogs.com/xuyanmei/p/5305367.html