Add tech_docs/networking/networking_python.md
This commit is contained in:
231
tech_docs/networking/networking_python.md
Normal file
231
tech_docs/networking/networking_python.md
Normal file
@@ -0,0 +1,231 @@
|
|||||||
|
# Python Tools for Network Engineering
|
||||||
|
|
||||||
|
## SSH Management Libraries
|
||||||
|
|
||||||
|
1. **Netmiko**
|
||||||
|
- Multi-vendor SSH connection handler with built-in support for 20+ vendors
|
||||||
|
- Implements send_command(), send_config_set(), and send_config_from_file() methods
|
||||||
|
- Handles credentials, connection timing, and error patterns
|
||||||
|
- Example usage:
|
||||||
|
```python
|
||||||
|
from netmiko import ConnectHandler
|
||||||
|
device = {
|
||||||
|
"device_type": "cisco_ios",
|
||||||
|
"host": "192.168.1.1",
|
||||||
|
"username": "admin",
|
||||||
|
"password": "cisco"
|
||||||
|
}
|
||||||
|
with ConnectHandler(**device) as conn:
|
||||||
|
output = conn.send_command("show ip int brief")
|
||||||
|
```
|
||||||
|
|
||||||
|
2. **Paramiko**
|
||||||
|
- Low-level SSH2 protocol implementation in Python
|
||||||
|
- Supports key-based authentication and proxy commands
|
||||||
|
- Implementation example:
|
||||||
|
```python
|
||||||
|
import paramiko
|
||||||
|
client = paramiko.SSHClient()
|
||||||
|
client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
|
||||||
|
client.connect("192.168.1.1", username="admin", password="cisco")
|
||||||
|
stdin, stdout, stderr = client.exec_command("show version")
|
||||||
|
```
|
||||||
|
|
||||||
|
## Configuration Management
|
||||||
|
|
||||||
|
1. **NAPALM**
|
||||||
|
- Unified API across vendors (Cisco IOS/XR/NXOS, Juniper, Arista)
|
||||||
|
- Key methods: get_facts(), get_interfaces(), merge_config(), replace_config()
|
||||||
|
- Compare_config() method for configuration validation before commit
|
||||||
|
- Implementation:
|
||||||
|
```python
|
||||||
|
from napalm import get_network_driver
|
||||||
|
driver = get_network_driver("ios")
|
||||||
|
device = driver("192.168.1.1", "admin", "cisco")
|
||||||
|
device.open()
|
||||||
|
device.load_merge_candidate(filename="new_config.txt")
|
||||||
|
diff = device.compare_config()
|
||||||
|
if len(diff) > 0:
|
||||||
|
device.commit_config()
|
||||||
|
device.close()
|
||||||
|
```
|
||||||
|
|
||||||
|
2. **Nornir**
|
||||||
|
- Concurrent task execution framework with inventory management
|
||||||
|
- Python-native, no DSL required like Ansible
|
||||||
|
- Plugins architecture for various connection methods
|
||||||
|
- Example:
|
||||||
|
```python
|
||||||
|
from nornir import InitNornir
|
||||||
|
from nornir_netmiko import netmiko_send_command
|
||||||
|
nr = InitNornir(config_file="config.yaml")
|
||||||
|
result = nr.run(netmiko_send_command, command_string="show version")
|
||||||
|
```
|
||||||
|
|
||||||
|
## Protocol-Specific Libraries
|
||||||
|
|
||||||
|
1. **Scapy**
|
||||||
|
- Full packet manipulation library supporting 400+ protocols
|
||||||
|
- Packet creation, capture, and analysis capabilities
|
||||||
|
- Built-in traceroute, ARP/DNS scanning, and route discovery
|
||||||
|
- Example:
|
||||||
|
```python
|
||||||
|
from scapy.all import IP, TCP, sr1
|
||||||
|
packet = IP(dst="8.8.8.8")/TCP(dport=53, flags="S")
|
||||||
|
response = sr1(packet, timeout=2)
|
||||||
|
if response:
|
||||||
|
print(response.summary())
|
||||||
|
```
|
||||||
|
|
||||||
|
2. **pyATS & Genie**
|
||||||
|
- Test framework with 1200+ parsers for network device outputs
|
||||||
|
- Stateful device modeling and configuration validation
|
||||||
|
- Implementation:
|
||||||
|
```python
|
||||||
|
from pyats.topology import loader
|
||||||
|
from genie.testbed import load
|
||||||
|
testbed = load('testbed.yaml')
|
||||||
|
device = testbed.devices['router1']
|
||||||
|
device.connect()
|
||||||
|
output = device.parse("show interfaces")
|
||||||
|
```
|
||||||
|
|
||||||
|
## API and Protocol Interfaces
|
||||||
|
|
||||||
|
1. **Requests**
|
||||||
|
- HTTP library supporting REST API interactions
|
||||||
|
- JSON parsing and TLS/SSL verification
|
||||||
|
- Example:
|
||||||
|
```python
|
||||||
|
import requests
|
||||||
|
url = "https://192.168.1.1/api/interfaces"
|
||||||
|
headers = {"Content-Type": "application/json"}
|
||||||
|
response = requests.get(url, headers=headers, auth=("admin", "cisco"), verify=False)
|
||||||
|
interfaces = response.json()
|
||||||
|
```
|
||||||
|
|
||||||
|
2. **ncclient**
|
||||||
|
- NETCONF protocol client library (RFC 6241)
|
||||||
|
- XPath filtering support
|
||||||
|
- Implementation:
|
||||||
|
```python
|
||||||
|
from ncclient import manager
|
||||||
|
with manager.connect(host="192.168.1.1", port=830, username="admin",
|
||||||
|
password="cisco", hostkey_verify=False) as m:
|
||||||
|
interfaces = m.get_config(source="running", filter=("subtree",
|
||||||
|
"<interfaces xmlns='urn:ietf:params:xml:ns:yang:ietf-interfaces'/>"))
|
||||||
|
```
|
||||||
|
|
||||||
|
3. **PyEZ (Juniper)**
|
||||||
|
- Juniper-specific automation framework
|
||||||
|
- RPC, configuration, and operational state methods
|
||||||
|
- Example:
|
||||||
|
```python
|
||||||
|
from jnpr.junos import Device
|
||||||
|
from jnpr.junos.utils.config import Config
|
||||||
|
dev = Device(host="192.168.1.1", user="admin", password="juniper")
|
||||||
|
dev.open()
|
||||||
|
cfg = Config(dev)
|
||||||
|
cfg.load("set system services netconf ssh", format="set")
|
||||||
|
if cfg.diff():
|
||||||
|
cfg.commit()
|
||||||
|
dev.close()
|
||||||
|
```
|
||||||
|
|
||||||
|
## Network Data Processing
|
||||||
|
|
||||||
|
1. **NetworkX**
|
||||||
|
- Graph theory library for network topology analysis
|
||||||
|
- Algorithms for path finding, centrality, and flow analysis
|
||||||
|
- Supports GraphML, GML, and JSON formats
|
||||||
|
- Implementation:
|
||||||
|
```python
|
||||||
|
import networkx as nx
|
||||||
|
G = nx.Graph()
|
||||||
|
G.add_edge("Router1", "Router2", weight=10)
|
||||||
|
G.add_edge("Router2", "Router3", weight=5)
|
||||||
|
shortest_path = nx.shortest_path(G, "Router1", "Router3", weight="weight")
|
||||||
|
```
|
||||||
|
|
||||||
|
2. **Pandas**
|
||||||
|
- Data analysis with DataFrame objects
|
||||||
|
- Network inventory management and statistics
|
||||||
|
- Example:
|
||||||
|
```python
|
||||||
|
import pandas as pd
|
||||||
|
interfaces_df = pd.DataFrame([
|
||||||
|
{"device": "router1", "interface": "Gi0/1", "ip": "10.1.1.1", "status": "up"},
|
||||||
|
{"device": "router1", "interface": "Gi0/2", "ip": "10.1.2.1", "status": "down"}
|
||||||
|
])
|
||||||
|
# Filter interfaces that are down
|
||||||
|
down_interfaces = interfaces_df[interfaces_df["status"] == "down"]
|
||||||
|
```
|
||||||
|
|
||||||
|
## Monitoring & Telemetry
|
||||||
|
|
||||||
|
1. **gRPC for Streaming Telemetry**
|
||||||
|
- Bidirectional stream supporting dial-in and dial-out models
|
||||||
|
- Protocol Buffers for efficient data encoding
|
||||||
|
- Implementation:
|
||||||
|
```python
|
||||||
|
import grpc
|
||||||
|
from telemetry_pb2 import TelemetrySubscription
|
||||||
|
from telemetry_pb2_grpc import TelemetryStub
|
||||||
|
|
||||||
|
channel = grpc.insecure_channel('192.168.1.1:57500')
|
||||||
|
stub = TelemetryStub(channel)
|
||||||
|
subscription = TelemetrySubscription(path="Cisco-IOS-XR-infra-statsd-oper:infra-statistics/interfaces")
|
||||||
|
for response in stub.CreateSub(subscription):
|
||||||
|
process_telemetry(response)
|
||||||
|
```
|
||||||
|
|
||||||
|
2. **Prometheus Client**
|
||||||
|
- Time-series metrics collection and exportation
|
||||||
|
- Counter, Gauge, Histogram, and Summary metric types
|
||||||
|
- Example:
|
||||||
|
```python
|
||||||
|
from prometheus_client import start_http_server, Gauge
|
||||||
|
import time
|
||||||
|
|
||||||
|
interface_status = Gauge('interface_status', 'Interface operational status', ['device', 'interface'])
|
||||||
|
|
||||||
|
def collect_metrics():
|
||||||
|
# Logic to collect interface statuses from network devices
|
||||||
|
interface_status.labels(device='router1', interface='Gi0/1').set(1) # 1 = up
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
start_http_server(8000)
|
||||||
|
while True:
|
||||||
|
collect_metrics()
|
||||||
|
time.sleep(60)
|
||||||
|
```
|
||||||
|
|
||||||
|
## YAML/JSON Parsing for Network Configurations
|
||||||
|
|
||||||
|
1. **PyYAML**
|
||||||
|
- YAML parser and emitter
|
||||||
|
- Used for configuration templates and data models
|
||||||
|
- Implementation:
|
||||||
|
```python
|
||||||
|
import yaml
|
||||||
|
|
||||||
|
with open('network_inventory.yaml', 'r') as f:
|
||||||
|
inventory = yaml.safe_load(f)
|
||||||
|
|
||||||
|
for device in inventory['devices']:
|
||||||
|
print(f"Connecting to {device['hostname']} at {device['ip']}")
|
||||||
|
```
|
||||||
|
|
||||||
|
2. **jq.py**
|
||||||
|
- JSON querying tool similar to jq command-line utility
|
||||||
|
- Complex data extraction capabilities
|
||||||
|
- Example:
|
||||||
|
```python
|
||||||
|
import jq
|
||||||
|
|
||||||
|
with open('network_data.json', 'r') as f:
|
||||||
|
data = f.read()
|
||||||
|
|
||||||
|
# Extract all interfaces with admin status up
|
||||||
|
result = jq.compile('.interfaces[] | select(.admin_status=="up") | .name').input(data).all()
|
||||||
|
```
|
||||||
Reference in New Issue
Block a user