Hadoop Automation Deployment A

Source: Internet
Author: User
Tags auth iptables ssh server

Last year wrote a python version of the Hadoop environment automatic deployment, but the end of the run, no longer in the mood to pick up, will be part of the blog, some feel useful, take to use it, as far as possible to improve the post back again, we share it

This script basically completes the SSH configuration for the entire environment, and requires only one profile to enter the password several times


#!/usr/bin/python #filename easyhadoop1.py import OS import sys import subprocess import pexpect import getpass Import Shu Til java_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 (), US Ername)) Index=child.expect ([Pexpect. TIMEOUT, ' Are you sure your want to continue connecting.* ', ' Password: '] if index==1:child.sendline (' Yes ') index =child.expect ([Pexpect. TIMEOUT, ' Are you sure your want to continue connecting.* ', ' Password: '] if Index==2:child.sendline (PSW) re T=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.sendl INE () Child.expect (pexpect. EOF) Finally:serverlist.close () def copypub2auth (USERNAME,PSW): Try:serverlist=open (' Server.txt ', ' R ') for serve R in ServerList: # Build Authorized_keys child=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 your want to continue connecting.* ', ' Password: '] if index==1:child.sendline (' Yes ') index =child.expect ([Pexpect. TIMEOUT, ' Are you sure your want to continue connecting.* ', ' Password: '] if Index==2:child.sendline (PSW) child.e Xpect (Pexpect. EOF) # Change file mode child=pexpect.spawn (ssh%s-l%s ' chmod Ssh/authorizEd_keys ' "% (server.split (' t ') [1],username)] Index=child.expect ([Pexpect. TIMEOUT, ' Are you sure your want to continue connecting.* ', ' Password: '] if index==1:child.send Line (' Yes ') index=child.expect ([Pexpect. TIMEOUT, ' Are you sure your want to continue connecting.* ', ' Password: '] if index==2:child.send Line (PSW) child.expect (pexpect. EOF) # Cat file content to Authorized_keys Auth=open ('.. /.ssh/authorized_keys ', ' + ') child=pexpect.spawn ("ssh%s-l%s ' Cat. Ssh/authorized_keys '"% (server.split (' \ t ') [1], username)) Index=child.expect ([Pexpect.
                                TIMEOUT, ' Are you sure your want to continue connecting.* ', ' Password: '] if index==1:child.sendline (' Yes ') Index=child.expect ([Pexpect. TIMEOUT, ' Are you sure your want to continue connecting.* ', ' Password: '] if Index==2:child.sendline (PSW) child.e Xpect (Pexpect. EOF) Content=child.before Auth.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 your want to continue connecting.* ', ' Password: '] if index==1:child.sendline (' Yes ') index =child.expect ([Pexpect. TIMEOUT, ' Are you sure your want to continue connecting.* ', ' Password: '] if Index==2:child.sendline (PSW) child.e Xpect (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=0 for server in SE Rverlist: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 your want to continue connecting.* ', ' Password: '] if Index==2:chi Ld.sendline (PSW) index=child.expect ([Pexpect.
                                TIMEOUT, ' Are you sure your want to continue connecting.* ', ' Password: '] if index==1:
		Child.sendline (' yes ') i+=1 finally:serverlist.close () def copyknowlist (): Try:  Serverlist=open (' Server.txt ', ' R ') namenode= "" I=0 for server in Serverlist:if i==0:namenode=server print "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 your want to continue connecting.* ', ' Password: '] if index==1:child.sendline (' Yes ') index =child.expect ([Pexpect. TIMEOUT, ' Are you sure your want to continue connecting.* ', ' Password: '] if Index==2:child.sendline (PSW) child.e Xpect (Pexpect. EOF) I+=1 finally: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=chi Ld.expect ([Pexpect. TIMEOUT, ' Are you sure your want to continue connecting.* ', ' Password: '] if index==1:child.sendline (' Yes ') index =child.expect ([Pexpect. TIMEOUT, ' Are you sure your want to continue connecting.* ', ' Password: '] if Index==2:child.sendline (ROOTPSW) Chi Ld.expect (Pexpect. EOF) child=pexpect.spawn ("ssh%s ' chkconfig--del iptables '"%server.split (' \ t ') [1].rstrip()) Index=child.expect ([Pexpect.
                                TIMEOUT, ' Are you sure your want to continue connecting.* ', ' Password: '] if index==1: Child.sendline (' yes ') index=child.expect ([Pexpect.
                                TIMEOUT, ' Are you sure your want to continue connecting.* ', ' Password: '] if index==2: Child.sendline (ROOTPSW) child.expect (pexpect. EOF) If Index==0:print ' timeout please check your 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 exists Ret=subprocess.call ("mkdir%s"%java_dest,shell=true) if ret==0 : Ret=subprocess.call ("cp-r%s"% (java_source,java_dest), shell=true) return True elif os.path.exists (java_dest+ja Va_source) ==true: #path is exists return True else:# the other Ret=subprocess.call ("Cp-r%s"% (java_source,java_dest), she Ll=true) return True def copybyssh (FR,TO,PSW): if Os.path.exists (FR) ==false:print "Can ' t find from Path" else:p Rint ' scp-r%s '% (fr,to) child = Pexpect.spawn ("scp-r%s"% (fr,to)) Index=child.expect ([Pexpect. TIMEOUT, "Password:"]) #print index if index==1: #print psw child.sendline (PSW) #child. Expect (pexpect. 
	EOF) Else:return False def 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 True else: return False if __name__== "__main__": #sys. Argv.length USErname=raw_input (' Login username: ') psw=getpass.getpass ("Login password:") i=0 while username== ' or psw== ': print "Usage:must Input you login username and password" username=raw_input (' Login username: ') psw=getpass.getpass ("Login Password:") i+=1 if I>=2:print "usage:error!,system.exit (0), must have and username" password
        	XIT (0) rootpsw=getpass.getpass (' Root password: ') while rootpsw== ': print ' usage:must input for you root password ' Rootpsw=getpass.getpass ("Root password:") i+=1 if I>=2:print "usage:error!,system.exit (0), must have root Password "sys.exit (0) buildssh (USERNAME,PSW) Copypub2auth (USERNAME,PSW) copyauth2server (USERNAME,PSW) BuildKnow
		List () copyknowlist () disableiptable (ROOTPSW) #modifyUlimit () #copyJDk () ret = Buildenvironment () if Ret==false: Sys.exit (0) Fr=java_dest+java_source try:serverlist = open ("Server.txt", ' R ') for server in Serverlist:to=server . Split (' t ') [1].rstrip ()+ ":" +java_dest copybyssh (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 ()


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.