欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

基于Python Shell獲取hostname和fqdn釋疑

 更新時(shí)間:2016年01月25日 10:08:02   投稿:mrr  
一直以來(lái)被linux的hostname和fqdn(Fully Qualified Domain Name)困惑著,今天通過(guò)腳本之家平臺(tái)把它們使用細(xì)節(jié)弄清分享給大家

一直以來(lái)被Linux的hostname和fqdn(Fully Qualified Domain Name)困惑了好久,今天專門抽時(shí)間把它們的使用細(xì)節(jié)弄清了。

一、設(shè)置hostname/fqdn

在Linux系統(tǒng)內(nèi)設(shè)置hostname很簡(jiǎn)單,如:

$ hostname florian

如果要設(shè)置fqdn的話,需要對(duì)/etc/hosts進(jìn)行配置。

$ cat /etc/hosts
127.0.0.1 localhost
192.168.1.1 florian.test.com florian 

/etc/hosts配置文件的格式是:

ip fqdn [alias]...

即第一列為主機(jī)ip地址,第二列為主機(jī)fqdn地址,第三列以后為別名,可以省略,否則至少要包含hostname。

上述配置文件的配置項(xiàng)的第一行為localhost的配置,第二行為主機(jī)名florian配置fqdn=florian.test.com,ip=192.168.1.1。
至于fqdn的域名后綴,最好和文件/etc/sysconfig/network的HOSTNAME配置保持一致:

$ cat /etc/sysconfig/network
NETWORKING=yes
HOSTNAME=test.com

二、查看hostname/fqdn

配置完成后,可以使用shell命令查看hostname和fqdn:

$ hostname && hostname -f 
florian
florian.test.com 

使用ping去測(cè)試hostname的ip映射是否成功。

$ ping florian
PING florian.test.com (192.168.1.1) 56(84) bytes of data.
$ ping florian.test.com
PING florian.test.com (192.168.1.1) 56(84) bytes of data. 

也可以使用python命令獲取hostname和fqdn。

$ python 
Python 2.6.6 (r266:84292, Dec 7 2011, 20:48:22) 
[GCC 4.4.6 20110731 (Red Hat 4.4.6-3)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import socket
>>> socket.gethostname()
'florian'
>>> socket.getfqdn() 
'florian.test.com' 

三、使用ip設(shè)置hostname帶來(lái)的fqdn問(wèn)題

以上描述了正常設(shè)置hostname和fqdn的方法,但是有時(shí)會(huì)使用ip地址直接作為hostname,此時(shí)會(huì)有些不同。

$ hostname 192.168.1.1
$ hostname && hostname -f
192.168.1.1
192.168.1.1 

我們發(fā)現(xiàn)使用ip作為hostname后,使用shell命令查詢hostname和fqdn都是ip地址!??!這是因?yàn)镈NS協(xié)議會(huì)解析hostname的內(nèi)容,當(dāng)發(fā)現(xiàn)其為ip地址時(shí),則不會(huì)再去查詢/etc/hosts文件。

再使用python查看一下,會(huì)發(fā)現(xiàn)python獲取的fqdn竟然還是florian.test.com?。?!

$ python
Python 2.6.6 (r266:84292, Dec 7 2011, 20:48:22) 
[GCC 4.4.6 20110731 (Red Hat 4.4.6-3)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import socket 
>>> socket.gethostname()
'192.168.1.1'
>>> socket.getfqdn()
'florian.test.com' 

即便是刷新dns緩存也無(wú)濟(jì)于事:

$ service nscd reload

將/etc/hosts文件的第二行注釋:

cat /etc/hosts
127.0.0.1 localhost
# 192.168.1.1 florian.test.com florian 

刷新dns緩存:

$ service nscd reload

我們發(fā)現(xiàn)fqdn恢復(fù)正常了。

$ python
Python 2.6.6 (r266:84292, Dec 7 2011, 20:48:22) 
[GCC 4.4.6 20110731 (Red Hat 4.4.6-3)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import socket 
>>> socket.gethostname()
'192.168.1.1'
>>> socket.getfqdn()
'192.168.1.1' 

之所以會(huì)有這樣的行為,是因?yàn)閜ython解析fqdn的邏輯和DNS并不完全一致,它會(huì)根據(jù)hostname查詢對(duì)應(yīng)的ip地址,然后在/etc/hosts內(nèi)獲取ip地址對(duì)應(yīng)的配置行(第一行有效),然后解析fqdn列和alias列,并返回第一個(gè)包含字符'.'的對(duì)應(yīng)列的值。

因此,使用ip設(shè)置hostname時(shí),需要注意兩點(diǎn):

•首先,將hostname設(shè)置為ip地址
•其次,將/etc/hosts內(nèi)包含該ip的配置項(xiàng)移除

為了保險(xiǎn)起見(jiàn),我們可以在/etc/hosts內(nèi)盡可能靠前的位置添加如下配置:

cat /etc/hosts
127.0.0.1 localhost
192.168.1.1 192.168.1.1 

這樣,即便是之后有包含該ip的配置項(xiàng)也不會(huì)生效,python會(huì)優(yōu)先解析第二行的配置項(xiàng),并獲取和ip地址完全一樣的fqdn地址。當(dāng)然,使用shell命令hostname獲取fqdn也不會(huì)出錯(cuò),因?yàn)閔ostname已經(jīng)被設(shè)為ip地址形式了。

下面給大家介紹python shell 根據(jù)ip 獲取 hostname || 根據(jù)hostname 獲取 ip

利用 socket 模塊 里的 gethostbyname 函數(shù)

<code class="hljs perl has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;">>>> import <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">socket</span> >>> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">socket</span>.<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">gethostbyname</span>(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"www.baidu.com"</span>) <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">'61.135.169.125'</span> >>> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">socket</span>.<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">gethostbyname</span>(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"rs.xidian.edu.cn"</span>) <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">'202.117.119.1'</span></code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li></ul>

方法2 利用 shell 中 hostname 命令

<code class="hljs livecodeserver has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;">def getHostName(ip): <span class="hljs-command" style="box-sizing: border-box;"><span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">command</span> = <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">'java -jar %s %s "hostname > %s.hostname"'</span> %(<span class="hljs-title" style="box-sizing: border-box;">remoteCmdLoca</span>,<span class="hljs-title" style="box-sizing: border-box;">ip</span>,<span class="hljs-title" style="box-sizing: border-box;">ip</span>)</span> <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">result</span> = subprocess.call(<span class="hljs-command" style="box-sizing: border-box;"><span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">command</span>, <span class="hljs-title" style="box-sizing: border-box;">shell</span>=<span class="hljs-title" style="box-sizing: border-box;">True</span>)</span> <span class="hljs-command" style="box-sizing: border-box;"><span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">command</span> = <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">'%s -q -r -pw Sybase123 %s root@%s:/root'</span> % (<span class="hljs-title" style="box-sizing: border-box;">pscpLoca</span>, <span class="hljs-title" style="box-sizing: border-box;">pscpLoca</span>, <span class="hljs-title" style="box-sizing: border-box;">ip</span>)</span> <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">result</span> = subprocess.call(<span class="hljs-command" style="box-sizing: border-box;"><span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">command</span>, <span class="hljs-title" style="box-sizing: border-box;">shell</span>=<span class="hljs-title" style="box-sizing: border-box;">True</span>)</span> <span class="hljs-command" style="box-sizing: border-box;"><span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">command</span> = <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">'%s -q -r -pw Sybase123 root@%s:/root/%s.hostname %s'</span> %(<span class="hljs-title" style="box-sizing: border-box;">pscpLoca</span>,<span class="hljs-title" style="box-sizing: border-box;">ip</span>,<span class="hljs-title" style="box-sizing: border-box;">ip</span>,<span class="hljs-title" style="box-sizing: border-box;">sumAutoLoca</span>)</span> <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">result</span> = subprocess.call(<span class="hljs-command" style="box-sizing: border-box;"><span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">command</span>, <span class="hljs-title" style="box-sizing: border-box;">shell</span>=<span class="hljs-title" style="box-sizing: border-box;">True</span>)</span> fileName = sumAutoLoca + ip + <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">'.hostname'</span> readFile = <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">open</span>(fileName,<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">'r'</span>) hostnameInfo = str(readFile.readline().strip(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">'\n'</span>)) readFile.<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">close</span>() subprocess.call(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">'rm '</span>+ fileName, <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">shell</span>=True) print <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"=========%s hostname is %s========"</span> %(ip,hostnameInfo) <span class="hljs-constant" style="box-sizing: border-box;">return</span> hostnameInfo</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li><li style="box-sizing: border-box; padding: 0px 5px;">8</li><li style="box-sizing: border-box; padding: 0px 5px;">9</li><li style="box-sizing: border-box; padding: 0px 5px;">10</li><li style="box-sizing: border-box; padding: 0px 5px;">11</li><li style="box-sizing: border-box; padding: 0px 5px;">12</li><li style="box-sizing: border-box; padding: 0px 5px;">13</li><li style="box-sizing: border-box; padding: 0px 5px;">14</li></ul>

設(shè)計(jì)思路 ##

有時(shí)候socket不太穩(wěn)定, 具體原因帶查明

目的: 根據(jù) ip 獲取 hostname (適當(dāng)改進(jìn)也可逆轉(zhuǎn))

先設(shè)計(jì)了一個(gè)遠(yuǎn)程執(zhí)行 ssh 命令jar, 或者可以用plink, 鏈接enter link description here

利用subprocess.call命令在遠(yuǎn)程ip機(jī)器上執(zhí)行hostname > %s.hostname命令, 將hostname 信息輸出到文件

用pscp將本地的pscp文件復(fù)制到遠(yuǎn)程ip機(jī)器上 /root 目錄下(后來(lái)發(fā)現(xiàn)這步不需要)

然后利用本地的 pscp 將遠(yuǎn)程機(jī)器上帶有hostname的文本文件/root/%s.hostname 復(fù)制到本地

利用 python 的文本讀取功能讀取信息, 從中取出hostname字符串

再利用 rm 命令把遠(yuǎn)程機(jī)器和本地的文本文件都刪除

相關(guān)文章

  • 在Sublime Editor中配置Python環(huán)境的詳細(xì)教程

    在Sublime Editor中配置Python環(huán)境的詳細(xì)教程

    這篇文章主要介紹在sublime編輯器中安裝python軟件包,以 實(shí)現(xiàn)自動(dòng)完成等功能,并在sublime編輯器本身中運(yùn)行build,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),需要的朋友參考下吧
    2020-05-05
  • python threading和multiprocessing模塊基本用法實(shí)例分析

    python threading和multiprocessing模塊基本用法實(shí)例分析

    這篇文章主要介紹了python threading和multiprocessing模塊基本用法,結(jié)合實(shí)例形式詳細(xì)分析了Python中threading和multiprocessing模塊基本概念、功能、使用方法及相關(guān)操作注意事項(xiàng),需要的朋友可以參考下
    2019-07-07
  • Python制作一個(gè)隨機(jī)抽獎(jiǎng)小工具的實(shí)現(xiàn)

    Python制作一個(gè)隨機(jī)抽獎(jiǎng)小工具的實(shí)現(xiàn)

    最近在工作中面向社群玩家組織了一場(chǎng)活動(dòng),需要進(jìn)行隨機(jī)抽獎(jiǎng),就做了一個(gè)簡(jiǎn)單的隨機(jī)抽獎(jiǎng)小工具。具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2021-07-07
  • Python編程實(shí)現(xiàn)雙擊更新所有已安裝python模塊的方法

    Python編程實(shí)現(xiàn)雙擊更新所有已安裝python模塊的方法

    這篇文章主要介紹了Python編程實(shí)現(xiàn)雙擊更新所有已安裝python模塊的方法,涉及Python針對(duì)模塊操作命令的相關(guān)封裝與調(diào)用技巧,需要的朋友可以參考下
    2017-06-06
  • python中的colorlog庫(kù)使用詳解

    python中的colorlog庫(kù)使用詳解

    這篇文章主要介紹了python中的colorlog庫(kù)詳解,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2019-07-07
  • python自動(dòng)生成model文件過(guò)程詳解

    python自動(dòng)生成model文件過(guò)程詳解

    這篇文章主要介紹了python自動(dòng)生成model文件過(guò)程詳解,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值
    2019-11-11
  • 如何使用Selenium實(shí)現(xiàn)簡(jiǎn)單的網(wǎng)絡(luò)自動(dòng)化操作指南

    如何使用Selenium實(shí)現(xiàn)簡(jiǎn)單的網(wǎng)絡(luò)自動(dòng)化操作指南

    Selenium是一個(gè)用于Web應(yīng)用測(cè)試的工具,Selenium測(cè)試直接運(yùn)行在瀏覽器中,就像真正的用戶在操作一樣,這篇文章主要給大家介紹了關(guān)于如何使用Selenium實(shí)現(xiàn)簡(jiǎn)單的網(wǎng)絡(luò)自動(dòng)化操作的相關(guān)資料,需要的朋友可以參考下
    2024-03-03
  • Django2.1.3 中間件使用詳解

    Django2.1.3 中間件使用詳解

    這篇文章主要介紹了Django2.1.3 中間件使用詳解,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2018-11-11
  • django echarts餅圖數(shù)據(jù)動(dòng)態(tài)加載的實(shí)例

    django echarts餅圖數(shù)據(jù)動(dòng)態(tài)加載的實(shí)例

    今天小編就為大家分享一篇django echarts餅圖數(shù)據(jù)動(dòng)態(tài)加載的實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2019-08-08
  • 基于scrapy實(shí)現(xiàn)的簡(jiǎn)單蜘蛛采集程序

    基于scrapy實(shí)現(xiàn)的簡(jiǎn)單蜘蛛采集程序

    這篇文章主要介紹了基于scrapy實(shí)現(xiàn)的簡(jiǎn)單蜘蛛采集程序,實(shí)例分析了scrapy實(shí)現(xiàn)采集程序的技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下
    2015-04-04

最新評(píng)論