FastHandle - IT Operations Examples

FastHandle is fast operation tools for infrastructure configurations and tests.

User Tools

Site Tools


Sidebar


Top     SiteMap

Manager Server

Target Server

$FHHOME/bin/xxx.sh | xxxx.exp













.

os:linux:net:index.html



Top#OS Management

Linux Network Configuration with Fabric (net.py)

Operation with Fabric

$ fab -l |grep -F net.


Fabric one-line Task Examples

$ H=test-server-01,test-server-02

# Interface's IP
$ fab -H $H  -- "ip a |grep inet"

# Bonding
$ fab -H $H  -- egrep  "*"  /proc/net/bonding/bond*
$ fab -H $H  -- egrep -i "mode|currently|status|count"  /proc/net/bonding/bond*

# Bonding Driver
$ fab -H $H  -- "lsmod |grep bonding"

# NIC Driver
$ fab -H $H  -- for i in `ip a |grep mtu |awk -F: {'print $2'} |egrep -v "lo"` ; do echo "----------" ; echo $i ;echo "----------"; ethtool -i $i ; done


# Routing
$ fab -H $H  -- netstat -rn
$ fab -H $H  -- cat /etc/sysconfig/static-route

# nmap
$ fab -H $H  -- sudo nmap -Pn -sT -p 22 xx.xx.xx.xx

# tcpdump
$ fab -H $H  -- sudo tcpdump  udp port 53 -i any  -W1 -G30  # check 30 seconds
$ fab -H $H  -- sudo tcpdump -n not arp and not port 123 and not port 22  -W1 -G30  # check 30 seconds

# DNS
$ fab -H $H  -- cat /etc/resolv.conf
$ fab -H $H  -- host google.com
$ fab -H $H  -- host test-server-01
$ fab -H $H  -- host x.x.x.x


net.py

$FHHOME/fabfile/net.py

import sys, os
from fabric.api import *
from fabric.contrib import files
from datetime import datetime

FHHOME=os.environ["FHHOME"]

# net.backup_config
@task
def backup_config():
    date = datetime.now().strftime('%Y%m%d_%H%M')
    run("test -d /tmp/nwconf_%s || mkdir /tmp/nwconf_%s" % date)
    # RHEL/CentOS
    run("cp -f /etc/sysconfig/network-scripts/ifcfg* /tmp/nwconf_%s/" % date)
    # Debian/Ubuntu
    run("cp -f /etc/network/interfaces /tmp/nwconf_%s/" % date)
    run("cp -f /etc/sysconfig/static-routes /tmp/nwconf_%s/" % date)


# net.set_bond:192.168.0.10,bond0,eth0,eth2
@task
def set_bond(ip,bond,nic1,nic2):
    backup_config()
    date = datetime.now().strftime('%Y%m%d_%H%M')
    # put
    put("%s/etc/sysconfig/network-scripts/ifcfg-bond.tpl" % FHHOME, "/tmp/ifcfg-%s.%s" % (bond,date))
    put("%s/etc/sysconfig/network-scripts/ifcfg-bond_nic.tpl" % FHHOME, "/tmp/ifcfg-%s.%s" % (nic1,date))
    put("%s/etc/sysconfig/network-scripts/ifcfg-bond_nic.tpl" % FHHOME, "/tmp/ifcfg-%s.%s" % (nic2,date))

    # bond
    sed("/tmp/ifcfg-%s.%s" % (bond,date), before="DEVICE=bondX", after="DEVICE=%s" % ip)
    sed("/tmp/ifcfg-%s.%s" % (bond,date), before="IPADDR=x.x.x.x", after="IPADDR=%s" % ip)
    # nic1
    sed("/tmp/ifcfg-%s.%s" % (nic1,date), before="DEVICE=X", after="DEVICE=%s" % nic1)
    sed("/tmp/ifcfg-%s.%s" % (nic1,date), before="MASTER=X", after="MASTER=%s" % bond)
    # nic2
    sed("/tmp/ifcfg-%s.%s" % (nic2,date), before="DEVICE=X", after="DEVICE=%s" % nic2)
    sed("/tmp/ifcfg-%s.%s" % (nic2,date), before="MASTER=X", after="MASTER=%s" % bond)

    run("diff /tmp/ifcfg-%s.%s  /etc/sysconfig/network-scripts/ifcfg-%s" % (bond,date,bond))
    run("diff /tmp/ifcfg-%s.%s  /etc/sysconfig/network-scripts/ifcfg-%s" % (nic1,date,nic1))
    run("diff /tmp/ifcfg-%s.%s  /etc/sysconfig/network-scripts/ifcfg-%s" % (nic2,date,nic2))

    sudo("mv /tmp/ifcfg-%s.%s  /etc/sysconfig/network-scripts/ifcfg-%s" % (bond,date,bond))
    sudo("mv /tmp/ifcfg-%s.%s /etc/sysconfig/network-scripts/ifcfg-%s" % (nic1,date.nic1))
    sudo("mv /tmp/ifcfg-%s.%s /etc/sysconfig/network-scripts/ifcfg-%s" % (nic2,date,nic2))



Configuration



os/linux/net/index.html.txt ยท Last modified: 2019/02/11 00:40 by kurihara