-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcommon.py
54 lines (44 loc) · 1.24 KB
/
common.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#! /usr/bin/python
# coding: utf-8
import os
import fcntl
import struct
import socket
from netaddr import IPNetwork
from string import Template
def get_ip_from_cidr(net_cidr, index):
ip_list = IPNetwork(net_cidr)
return ip_list[index].format()
def fill_service_configure(service_temp_file, service_target_file, user_params):
with open(service_temp_file, "r") as temp_f:
temp_content = temp_f.read()
temp = Template(temp_content)
temp_f.close()
with open(service_target_file, "w+") as target_f:
target_f.write(temp.substitute(user_params))
target_f.close()
def get_hostname():
'''
:return:
Princess
'''
return socket.gethostname()
def get_host_by_dev(device="eth0"):
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
return socket.inet_ntoa(fcntl.ioctl(
s.fileno(),
0x8915,
struct.pack('256s', device[:15])
)[20:24])
def get_host_fdnq():
'''
:return:
Princess.intra.legendsec.com
'''
return socket.getfqdn(socket.gethostname())
def get_host_ips():
'''
:return:
('Princess.intra.legendsec.com', [], ['192.168.47.1', '192.168.57.1', '172.24.46.49'])
'''
return socket.gethostbyname(socket.gethostname())