zhouleilei的专栏

去年写了个python 版本的 hadoop 环境自动部署,,但虎头蛇尾,再也没有心情捡起来了,将其中一部分贴在博客上,有觉得有用的,拿去用吧,尽量完善一下 再次贴回来,大家共享一下 吧

我这个脚本基本上能完成 整个环境的ssh 配置,只需要一个配置文件几次输入密码

#!/usr/bin/python#filename easyhadoop1.pyimport osimport sysimport subprocessimport pexpectimport getpassimport shutiljava_source="jdk1.7.0"java_dest="/usr/java/"def buildSSH(username,psw):try:serverlist = open('server.txt','r')for server in serverlist:child=pexpect.spawn("ssh %s -l %s 'ssh-keygen -t rsa'"%(server.split('\t')[1].rstrip(),username))index=child.expect([pexpect.TIMEOUT,'Are you sure you want to continue connecting.*','password:'])if index==1:child.sendline('yes')index=child.expect([pexpect.TIMEOUT,'Are you sure you want to continue connecting.*','password:'])if index==2:child.sendline(psw)ret=child.expect([pexpect.TIMEOUT,'Enter file in which to save the key.* '])if ret==1:child.sendline()ret=child.expect([pexpect.TIMEOUT,'Overwrite.*','Enter passphrase.* '])if ret==1:child.sendline('y')ret=child.expect([pexpect.TIMEOUT,'Overwrite.*','Enter passphrase.* '])if ret==2:child.sendline()ret=child.expect([pexpect.TIMEOUT,'Enter same passphrase again: '])if ret==1:child.sendline()child.expect(pexpect.EOF)finally:serverlist.close()def copyPub2Auth(username,psw):try:serverlist=open('server.txt','r')for server in serverlist:# build authorized_keyschild=pexpect.spawn("ssh %s -l zhoulei 'cp .ssh/id_rsa.pub .ssh/authorized_keys'"%server.split('\t')[1])index=child.expect([pexpect.TIMEOUT,'Are you sure you want to continue connecting.*','password:'])if index==1:child.sendline('yes')index=child.expect([pexpect.TIMEOUT,'Are you sure you want to continue connecting.*','password:'])if index==2:child.sendline(psw)child.expect(pexpect.EOF)# change file modechild=pexpect.spawn("ssh %s -l %s 'chmod 600 .ssh/authorized_keys'"%(server.split('\t')[1],username))index=child.expect([pexpect.TIMEOUT,'Are you sure you want to continue connecting.*','password:'])if index==1:child.sendline('yes')index=child.expect([pexpect.TIMEOUT,'Are you sure you want to continue connecting.*','password:'])if index==2:child.sendline(psw)child.expect(pexpect.EOF)# cat file content to authorized_keysauth=open('../.ssh/authorized_keys','a+')child=pexpect.spawn("ssh %s -l %s 'cat .ssh/authorized_keys'"%(server.split('\t')[1],username))index=child.expect([pexpect.TIMEOUT,'Are you sure you want to continue connecting.*','password:'])if index==1:child.sendline('yes')index=child.expect([pexpect.TIMEOUT,'Are you sure you want to continue connecting.*','password:'])if index==2:child.sendline(psw)child.expect(pexpect.EOF)content=child.beforeauth.write(content.rstrip())auth.close()finally:serverlist.close()auth.close()def copyAuth2Server(username,psw):try:serverlist=open('server.txt','r')for server in serverlist:print "scp ../.ssh/authorized_keys %s@%s:~/.ssh/"%(username,server.split('\t')[1].rstrip())child=pexpect.spawn("scp ../.ssh/authorized_keys %s@%s:~/.ssh/"%(username,server.split('\t')[1].rstrip()))index=child.expect([pexpect.TIMEOUT,'Are you sure you want to continue connecting.*','password:'])if index==1:child.sendline('yes')index=child.expect([pexpect.TIMEOUT,'Are you sure you want to continue connecting.*','password:'])if index==2:child.sendline(psw)child.expect(pexpect.EOF)finally:serverlist.close()def buildKnowList():try:ex=os.path.exists(r'../.ssh/known_hosts')if ex==True:os.remove(r'../.ssh/known_hosts')serverlist=open('server.txt','r')namenode=""i=0for server in serverlist:if i==0:namenode=server.split('\t')[1].rstrip()print "ssh %s -l zhoulei -tt 'ssh %s'"%(namenode,server.split('\t')[1].rstrip())child=pexpect.spawn("ssh %s -l zhoulei -tt 'ssh %s'"%(namenode,server.split('\t')[1].rstrip()))index=child.expect([pexpect.TIMEOUT,'Are you sure you want to continue connecting.*','password:'])if index==2:child.sendline(psw)index=child.expect([pexpect.TIMEOUT,'Are you sure you want to continue connecting.*','password:'])if index==1:child.sendline('yes')i+=1finally:serverlist.close()def copyKnowList():try:serverlist=open('server.txt','r')namenode=""i=0for server in serverlist:if i==0:namenode=serverprint "scp ../.ssh/known_hosts %s@%s:~/.ssh/"%(username,server.split('\t')[1].rstrip())child=pexpect.spawn("scp ../.ssh/known_hosts %s@%s:~/.ssh/"%(username,server.split('\t')[1].rstrip()))index=child.expect([pexpect.TIMEOUT,'Are you sure you want to continue connecting.*','password:'])if index==1:child.sendline('yes')index=child.expect([pexpect.TIMEOUT,'Are you sure you want to continue connecting.*','password:'])if index==2:child.sendline(psw)child.expect(pexpect.EOF)i+=1finally:serverlist.close()def disableIpTable(rootpsw):try:serverlist=open('server.txt','r')for server in serverlist:child=pexpect.spawn("ssh %s 'service iptables stop'"%server.split('\t')[1].rstrip())index=child.expect([pexpect.TIMEOUT,'Are you sure you want to continue connecting.*','password:'])if index==1:child.sendline('yes')index=child.expect([pexpect.TIMEOUT,'Are you sure you want to continue connecting.*','password:'])if index==2:child.sendline(rootpsw)child.expect(pexpect.EOF)child=pexpect.spawn("ssh %s 'chkconfig –del iptables'"%server.split('\t')[1].rstrip())index=child.expect([pexpect.TIMEOUT,'Are you sure you want to continue connecting.*','password:'])if index==1:child.sendline('yes')index=child.expect([pexpect.TIMEOUT,'Are you sure you want to continue connecting.*','password:'])if index==2:child.sendline(rootpsw)child.expect(pexpect.EOF)if index==0:print 'timeout please check you root password'sys.exit(0)finally:serverlist.close()#def modifyUlimit():def buildEnvironment():if os.path.exists(java_source)==False:print "can't find java_source"return False;if os.path.exists(java_dest)==False:#path is not existsret=subprocess.call("mkdir %s"%java_dest,shell=True)if ret==0:ret=subprocess.call("cp -r %s %s"%(java_source,java_dest),shell=True)return Trueelif os.path.exists(java_dest+java_source)==True:#path is existsreturn Trueelse:# otherret=subprocess.call("cp -r %s %s"%(java_source,java_dest),shell=True)return Truedef copyBySSH(fr,to,psw):if os.path.exists(fr)==False:print "Can't find from path"else:print "scp -r %s %s"%(fr,to)child = pexpect.spawn("scp -r %s %s"%(fr,to))index=child.expect([pexpect.TIMEOUT,"password: "])#print indexif index==1:#print pswchild.sendline(psw)#child.expect(pexpect.EOF)else:return Falsedef echoProfile():print "echo 'JAVA_HOME=%s'>>/etc/profile"%(java_dest+java_source)ret = subprocess.call("echo 'JAVA_HOME=%s'>>/etc/profile"%(java_dest+java_source),shell=True)print "echo 'PATH=$JAVA_HOME/bin:$PATH'>>/etc/profile"ret = subprocess.call("echo 'PATH=$JAVA_HOME/bin:$PATH'>>/etc/profile")if ret == 0:ret = subprocess.call("ssh server -l source /etc/profile'",shell=True)return Trueelse:return Falseif __name__=="__main__":#sys.argv.lengthusername=raw_input('login username: ')psw=getpass.getpass("login password: ")i=0while username=='' or psw=='':print "Usage: must input you login username and password"username=raw_input('login username: ')psw=getpass.getpass("login password: ")i+=1if i>=2:print "Usage: error!,system.exit(0),must have username and password"sys.exit(0)rootpsw=getpass.getpass('root password:')while rootpsw=='':print "Usage: must input you root password"rootpsw=getpass.getpass("root password: ")i+=1if i>=2:print "Usage: error!,system.exit(0),must have root password"sys.exit(0)buildSSH(username,psw)copyPub2Auth(username,psw)copyAuth2Server(username,psw)buildKnowList()copyKnowList()disableIpTable(rootpsw)#modifyUlimit()#copyJDk()ret = buildEnvironment()if ret==False:sys.exit(0)fr=java_dest+java_sourcetry:serverlist = open("server.txt",'r')for server in serverlist:to=server.split('\t')[1].rstrip()+":"+java_destcopyBySSH(fr,to,rootpsw)#ret = echoProfile()#for server in serverlist:#print "server:"+server#fr="/etc/profile"#to=server.split('\t')[1].rstrip()+":"+"/etc/"#copyBySSH(fr,to)#for server in serverlist:#echoHosts(server)finally:serverlist.close()

因为冲动会做下让自己无法挽回的事情。

zhouleilei的专栏

相关文章:

你感兴趣的文章:

标签云: