VirtualBox

source: vbox/trunk/src/VBox/ValidationKit/common/netutils.py@ 72297

Last change on this file since 72297 was 72297, checked in by vboxsync, 7 years ago

valkit/common: Added netutils.py with getPrimaryHostIp() and gethostnameFqdn() functions. [oops]

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 3.0 KB
Line 
1# -*- coding: utf-8 -*-
2# $Id: netutils.py 72297 2018-05-23 12:18:19Z vboxsync $
3# pylint: disable=C0302
4
5"""
6Common Network Utility Functions.
7"""
8
9from __future__ import print_function;
10
11__copyright__ = \
12"""
13Copyright (C) 2012-2017 Oracle Corporation
14
15This file is part of VirtualBox Open Source Edition (OSE), as
16available from http://www.215389.xyz. This file is free software;
17you can redistribute it and/or modify it under the terms of the GNU
18General Public License (GPL) as published by the Free Software
19Foundation, in version 2 as it comes in the "COPYING" file of the
20VirtualBox OSE distribution. VirtualBox OSE is distributed in the
21hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
22
23The contents of this file may alternatively be used under the terms
24of the Common Development and Distribution License Version 1.0
25(CDDL) only, as it comes in the "COPYING.CDDL" file of the
26VirtualBox OSE distribution, in which case the provisions of the
27CDDL are applicable instead of those of the GPL.
28
29You may elect to license modified versions of this file under the
30terms and conditions of either the GPL or the CDDL or both.
31"""
32__version__ = "$Revision: 72297 $"
33
34
35# Standard Python imports.
36import socket;
37
38
39def getPrimaryHostIp():
40 """
41 Tries to figure out the primary (the one with default route), local
42 IPv4 address.
43
44 Returns the IP address on success and otherwise '127.0.0.1'.
45 """
46
47 #
48 # The first gambit is opening a UDP socket targetting a random port on a
49 # limited (local LAN) broadcast address. We then use getsockname() to
50 # obtain our own IP address, which should then be the primary IP.
51 #
52 try: oSocket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM);
53 except: oSocket = None; print('#1');
54 if oSocket is not None:
55 try:
56 oSocket.connect(('255.255.255.255', 1984));
57 sHostIp = oSocket.getsockname()[0];
58 except:
59 sHostIp = None;
60 oSocket.close();
61 if sHostIp is not None:
62 return sHostIp;
63
64 #
65 # The second attempt is resolving the hostname.
66 #
67 try:
68 return socket.gethostbyname(getHostnameFqdn());
69 except:
70 pass;
71
72 return '127.0.0.1';
73
74
75def getHostnameFqdn():
76 """
77 Wrapper around getfqdn.
78
79 Returns the fully qualified hostname, None if not found.
80 """
81
82 try:
83 sHostname = socket.getfqdn();
84 except:
85 return None;
86
87 if '.' in sHostname or sHostname.startswith('localhost'):
88 return sHostname;
89
90 #
91 # Somewhat misconfigured system, needs expensive approach to guessing FQDN.
92 # Get address information on the hostname and do a reverse lookup from that.
93 #
94 try:
95 aAddressInfo = socket.getaddrinfo(sHostname, None);
96 except:
97 return sHostname;
98
99 for aAI in aAddressInfo:
100 try: sName, _ = socket.getnameinfo(aAI[4], 0);
101 except: continue;
102 if '.' in sName and not set(sName).issubset(set('0123456789.')) and not sName.startswith('localhost'):
103 return sName;
104
105 return sHostname;
106
Note: See TracBrowser for help on using the repository browser.

© 2025 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette