美文网首页
通过selenium+python修改服务器带外hostname

通过selenium+python修改服务器带外hostname

作者: 讲武德的年轻人 | 来源:发表于2020-12-22 16:38 被阅读0次

chrome浏览器,webdirver版本需要和浏览器版本对应

  • think system sr650型号的:
    脚本如下:
from selenium import webdriver
import time


def main():
    chrome_driver = r'C:\Users\Administrator\Desktop\chromedriver_win32\chromedriver.exe'  #chromedriver的文件位置


    f = open('IpList.txt', 'r')
    for line in f.readlines():
        b = webdriver.Chrome(executable_path=chrome_driver)
        b.maximize_window()  # 浏览器窗口最大化
        line = line.strip('\n').split()

        b.get('https://{0[0]}/'.format(line))
        detailsButton = b.find_element_by_id('details-button')
        detailsButton.click()
        ProceedLink = b.find_element_by_id('proceed-link')
        ProceedLink.click()
        time.sleep(1)
        # 以上是pc服务器通用部分

        username = b.find_element_by_id('login_username')
        username.send_keys('admin')
        password = b.find_element_by_id('login_password')
        password.send_keys('xxx')
        time.sleep(1)
        ID_LOGON = b.find_element_by_id('login_right_submit_btn')
        ID_LOGON.click()
        # print('登录成功')
        time.sleep(8)

#-----------------------------------------------

        # 点击BMC Configuration
        b.find_element_by_xpath('//div[1]/div/ul/li[8]/a/div[2]').click()
        time.sleep(8)
        # 点击network
        b.find_element_by_xpath('//div[1]/div/ul/li[8]/ul/li[3]/a/div').click()
              #       '/html/body/div[4]/div[1]/div/ul/li[8]/ul/li[3]/a/div'
        # # 修改主机名
        time.sleep(2)
        dns_name = b.find_element_by_xpath('//div[1]/table[1]/tbody/tr/td/table[3]/tbody/tr[1]/td[1]/table/tbody/tr/td[1]/table/tbody/tr/td[2]/form/input')
        # dns_name = b.find_element_by_xpath('//div[1]/table[1]/tbody/tr/td/table[3]/tbody/tr[1]/td[1]/table/tbody/tr/td[1]/table/tbody/tr/td[2]/form/input')
        hostname0 = b.find_element_by_xpath('//div[1]/table[1]/tbody/tr/td/table[3]/tbody/tr[1]/td[1]/table/tbody/tr/td[1]/table/tbody/tr/td[2]/form/input').get_attribute('value')
        # print(hostname0)
        # print('-----------------------------')
        if hostname0 == line[1]:
            print(line[0]+'不需要修改')
            b.quit()
            continue
        else:
            # 清空原来的主机名
            dns_name.clear()
            time.sleep(5)
            # 填入要修改的主机名
            dns_name.send_keys(line[1])
            time.sleep(5)
            # 点击apply
            b.find_element_by_xpath(
                '//div[1]/table[1]/tbody/tr/td/table[3]/tbody/tr[6]/td/table/tbody/tr/td/button[1]').click()
            time.sleep(1)
            # 确认修改弹窗
            b.find_element_by_xpath('//div[1]/div[4]/div/div/div[3]/button[1]').click()
            print(line[0] +  '修改成功')
            time.sleep(5)

            b.quit()

if __name__ == '__main__':
    main()

IpList.txt文件格式如下:


  • ProLiant DL380 Gen9型号的
from selenium import webdriver
import time


def main():
    chrome_driver = r'C:\Users\Administrator\Desktop\chromedriver_win32\chromedriver.exe'  #chromedriver的文件位置
    b = webdriver.Chrome(executable_path = chrome_driver)

    f = open('hp_dl_380_gen9.txt', 'r')
    for line in f.readlines():
        line = line.strip('\n').split()

        b.get('https://{0[0]}/'.format(line))
        detailsButton = b.find_element_by_id('details-button')
        detailsButton.click()
        ProceedLink = b.find_element_by_id('proceed-link')
        ProceedLink.click()
        time.sleep(1)
        # username = b.find_element_by_id('usernameInput')
        b.switch_to.frame('modalFrame')
        username = b.find_element_by_xpath('//*[@id="usernameInput"]')
        username.send_keys('xxx')
        password = b.find_element_by_id('passwordInput')
        password.send_keys('xxx')
        time.sleep(1)
        ID_LOGON = b.find_element_by_id('ID_LOGON')
        ID_LOGON.click()

        #
        # b.switch_to.frame('frameDirectory')
        time.sleep(5)

        b.switch_to.default_content() #返回根frame
        b.switch_to.frame('appFrame')
        b.switch_to.frame('frameDirectory')
        # 点击network
        b.find_element_by_id('tree_net').click()
        # 点击iLO Dedicated Network Port
        b.find_element_by_id('tabset_dedicateNIC').click()
        #点击general
        b.switch_to.parent_frame() #返回上一级frame
        b.switch_to.frame('frameContent')
        b.find_element_by_id('tab_general').click()

        # 修改主机名
        b.switch_to.frame('iframeContent')
        dns_name = b.find_element_by_id('dns_name')

        dns_name.clear()
        time.sleep(1)
        dns_name.send_keys(line[1])
        time.sleep(1)
        b.find_element_by_id('submitButton').click()
        time.sleep(1)
        b.find_element_by_id('resetButton').click()
        time.sleep(1)
        b.switch_to.alert.accept()
        print(line[0]+'修改成功')
        time.sleep(5)
    # b.quit()
if __name__ == '__main__':
    main()


# selenium里面需要掌握的关于frame的三个用法:
# driver.switch_to.frame(reference)   进入某个frame
# driver.switch_to.parent_frame()     返回上一级frame
# driver.switch_to.default_content()  返回根frame

hp_dl_380_gen9.txt文件格式和IpList.txt相同。

完。

相关文章

网友评论

      本文标题:通过selenium+python修改服务器带外hostname

      本文链接:https://www.haomeiwen.com/subject/osmsnktx.html