Get hostname and FQDN based on Python shell

Source: Internet
Author: User
Tags get ip readfile pscp fully qualified domain name
The Linux hostname and FQDN (Fully qualified Domain Name) have been confusing for a long time, and today they are dedicated to figuring out the details of their use.

First, set Hostname/fqdn

Setting up hostname in a Linux system is simple, such as:

$ hostname Florian

If you want to set the FQDN, you need to configure the/etc/hosts.


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

IP FQDN [alias] ...

That is, the first list is the host IP address, the second is the host FQDN address, the third column is an alias later, you can omit it, or at least include hostname.

The first behavior of the configuration item for the above configuration file is the configuration of localhost, and the second behavior of the host name Florian Configuration fqdn=florian.test.com,ip=192.168.1.1.
As for the domain name suffix of the FQDN, it is best to maintain consistency with the hostname configuration of the file/etc/sysconfig/network:

$ cat/etc/sysconfig/networknetworking=yeshostname=test.com

Second, view Hostname/fqdn

After the configuration is complete, you can use the shell command to view the hostname and FQDN:

Use ping to test whether the IP mapping of the hostname is successful.


You can also use the Python command to get hostname and FQDNs.

Third, using IP settings hostname the FQDN problem

The above describes how to set the hostname and FQDN normally, but sometimes the IP address is used directly as the hostname, which can be a bit different at this point.

We found that using the IP as hostname, using the shell command query hostname and FQDN are IP addresses!!! This is because the DNS protocol resolves the contents of the hostname, and when it is found to be an IP address, the/etc/hosts file is no longer queried.

Using Python again, you will find that the FQDN obtained by Python is still florian.test.com!!!

Even refreshing the DNS cache is useless:

$ Service NSCD Reload

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

Refresh the DNS cache:

$ Service NSCD Reload

We found the FQDN back to normal.

The reason for this behavior is that the logic and DNS of the Python parsing FQDN are not exactly the same, it will query the corresponding IP address according to hostname, and then get the configuration line corresponding to the IP address in/etc/hosts (the first row is valid). Then parse the FQDN column and the alias column and return the first containing character '. ' The value of the corresponding column.

Therefore, when using IP settings hostname, there are two points to note:

• First, set the hostname to an IP address
• Next, remove the configuration item that contains the IP in the/etc/hosts

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

This way, even if a configuration item that contains the IP does not take effect after that, Python prioritizes the second line of configuration items and obtains the same FQDN address as the IP address. Of course, using the shell command hostname to get the FQDN is also not an error, because hostname has been set to the IP address form.

Let's introduce python shell to get hostname based on IP | | Get IP based on hostname

Using the gethostbyname function in the socket module

>>> ImportSocket>>>Socket.gethostbyname("Www.baidu.com")' 61.135.169.125 '>>>Socket.gethostbyname("rs.xidian.edu.cn")' 202.117.119.1 '
 
 
    • 1
    • 2
    • 3
    • 4
    • 5

Method 2 using the hostname command in the shell

def gethostname (IP): Command = ' Java-jar%s '%s ' hostname >%s.hostname '% (remotecmdloca,IP,  IP) result= Subprocess.call ( command, shell=True)  Command = '%s-q-r-pw Sybase123%s root@%s:/root ' % (pscploca, pscploca, ip< /c5>) result= Subprocess.call ( command, shell=True)  Command = '%s-q-r-pw Sybase123 root@%s:/root/%s.hostname%s ' % (pscploca,IP, C5>ip,sumautoloca) result= Subprocess.call ( command, shell=True)FileName = sumautoloca + IP +'. Hostname 'ReadFile =Open(FileName,' R ') Hostnameinfo = str (Readfile.readline (). Strip (' \ n ')) ReadFile.Close() Subprocess.call (' RM '+ FileName,Shell=true) Print"=========%s hostname is%s========"% (Ip,hostnameinfo)returnHostnameinfo
 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14

Design Ideas # #

Sometimes the socket is not very stable, the specific reason is identified

Purpose: Obtain hostname based on IP (appropriate improvements can also be reversed)

A remote Execute SSH command jar is designed, or you can use Plink, link enter link description here

Execute the hostname >%s.hostname command on the remote IP machine using the subprocess.call command to output the hostname information to a file

Copy the local PSCP file to the/root directory on the remote IP machine using PSCP (This step was later found to be unnecessary)

Then use the local PSCP to copy the hostname text file/root/%s.hostname on the remote machine to the local

Use Python's text read function to read the information and remove the hostname string from it

The RM command is used to remove both the remote machine and the local text file.

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