import pexpect
import datetime
import sys,re
AUTO_USER = "emc"
SUDO_PASSWORD = "Password123!"
LOCAL_DIR = "/home/"
LOG_FILE = 'trace.log'
def print_msg(msg):
print "*"*6 + msg +"*"*6
def sudo_command(cmd_line):
fdebug = open (LOG_FILE, "ab")
print_msg(cmd_line + "start")
fdebug.write ('%s\n' %(datetime.datetime.now()))
p = pexpect.spawn(cmd_line, logfile = fdebug)
index = p.expect([r'password', r'DO you want to continue',r'Are you sure you want to continue connecting ', pexpect.EOF])
if index==0:
p.sendline(SUDO_PASSWORD)
index = p.expect([r'password', r'DO you want to continue',r'Are you sure you want to continue connecting ', pexpect.EOF])
if index==0:
p.sendline('y\n')
index=p.expect([r'password', pexpect.EOF])
if index ==0:
p.sendline(SUDO_PASSWORD)
if index ==1:
p.sendline('y\n')
index=p.expect([r'password', pexpect.EOF])
if index ==0:
p.sendline(SUDO_PASSWORD)
elif index==2:
p.sendline("yes\n")
index=p.expect([r'password', pexpect.EOF])
if index ==0:
p.sendline(SUDO_PASSWORD)
elif index==2:
print_msg(cmd_line+ " Passed!")
r = p.read()
print_msg(r)
elif index==1:
p.sendline('y\n')
elif index==2:
p.sendline("yes\n")
index=p.expect([r'password', pexpect.EOF])
if index ==0:
p.sendline(SUDO_PASSWORD)
else:
print_msg(cmd_line + " failed!")
return False
index=p.expect(pexpect.EOF)
if index == 0:
print_msg(cmd_line)
return True
def hosts_setting(device_num, slot):
try:
sudo_command("sudo cp hosts /etc/hosts")
p = popen('sudo cat /etc/hosts').read()
r = re.findall(r'192.168.10(.*?).101 psc(.*?).localdomain.local',p)
sudo_command("sudo sed -i -e 's/%s/%s/g' -e 's/10%s\./10%s./g' /etc/hosts" % (r[1],device_num,r[0],slot))
return True
except:
print "pxssh failed on login."
return "False"
def dns_setting(device_num, slot):
try:
if install_bind():
sudo_command("sudo cp db.localdomian.local /var/cache/bind/.")
p = popen('sudo cat /var/cache/bind/.').read()
r = re.findall(r'vc(.*?)\s+I.*?192.168.10(.*?)\.',p)
sudo_command("sudo sed -i -e 's/%s/%s/g' -e 's/10%s\./10%s./g' /var/cache/bind/db.localdomain.local" % (r[0],device_num,r[1],slot))
sudo_command("sudo /etc/init.d/bind9 restart")
return True
except :
print "pxssh failed on login."
return "False"
if len(sys.argv) != 4:
print ("parameter is not enough")
elif sys.argv[0] != "0" and sys.argv[2] != "SLOT":
print """format is not correct, the right format is 'python dns_setting.py DEVICE_NUM 31 SLOT 1'"""
else:
DEVICE_NUM = sys.argv[1]
SLOT = sys.argv[3]
dns_setting(DEVICE_NUM, SLOT)
hosts_setting(DEVICE_NUM, SLOT)