Skip to content

Instantly share code, notes, and snippets.

'''
Usage::
cat momentary.txt | python translate.py >output.txt
'''
import sys
import csv
# create a CSV reader
reader = csv.reader(sys.stdin, dialect='excel-tab')
@svinota
svinota / etc-hosts-on-win.md
Last active November 5, 2018 13:07 — forked from zenorocha/etc-hosts-on-win.md
/etc/hosts on Windows

notepad

For Windows 10 and 8
  1. Press the Windows key.
  2. Type Notepad in the search field.
  3. In the search results, right-click Notepad and select Run as administrator.
  4. From Notepad, open the following file: c:\Windows\System32\Drivers\etc\hosts
  5. Make the necessary changes to the file.
  6. Click File > Save to save your changes.
from fjorton import fjorton
from fjorton import apply
from pyroute2 import IPRoute
from functools import partial
@fjorton
def f():
with IPRoute() as ip:
ip.get_links()
@svinota
svinota / partial.py
Last active April 22, 2016 10:20
partial transaction session
In [1]: from pyroute2 import IPDB
In [2]: ip = IPDB()
# create future VRF port
In [3]: ip.create(ifname='v0p0', kind='dummy').commit()
Out[3]: {'family': 0, 'txqlen': 1000, 'ipdb_scope': 'system', 'index': 1118, 'operstate': 'DOWN', 'num_tx_queues': 1, 'group': 0, 'carrier_changes': 0, 'unknown': '08:00:29:00:00:00:01:00', 'ipaddr': [], 'neighbours': [], 'ifname': 'v0p0', 'promiscuity': 0, 'linkmode': 0, 'broadcast': 'ff:ff:ff:ff:ff:ff', 'address': 'f6:22:cf:07:8a:bc', 'vlans': [], 'ipdb_priority': 0, 'change': 4294967295, 'kind': 'dummy', 'qdisc': 'noop', 'mtu': 1500, 'num_rx_queues': 1, 'carrier': 1, 'flags': 130, 'ifi_type': 1, 'proto_down': 0, 'ports': []}
# create VRF interface
In [4]: ip.create(ifname='v0', kind='vrf', vrf_table=20).commit()
package main
import (
"os"
"time"
"github.com/vishvananda/netlink"
)
@svinota
svinota / functional.py
Created April 22, 2016 04:46
create a bridge, create 20 veth interfaces, put veth peers to namespaces (and rename as eth0), add veth interfaces as ports to the bridge
from pyroute2 import IPDB
from pyroute2 import netns
with IPDB() as ip:
ip.create(ifname='v0', kind='bridge').commit()
map(lambda x: ip.create(ifname='v0p%02i' % x,
peer='p0p%02i' % x,
kind='veth').commit(), range(20))
def add_iface_to_netns(if_name, ns_name):
#
# init IPDB and netns
ipdb = IPDB()
netns = None
#
# try to perform the operation
try:
# NetNS() constructor also can raise exceptions,
# so put it under try/catch
def add_iface_to_netns(if_name, ns_name):
#
# IPDB instantiation, as well as NetNS instantiation,
# are expensive operations. NetNS spawns an additional
# process, and IPDB populates itself with all the data
# from the operating system networking.
#
# Maybe, it would be not a bad idea to keep IPDB as a
# long-running global object, or like that.
#
@svinota
svinota / gist:776d7add72a224e12111
Last active August 29, 2015 14:12
pyroute2 IPDB + network namespaces
$ sudo python
Python 2.7.5 (default, Nov 3 2014, 14:26:24)
[GCC 4.8.3 20140911 (Red Hat 4.8.3-7)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> from pyroute2 import IPDB
>>> from pyroute2 import NetNS
>>> ip1 = IPDB()
>>> ip2 = IPDB(nl=NetNS('test'))
>>> ip1.by_name.keys()
['bond0', 'lo', 'vnet0', 'em1', 'wlo1', 'dummy0', 'v13p0', 'virbr0-nic', 'virbr0']