Skip to content

Instantly share code, notes, and snippets.

@belyaev-pa
Created March 26, 2019 08:34
Show Gist options
  • Save belyaev-pa/b504acc8d654b2b0e3ede49ce4c33de9 to your computer and use it in GitHub Desktop.
Save belyaev-pa/b504acc8d654b2b0e3ede49ce4c33de9 to your computer and use it in GitHub Desktop.
# -*- coding: utf-8 -*-
from collections import defaultdict, OrderedDict
PINGDROP = OrderedDict([
('- первичный пинг локальных сокетов', ['ping', '-i', '0.2', '-w', '1', '127.0.0.1']),
(
'установка правила для блокировки icmp пакетов идущих на локальные сокеты',
['iptables', '-I', 'INPUT', '-s', '127.0.0.1', '-p', 'icmp', '-j', 'DROP']
),
('- повторный пинг локальных сокетов', ['ping', '-i', '0.2', '-w', '1', '127.0.0.1']),
(
'удаление правила для блокировки icmp пакетов идущих на локальные сокеты',
['iptables', '-D', 'INPUT', '-s', '127.0.0.1', '-p', 'icmp', '-j', 'DROP']
),
('- повторный пинг локальных сокетов после блокирования', ['ping', '-i', '0.2', '-w', '1', '127.0.0.1']),
])
def drop_packs():
"""
Выполняет и анализиует результаты исполнения команд согласно PINGDROP OrderedDict:
пинг -> блокировка -> пинг -> снятие блокировки -> пинг
В случае ошибки на любом этапе - логируем и декларируем непрохождение теста
Для анализа результатов пинга используется функция ping_process()
:return: True or False
"""
selector = defaultdict(lambda: (make_pass, False),
{'0': (ping_process, False),
'2': (ping_process, True),
'4': (ping_process, False),})
no_errors = True
for number, (comment, cmnd) in enumerate(PINGDROP.iteritems()):
result = execute(cmnd)
if not selector[str(number)][0](result, is_lost_packets=selector[str(number)][1]):
no_errors = False
return no_errors
def execute(str):
pass
def ping_process(res, is_lost_packets=False):
print(res)
print(is_lost_packets)
pass
def make_pass(a, is_lost_packets=False):
pass
drop_packs()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment