https://github.com/pouyabahri/network-automation
https://github.com/pouyabahri/network-automation
ansible napalm python
Last synced: about 2 months ago
JSON representation
- Host: GitHub
- URL: https://github.com/pouyabahri/network-automation
- Owner: pouyabahri
- Created: 2025-01-24T19:23:37.000Z (over 1 year ago)
- Default Branch: main
- Last Pushed: 2025-11-10T18:04:10.000Z (8 months ago)
- Last Synced: 2025-11-10T20:10:16.543Z (8 months ago)
- Homepage:
- Size: 48.8 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Network Automation using Python and Ansible
This GitHub Repo focuses on using [NAPALM](https://github.com/napalm-automation/napalm) for automating network management on Cisco IOS devices.
This project aims to streamline network management tasks by utilizing NAPALM for network automation. NAPALM is a Python library that allows consistent interaction with network devices, regardless of vendor which simplifies configuration management and orchestration. It is a powerful solution for automating network operations.
## Table of Contents
- [Version History](#version-history)
- [References](#references)
- [Want to Contribute?](#want-to-contribute)
## Version History
### v1.3 (Latest)
- New: Added specific error handling for ping failures (e.g., "Request Timed Out", "Destination Host Unreachable").
```python
if "request timed out" in output:
return False, "Request Timed Out"
elif "destination host unreachable" in output:
return False, "Destination Host Unreachable"
elif result.returncode == 0:
return True, "Host is Reachable"
else:
return False, "Unknown Error"
except subprocess.TimeoutExpired:
return False, "Ping Command Timed Out"
except Exception as e:
return False, f"Error: {str(e)}"
```
Output:
```
🔎 Pinging 203.0.0.1...
❌ 203.0.0.1 is unreachable. Reason: Request Timed Out.
```
### v1.2
- New: Added specific error handling for SSH connection failures (e.g., authentication issues, timeouts).
```python
except napalm.base.exceptions.ConnectionException as e:
print(f'❌ SSH Connection error with {hostname}: {e}')
except Exception as e:
print(f'❌ Error configuring {hostname}: {e}')
```
Output:
```
🔎 Pinging 203.0.0.1...
✅ 203.0.0.1 is reachable. Proceeding with configuration...
🔌 Connecting to 203.0.0.1 via SSH...
❌ SSH Connection error with 203.0.0.1: Authentication failed.
```
### v1.1
- New: Added ping before establishing SSH session.
- Improved console output.
```python
import subprocess
def ping_host(hostname):
"""Ping a host to check if it is reachable."""
try:
result = subprocess.run(["ping", "-n", "1", hostname], stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
return result.returncode == 0 # 0 means the host is reachable
except Exception:
return False
for host in hosts:
hostname = host['hostname']
print(f'🔎 Pinging {hostname}...')
if ping_host(hostname):
print(f'✅ {hostname} is reachable. Proceeding with configuration...')
push_ios_config(host['hostname'], host['username'], host['password'], config_commands)
else:
print(f'❌ {hostname} is unreachable. Skipping configuration.')
```
### v1.0
- Initial release.
## References
- [ansible-napalm-samples](https://github.com/network-automation/ansible-napalm-samples)
## Want to Contribute?
We love and appreciate contributions of all kinds! Whether it's fixing bugs, adding features, or improving documentation. Your help makes a difference.
Have questions? Contact me at [pouya.bahri@yahoo.com].