dns_setting


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)


    
    
    
    
    
    

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值