利用paramiko模块批量登录(执行命令/上传文件)
工作中由于服务器主机很多,如果手动的一台一台去添加ssh认证,效率太低了,而此脚本正是为了解决此问题 此脚本的实现的功能: 1、实现了(密码、ssh认证)单一主机登录和批量主机登录 2、实现了(密码、ssh认证)单一主机上传文件和批量主机上传文件(下载文件的原理和此一样) 3、主机批量添加ssh认证(这才是我的主要目的) 脚本的不足: 1、只能循环主机名 2、所有的主机的账号和密码都是一样的,不够灵活 有需求的朋友可以修改下代码,可以把主机、账号密码存放在一个文件中,循环读取文件 下面贴上代码吧 如下代码有错 欢迎各位纠正 有错才有进步 你们的指点是我进步的源泉 #!/usr/bin/python #coding:utf-8 #需要安装paramiko模块 #yuminstallpython-paramiko-y importparamiko,os classSSH2: def__init__(self,hostname=None,username=None,password=None,key_file=None,port=22,timeout=30): self.h=hostname self.u=username self.p=password self.t=timeout self.port=int(port) self.key_file=key_file #使用密码登录 defssh2_pass(self,command=None): paramiko.util.log_to_file('/tmp/paramiko.log') ssh2=paramiko.SSHClient() ssh2.load_system_host_keys() ssh2.set_missing_host_key_policy(paramiko.AutoAddPolicy()) ssh2.connect(hostname=self.h,username=self.u,password=self.p,port=self.port,timeout=self.t) stdin,stdout,stderr=ssh2.exec_command(command) print'{0}\t{1}'.format(self.h,command) printstdout.read() ssh2.close() #使用key密钥登录 defssh2_key(self,command=None): paramiko.util.log_to_file('/tmp/paramiko.log') key=paramiko.RSAKey.from_private_key_file(self.key_file) ssh2=paramiko.SSHClient() ssh2.load_system_host_keys() ssh2.set_missing_host_key_policy(paramiko.AutoAddPolicy()) ssh2.connect(hostname=self.h,username=self.u,pkey=key,port=self.port,timeout=self.t) stdin,stdout,stderr=ssh2.exec_command(command) print'{0}\t{1}'.format(self.h,command) printstdout.read() ssh2.close() #使用密码上传文件 defsftp_pass_put(self,localpath=None,remotepath=None): t=paramiko.Transport((self.h,self.port)) t.connect(username=self.u,password=self.p) sftp=paramiko.SFTPClient.from_transport(t) sftp.put(localpath=localpath,remotepath=remotepath) printself.h,localpath t.close() #使用key密钥上传文件 defsftp_key_put(self,localpath=None,remotepath=None): key=paramiko.RSAKey.from_private_key_file(self.key_file) t=paramiko.Transport((self.h,self.port)) t.connect(username=self.u,pkey=key) sftp=paramiko.SFTPClient.from_transport(t) sftp.put(localpath=localpath,remotepath=remotepath) printself.h,localpath t.close() if__name__=='__main__': ###################使用密码登录主机############################################################# #远程服务器IP地址 #hostname='192.168.31.102' #远程服务器的账号和密码 #username='xiaomo' #password='xiaomo' #SSH=SSH2(hostname=hostname,username=username,password=password,port=22) #SSH.ssh2_pass(command='df-h') ###################使用密钥登录主机############################################################# #远程服务器IP地址 #hostname='192.168.31.102' #远程服务器的账号 #username='xiaomo' #指定密钥文件的位置 #key_file='/home/xiaomo/.ssh/id_rsa' #SSH=SSH2(hostname=hostname,username=username,key_file=key_file,port=22) #SSH.ssh2_pass(command='df-h') ###################使用密码上传文件############################################################# #远程服务器IP地址 #hostname='192.168.31.102' #远程服务器的账号和密码 #username='xiaomo' #password='xiaomo' #本地文件绝对路径 #密钥需要自己ssh-keygen生成 #local='/home/xiaomo/.ssh/id_rsa.pub' #远程服务器文件绝对路径 #remote='/tmp/id_rsa.pub' #SSH=SSH2(hostname=hostname,username=username,password=password,port=22) #SSH.sftp_pass_put(localpath=local,remotepath=remote) #SSH.ssh2_pass(command='ls-l{0}'.format(remote)) ###################使用密钥上传文件############################################################# #远程服务器IP地址 #hostname='192.168.31.102' #远程服务器的账号 #username='xiaomo' #指定密钥文件的位置 #key_file='/home/xiaomo/.ssh/id_rsa' #本地文件绝对路径 #local='/home/xiaomo/.ssh/id_rsa.pub' #远程服务器文件绝对路径 #remote='/tmp/id_rsa.pub' #SSH=SSH2(hostname=hostname,username=username,port=22,key_file=key_file) #SSH.sftp_key_put(localpath=local,remotepath=remote) #SSH.ssh2_key(command='ls-l{0}'.format(remote)) #####################使用密码批量添加用户认证############################################################ #hostname=['192.168.31.101','192.168.31.102','192.168.31.103'] #远程服务器的账号和密码 #username='xiaomo' #password='xiaomo' #本地文件绝对路径 #local='/home/xiaomo/.ssh/id_rsa.pub' #远程服务器文件绝对路径 #remote='/tmp/id_rsa.pub' #批量上传密钥文件并添加认证 #如果.ssh目录或authorized_keys文件事先存在,请确认权限是否正确 #forhinhostname: #SSH=SSH2(hostname=h,username=username,password=password,port=22,timeout=30) ##/home/xiaomo/.ssh/是远程服务器的用户目录 #SSH.ssh2_pass(command='(umask077;test-d/home/xiaomo/.ssh||mkdir/home/xiaomo/.ssh)') #SSH.sftp_pass_put(localpath=local,remotepath=remote) #SSH.ssh2_pass(command='(umask077;cat{0}>>/home/xiaomo/.ssh/authorized_keys&&rm-f{0})'.format(remote)) #SSH.ssh2_pass(command='cat/home/xiaomo/.ssh/authorized_keys') ##################使用密钥批量上传文件################################################################### #hostname=['192.168.31.101','192.168.31.102','192.168.31.103'] #远程服务器的账号 #username='xiaomo' #指定密钥文件的位置 #key_file='/home/xiaomo/.ssh/id_rsa' #本地文件绝对路径 #local='/home/xiaomo/.ssh/id_rsa.pub' #远程服务器文件绝对路径 #remote='/tmp/id_rsa.pub' #forhinhostname: #SSH=SSH2(hostname=h,username=username,port=22,key_file=key_file) #SSH.sftp_key_put(localpath=local,remotepath=remote) #SSH.ssh2_key(command='ls-l{0}'.format(remote)) #############################################################################################