-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprint_ip_of_all_interfaces.py
39 lines (31 loc) · 1.08 KB
/
print_ip_of_all_interfaces.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
import os
ifaces = os.listdir('/sys/class/net/')
import socket
import requests
from requests import adapters
from urllib3.poolmanager import PoolManager
class InterfaceAdapter(adapters.HTTPAdapter):
def __init__(self, **kwargs):
self.iface = kwargs.pop('iface', None)
super(InterfaceAdapter, self).__init__(**kwargs)
def _socket_options(self):
if self.iface is None:
return []
else:
return [(socket.SOL_SOCKET, socket.SO_BINDTODEVICE, self.iface)]
def init_poolmanager(self, connections, maxsize, block=False):
self.poolmanager = PoolManager(
num_pools=connections,
maxsize=maxsize,
block=block,
socket_options=self._socket_options()
)
print(ifaces)
for iface in filter(lambda x: x != 'lo', ifaces):
try:
session = requests.Session()
for prefix in ('http://', 'https://'):
session.mount(prefix, InterfaceAdapter(iface=iface.encode()))
print(session.get("https://ifconfig.me/").text)
except Exception as e:
pass