7 Sept 2024
Tired of managing network automation with raw Python scripts? Discover how Laravel, with its built-in scheduling, queues, and web UI support, offers a surprisingly elegant solution for scalable, secure network automation—even in complex environments.
rConfig
All at rConfig
When it comes to network automation, many developers and network engineers default to using Python, thanks to its robust libraries like Netmiko
, NAPALM
, and Paramiko
. Python is well-suited for scripting, but it can be a bit tricky when scaling projects, building a user-friendly interface, or managing complex workflows. This is where Laravel, the PHP-based web framework, can offer a compelling alternative.
Laravel's ease of use, extensive ecosystem, and well-organized structure make it surprisingly effective for building network automation tools. In this blog, we’ll walk through how you can get started with Laravel for network automation, and why it might be simpler than building everything from scratch with Python.
Why Laravel for Network Automation?
Laravel offers several advantages for network automation:
Built-In Structure: Laravel’s MVC (Model-View-Controller) architecture organizes your code in a clear way, making it easier to maintain and scale.
Web Interface: Laravel’s Blade templating engine makes it easy to create a clean web UI for your automation platform.
Eloquent ORM: Managing databases and logging network operations becomes easier with Laravel’s built-in ORM (Object Relational Mapping) for working with SQL databases.
Job Queues and Scheduling: Laravel makes it incredibly easy to schedule tasks and queue jobs, essential for automating recurring network operations.
Third-Party Integrations: Laravel’s ecosystem is vast, with plenty of packages for SSH, APIs, and more—simplifying things you’d otherwise write custom code for in Python.
Let’s dive into a simple example of using Laravel to automate network tasks.
Step 1: Install Laravel
To start, you'll need to install Laravel. If you haven’t done that already, you can install it via Composer.
After installing Laravel, create a new project:
Once Laravel is installed, navigate into your new project directory:
Step 2: Set Up SSH for Network Devices
To automate network devices, you need a way to communicate with them. In Python, you would use libraries like Netmiko
, but in Laravel, we can use a package like Spatie’s SSH package to easily run SSH commands.
First, install the SSH package:
Now, let's create a simple controller that can SSH into a network device and run a command.
Create a New Controller
Run the following Artisan command to generate a controller:
This will generate a NetworkController.php
file inside app/Http/Controllers/
. In this controller, we'll use the Spatie SSH package to automate some tasks.
Code Example: SSH Into a Network Device
Here's how you can SSH into a network device and fetch its configuration:
In this example:
We’re SSHing into a network device (in this case,
192.168.1.1
).The
execute('show running-config')
command retrieves the running configuration.If successful, the result is passed to a Blade view to be displayed.
Create a Blade View
Let’s create a simple Blade view to show the output of the network device configuration.
Create a new file named show.blade.php
in the resources/views/config/
folder.
Now, when you navigate to the route connected to this controller method, you’ll see the configuration output in your browser.
Step 3: Define a Route for the Network Controller
Next, set up a route to trigger the controller action. Open routes/web.php
and add:
Now, if you navigate to http://your-app-url/fetch-config
, you’ll trigger the SSH command, and the device’s configuration will be displayed in the web UI.
Step 4: Automate Tasks with Laravel’s Task Scheduler
One of the biggest challenges in network automation is scheduling regular tasks, like backups or status checks. Laravel’s built-in task scheduler makes this incredibly easy.
First, let’s set up a recurring task to backup the network device’s configuration every day.
Scheduling a Task
In Laravel, scheduled tasks are defined in the app/Console/Kernel.php
file. Open it up and add the following inside the schedule
method:
This will SSH into the device and save the configuration to a backup file in the storage/app/backups/
directory every day. The scheduler will handle running this job in the background automatically.
Running the Scheduler
To ensure the scheduler runs, you need to add the following cron entry on your server:
This will run Laravel’s task scheduler every minute, which in turn will run the defined tasks at the correct time (e.g., daily, weekly, etc.).
Step 5: Use Laravel Queues for Bulk Operations
Network automation often requires running tasks across many devices in parallel. Laravel’s job queues are perfect for this. Let’s say we want to run a command on multiple devices simultaneously—Laravel can queue up the jobs to process them in parallel.
Create a New Job
You can create a job with Artisan:
In the RunCommandOnDevice
job, we’ll add the logic to SSH into a device and execute a command.
Now, you can queue this job for multiple devices:
Laravel will queue up these jobs, and you can process them in parallel with workers.
Running the Queue Worker
To start processing the jobs, run the queue worker with:
This allows you to scale up and handle large-scale network automation tasks efficiently.
Conclusion
While Python has been the go-to choice for network automation, Laravel offers a more structured, scalable, and user-friendly solution, especially when building out more complex automation platforms with a UI, scheduling, and job queuing. With packages like Spatie SSH and Laravel’s built-in task scheduling, you can get a powerful network automation platform up and running much faster than starting from scratch with Python.
If you’re building automation tools for larger teams or need a simple way to manage recurring network tasks, Laravel provides an excellent foundation to get
Why Rigid Network Automation Platforms Fail—and How rConfig Empowers IT Teams with Flexibility
Discover why opinionated network automation tools fall short in today’s multi-vendor environments. Learn how rConfig’s flexible, vendor-agnostic NCM approach empowers IT teams to automate, scale, and innovate without compromise.
rConfig
All at rConfig
Why Network State Backups Matter: How rConfig Goes Beyond Configuration for Real Visibility
Go beyond intent with rConfig’s state-aware network management. Capture real-time device behavior with state backups—BGP, routing tables, LLDP/CDP, and more—for faster troubleshooting, compliance, and full-stack visibility across your network.
rConfig
All at rConfig
Why Big Tech is Abandoning NCM—and How rConfig Fills the Gap with Reliable, Vendor-Agnostic Solutions
As Cisco, VMware, and others pivot to AI, traditional NCM tools are being left behind. Discover how rConfig delivers modern, vendor-agnostic network configuration management—built for security, compliance, and real-world IT needs.
rConfig
All from rConfig