FastHandle - Automation Examples

FastHandle is fast operation tools for infrastructure configurations and tests.

User Tools

Site Tools


Top     SiteMap

Sidebar


Top     SiteMap

Manager Server

Target Server













.

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 auth.pro -- "ip a |grep inet"

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

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

# NIC Driver
$ fab -H $H auth.pro -- 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 auth.pro -- netstat -rn
$ fab -H $H auth.pro -- cat /etc/sysconfig/static-route

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

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

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


net.py

$FHHOME/fabfile/net.py

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

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: 2018/02/22 23:21 by kurihara