Python library and tools for managing MikroTik SwOS and SwOS Lite switches
Project description
SwOS API and Tools
Python library and tools for managing MikroTik SwOS (SwitchOS) and SwOS Lite (SwitchOS Lite) switches.
Components
- swos package: Python library for reading/writing switch configuration
- swos-config: CLI tool for displaying configuration
- swos-export: CLI tool for exporting configurations to Ansible YAML format
- Ansible module: Full-featured Ansible integration for declarative, idempotent switch configuration management
- Supports check mode (dry-run)
- Auto-detects platform (SwOS vs SwOS Lite)
- Manages ports, PoE, VLANs, LAG/LACP, SNMP
- See ANSIBLE.md for complete documentation
Capabilities
Read: System info, ports, PoE, LAG/LACP, per-port VLANs, VLAN table, host table, SFP info, SNMP Write: System settings, port config, PoE settings, LAG/LACP, per-port VLANs, VLAN table, SNMP Backup/Restore: Download binary backups, restore from backup files SwOS-only: VLAN names, isolation, learning, mirror settings (not available on SwOS Lite) Note: All configuration changes are immediately applied and persisted by the switch.
Requirements
- Python 3.6+
- requests>=2.25.0
Installation
pip install mikrotik-swos
Or for development:
pip install -r requirements.txt
Confirmed Compatibility
-
SwOS 2.17
-
SwOS 2.18
-
SwOS Lite 2.17
-
SwOS Lite 2.18
-
SwOS Lite 2.19
-
SwOS Lite 2.20
Tested Hardware
-
CRS305-1G-4S+
-
CRS309-1G-8S+
-
CRS310-8G+2S+
-
CRS326-24S+2Q
-
CSS610-8G-2S+
-
CSS610-8P-2S+
Note: Gracefully handles switches without PoE, LAG/LACP, or SFP capabilities.
Quick Start
CLI Tools
swos-config - Display switch configuration:
# Display configuration
swos-config 192.168.88.1 admin ""
# Save to file
swos-config 192.168.88.1 admin "" > backup.txt
swos-export - Export to Ansible YAML format:
# Export all switches from inventory
swos-export -i inventory.yml -o configs/
# Export single switch
swos-export --host 192.168.1.1 --username admin --password "" -o switch.yml
Python API
from swos import get_system_info, set_port_config, get_backup
url = "http://192.168.88.1"
system = get_system_info(url, "admin", "")
print(f"{system['identity']} - {system['model']}")
# Configure a port
set_port_config(url, "admin", "", port_number=1, name="Uplink")
# Create a backup
backup_data = get_backup(url, "admin", "")
with open("switch_backup.swb", "wb") as f:
f.write(backup_data)
See module docstrings for complete API documentation.
Ansible - Infrastructure as Code
Perfect for managing multiple switches declaratively!
Quick Setup:
# Install package
pip install mikrotik-swos
# Copy module to your playbook
mkdir -p library
cp ansible/swos.py library/
Example Playbook:
- name: Configure Switch
hosts: switches
tasks:
- name: Apply configuration
swos:
host: "{{ ansible_host }}"
username: admin
password: ""
config:
system:
identity: "Office-Switch-01"
ports:
- port: 1
name: "Uplink"
enabled: true
- port: 2
name: "Server"
enabled: true
vlans:
- vlan_id: 10
members: [2, 3, 4]
Features:
- Idempotent - only applies changes when needed
- Check mode - preview changes with
--check - Auto-detection - works with both SwOS and SwOS Lite
- Complete validation - catches configuration errors before applying
Full Ansible Documentation - Complete guide with advanced examples
API Functions
Read: get_system_info(), get_links(), get_poe(), get_lag(), get_port_vlans(), get_vlans(), get_hosts(), get_sfp_info(), get_snmp()
Write: set_system(), set_port_config(), set_poe_config(), set_lag_config(), set_port_vlan(), set_vlans(), set_snmp()
Backup/Restore: get_backup(), restore_backup()
All functions take (url, username, password, ...) parameters.
Read functions return lists of dictionaries with configuration data.
Write functions take port_number and optional setting parameters (except set_system(), set_snmp(), and set_vlans() which set global config).
Backup functions work with binary .swb files (encrypted/proprietary MikroTik format).
See docstrings in the swos module for detailed parameters and return values.
Security
- SwOS Lite uses HTTP with Digest Authentication (no HTTPS)
- Use on trusted networks only
- Use Ansible Vault for password storage
Development
Publishing a New Release
-
Create and push a git tag:
git tag v0.2.0 git push origin v0.2.0
-
GitHub Actions automatically builds and publishes to PyPI
Credits
Certain components of this codebase were created with the assistance of AI.
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file mikrotik_swos-1.2.2.tar.gz.
File metadata
- Download URL: mikrotik_swos-1.2.2.tar.gz
- Upload date:
- Size: 140.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
cd7ee97f2576f3a3e0c2836b96d507e6512d6467711294debf7922c3456a918b
|
|
| MD5 |
48b697b56e9b4e14f4d6481f843a1cfb
|
|
| BLAKE2b-256 |
112c8bc2e375196103ced1aeb88622db640a5a894d2f0d0e9db5021aaee971e2
|
Provenance
The following attestation bundles were made for mikrotik_swos-1.2.2.tar.gz:
Publisher:
publish.yml on lanrat/python-mikrotik-swos
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
mikrotik_swos-1.2.2.tar.gz -
Subject digest:
cd7ee97f2576f3a3e0c2836b96d507e6512d6467711294debf7922c3456a918b - Sigstore transparency entry: 889804248
- Sigstore integration time:
-
Permalink:
lanrat/python-mikrotik-swos@d403c18ebbc8882b1763a133fd913298d4dde19a -
Branch / Tag:
refs/tags/v1.2.2 - Owner: https://github.com/lanrat
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@d403c18ebbc8882b1763a133fd913298d4dde19a -
Trigger Event:
push
-
Statement type:
File details
Details for the file mikrotik_swos-1.2.2-py3-none-any.whl.
File metadata
- Download URL: mikrotik_swos-1.2.2-py3-none-any.whl
- Upload date:
- Size: 28.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
5d6985a2d262a7b0327ddcc93522b708be021e2c0dbed6f971f19bdd3a78746e
|
|
| MD5 |
57de8d77840c0e62668f5d5815a22a8f
|
|
| BLAKE2b-256 |
35579ba00ac2ee40e91538c0c300b59bade870a1ace54a07c2af210e5d799895
|
Provenance
The following attestation bundles were made for mikrotik_swos-1.2.2-py3-none-any.whl:
Publisher:
publish.yml on lanrat/python-mikrotik-swos
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
mikrotik_swos-1.2.2-py3-none-any.whl -
Subject digest:
5d6985a2d262a7b0327ddcc93522b708be021e2c0dbed6f971f19bdd3a78746e - Sigstore transparency entry: 889804302
- Sigstore integration time:
-
Permalink:
lanrat/python-mikrotik-swos@d403c18ebbc8882b1763a133fd913298d4dde19a -
Branch / Tag:
refs/tags/v1.2.2 - Owner: https://github.com/lanrat
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@d403c18ebbc8882b1763a133fd913298d4dde19a -
Trigger Event:
push
-
Statement type: