Obtain hostname and fqdn Based on Python Shell.

Source: Internet
Author: User
Tags pscp fully qualified domain name

Obtain hostname and fqdn Based on Python Shell.

Linux's hostname and fqdn (Fully Qualified Domain Name) have been confusing for a long time. Today we are dedicated to figuring out their usage details.

1. Set hostname/fqdn

Setting hostname in Linux is very easy, for example:

$ Hostname florian

If you want to set fqdn, You need to configure/etc/hosts.

$ cat /etc/hosts127.0.0.1 localhost192.168.1.1 florian.test.com florian 

The format of the/etc/hosts configuration file is:

Ip fqdn [alias]...

That is, the first column is the Host IP address, the second column is the host fqdn address, and the third column is later an alias, which can be omitted. Otherwise, at least the hostname must be included.

The configuration items in the preceding configuration file are the first configuration of localhost, and the second configuration is the host name of florian with fqdn = florian.test.com, ip = 192.168.1.1.
As for the domain name suffix of fqdn, it is best to keep consistent with the HOSTNAME configuration of the file/etc/sysconfig/network:

$ cat /etc/sysconfig/networkNETWORKING=yesHOSTNAME=test.com

Ii. View hostname/fqdn

After the configuration is complete, run the shell command to view the hostname and fqdn:

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

Use ping to test whether the ip address ing of hostname is successful.

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

You can also use the python command to obtain the hostname and 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 linux2Type "help", "copyright", "credits" or "license" for more information.>>> import socket>>> socket.gethostname()'florian'>>> socket.getfqdn() 'florian.test.com' 

Iii. fqdn problems caused by setting hostname using IP

The above describes how to set the hostname and fqdn normally, but sometimes the IP address is directly used as the hostname, which may be somewhat different.

$ hostname 192.168.1.1$ hostname && hostname -f192.168.1.1192.168.1.1 

After using the ip address as the hostname, we can use the shell command to query whether the hostname and fqdn are ip addresses !!! This is because the DNS protocol will parse the content of the hostname. When it finds that it is an IP address, it will not query the/etc/hosts file again.

Use python to check whether the fqdn obtained by python is florian.test.com !!!

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

Even refreshing the dns Cache does not help:

$ Service nscd reload

Comment on the second line of the/etc/hosts file:

cat /etc/hosts127.0.0.1 localhost# 192.168.1.1 florian.test.com florian 

Refresh dns Cache:

$ Service nscd reload

We found that the fqdn is back to normal.

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

The reason for this behavior is that the logic for parsing the fqdn in python is not exactly the same as that in DNS. It queries the corresponding IP address based on the hostname, then, obtain the configuration line corresponding to the IP address in/etc/hosts (valid in the first line), parse the fqdn column and the alias column, and return the first containing character '..

Therefore, when using ipto set the hostname, pay attention to the following two points:

• First, set the hostname to an IP address
• Remove the configuration items that contain the ip address in/etc/hosts.

For the sake of insurance, we can add the following configuration in the/etc/hosts as early as possible:

cat /etc/hosts127.0.0.1 localhost192.168.1.1 192.168.1.1 

In this way, even configuration items containing the ip address will not take effect. python will first parse the configuration items in the second line and obtain the fqdn address exactly the same as the ip address. Of course, using the shell command hostname to obtain the fqdn will not cause errors, because the hostname has been set as an IP address.

The following describes how to obtain a hostname from an ip address in python shell. | obtains an ip address based on the hostname.

Use the gethostbyname function in the socket module

<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>

Method 2 use the hostname command in shell

<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>

Design Concept ##

Sometimes the socket is unstable, and the specific cause is to find out

Objective: To obtain the hostname Based on the ip address (improvement can also be reversed)

First, we designed a remote command to execute the ssh command jar, or you can use plink to link enter link description here

Use the subprocess. call Command to run the hostname> % s. hostname command on the remote ip machine to output the hostname Information to the file.

Use pscp to copy the local pscp file to the/root directory of the remote ip machine (this step is not required later)

Then, the local pscp is used to copy the text file/root/% s. hostname with hostname on the remote machine to the local machine.

Use the python text reading function to read information and retrieve the hostname string

Use the rm command to delete the remote machine and local text files.

Articles you may be interested in:
  • How to Set fqdn (full name domain name) and hostname in centos

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.