import paramiko
import os
class Pre_root(object):
def __init__(self,hostname,username,password):
self.hostname=hostname
self.username=username
self.password=password
def sftp_put_core(self,localfile,remotefile):
try:
socks=(self.hostname,22)
t=paramiko.Transport((socks))
t.connect(username=self.username,password=self.password)
sftp=paramiko.SFTPClient.from_transport(t)
sftp.put(localfile,remotefile)
except Exception,e:
print 'upload file failed:',e
t.close()
finally:
t.close()
def remove_old_core(self):
paramiko.util.log_to_file('remove_old_core.log')
s=paramiko.SSHClient()
s.load_system_host_keys()
s.connect(self.hostname,22,self.username,self.password)
stdin,stdout,stderr=s.exec_command('mv /etc/sysctl.conf
/etc/sysctl.conf.old;mv /etc/security/limits.conf
/etc/security/limits.conf.old;mv /etc/pam.d/login
/etc/pam.d/login.old;mv /etc/sysctl.conf.new /etc/sysctl.conf;mv
/etc/security/limits.conf.new /etc/security/limits.conf;mv
/etc/pam.d/login.new /etc/pam.d/login');
stdout.read()
s.close()
def create_ora_user(self,__cmd):
try:
paramiko.util.log_to_file('create_ora_user.log')
s=paramiko.SSHClient()
s.load_system_host_keys()
s.connect(self.hostname,22,self.username,self.password)
stdin,stdout,stderr=s.exec_command(__cmd)
stdout.read()
except Exception,e:
print 'error:',e
s.close()
finally:
s.close()
if __name__=="__main__":
localfile1=r'/mypython/sysctl.conf'
localfile2=r'/mypython/limits.conf'
localfile3=r'/mypython/login'
remotefile1=r'/etc/sysctl.conf.new'
remotefile2=r'/etc/security/limits.conf.new'
remotefile3=r'/etc/pam.d/login.new'
cmd1='cd /;mkdir u01'
cmd2='groupadd oinstall;groupadd dba;groupadd oper'
cmd3='useradd -m -g oinstall -G dba oracle'
cmd4='cd /;chown -R oracle:oinstall /u01;chmod -R 755
/u01'
cmd5='cd /u01;mkdir oracle;mkdir soft;cd /u01/oracle;mkdir
11g;chown -R oracle:oinstall /u01/oracle;chown -R oracle:oinstall
/u01/oracle/11g;chown -R oracle:oinstall /u01/soft;chmod -R 755
/u01/oracle;chmod -R 755 /u01/oracle/11g;chmod -R 755
/u01/soft'
host=Pre_root('10.10.24.60','root','ocm123')
host.sftp_put_core(localfile1,remotefile1)
host.sftp_put_core(localfile2,remotefile2)
host.sftp_put_core(localfile3,remotefile3)
host.remove_old_core()
host.create_ora_user(cmd1)
host.create_ora_user(cmd2)
host.create_ora_user(cmd3)
host.create_ora_user(cmd4)
host.create_ora_user(cmd5)
本地/mypython目录下的sysctl.conf,limits.conf和login是已经编写好的内核参数文件,脚本执行时,这三个文件会被传送到CentOS的/etc,/etc/security和'/etc/pam.d下,并被重命名为sysctl.conf.new,limits.conf.new和login.new,然后利用mv命令进行重命名。该脚本同时创建了oisntall
dba组,Oracle用户和相关目录,需要注意的是,oracle用户创建完成后,需要在10.10.24.60主机上手动执行passwd命令设置密码,这也是该脚本的缺陷:无法为新用户创建密码。需要改进。
(三)oracle操作系统用户创建和相关目录,环境变量配置
oracle操作系统用户和目录已经在上面创建完成,只需要配置环境变量即可。方法亦是文件替换。
import paramiko
import os
class Pre_oracle(object):
def __init__(self,hostname,username,password):
self.hostname=hostname
self.username=username
self.password=password
def remove_old_oracle_profile(self,__cmd):
s.paramiko.SSHClient()
s.load_system_host_keys()
s.connect(hostname,port,username,password)
stdin,stdout,stderr=s.exec_command(_cmd);
print stdout.read()
s.close()
def sftp_put_oracle_profile(self,localfile,remotefile):
t=paramiko.Transport(hostname,port)
t.connect(username=username,password=password)
sftp=paramikio.SFTPClient.from_transport(t)
sftp.put(localfile,remotefile)
t.close()
if __name__=="__main__":
localfile=r'/u01/oracle/bash_profile.n'
remotefile=r'/home/oracle/bash_profile.n'
cmd='cd /home/oracle;mv .bash_profile .bash_profile.old;mv
bash_profile.n .bash_profile'
host=Pre_oracle('10.10.24.60','oracle','oracle')
host.sftp_put_oracle_profile(localfile,remotefile)
host.remove_old_oracle_profile(cmd)
该脚本用oracle用户执行。脚本执行完成后,可以用echo $ORACLE_BASE命令查看环境变量。