• Contact
  • About Me
  • Privacy Policy
  • Disclaimer
DefenceDev
  • Home
  • Linux
    • Bash Scripting
      • Bash Scripting Lessons
    • Commands&Tips
    • Monitoring
  • Networking
    • FortiGate
    • MikroTik
  • Projects & Case Studies
  • Self-Hosted
    • AdGuard
    • Immich
    • Nextcloud
    • WordPress
  • Virtualization
    • Proxmox
    • VMware
  • Tools
No Result
View All Result
  • Home
  • Linux
    • Bash Scripting
      • Bash Scripting Lessons
    • Commands&Tips
    • Monitoring
  • Networking
    • FortiGate
    • MikroTik
  • Projects & Case Studies
  • Self-Hosted
    • AdGuard
    • Immich
    • Nextcloud
    • WordPress
  • Virtualization
    • Proxmox
    • VMware
  • Tools
No Result
View All Result
DefenceDev
No Result
View All Result
Home Networking FortiGate

Fortigate and Ubuntu Bash Automation

by neo
September 3, 2026
in FortiGate
0
Solution: Connect from Ubuntu to the FortiGate Firewall and Execute Command (Bash Script)

Solution: Connect from Ubuntu to the FortiGate Firewall and Execute Command (Bash Script)

0
SHARES
266
VIEWS
Share on FacebookShare on LinkedIn
Table of Contents
  • Connect from Ubuntu Shell to the FortiGate Firewall
    • Purpose of the Script
    • My Script to Conncet to the FortiGate
  • GitHub Repository

Connect from Ubuntu Shell to the FortiGate Firewall

Managing a FortiGate firewall from a remote system can save valuable time and effort, especially when automation is key to streamlining routine tasks. By connecting from an Ubuntu system to a FortiGate device and executing commands via Bash scripts, you can effectively manage firewall configurations, monitor traffic, or perform troubleshooting tasks without manual intervention. In this post, we’ll walk you through the steps to connect your Ubuntu machine to FortiGate, and show you how to create and execute Bash scripts for seamless command execution, improving both efficiency and control over your network security.

Purpose of the Script

I needed to connect to a FortiGate device (CLI) from my Ubuntu server within the local network, especially when the internet was down. The goal was to disable the WAN interface, wait for 60 seconds, and then re-enable the interface. To accomplish this, I used execute_commands function in bash script.

This function is designed to execute a series of commands on a remote server via SSH. Here’s a detailed breakdown of how it works:

execute_commands() {
    local commands=("$@")
  • execute_commands() is the name of the function.
  • local commands=(“$@”) creates a local array named commands that contains all the arguments passed to the function.
sshpass -p "$PASSWORD" ssh -o StrictHostKeyChecking=no -p $PORT $USER@$HOST << EOF
$(for cmd in "${commands[@]}"; do echo "$cmd"; done)
EOF
  • sshpass -p “$PASSWORD” uses the sshpass utility to provide the SSH password stored in the PASSWORD variable. This allows for non-interactive password authentication.
  • ssh -o StrictHostKeyChecking=no -p $PORT $USER@$HOST runs the ssh command with the following options:
  • -o StrictHostKeyChecking=no: Disables strict host key checking, which prevents SSH from asking for confirmation if the host key is not already in the known hosts file.
  • -p $PORT: Specifies the SSH port to connect to, using the value of the PORT variable.
  • $USER@$HOST: Specifies the remote user and host to connect to, using the values of the USER and HOST variables.

Command execution block:

<< EOF
$(for cmd in "${commands[@]}"; do echo "$cmd"; done)
EOF
  • << EOF initiates a here-document, which allows you to provide input to the SSH command.
  • $(for cmd in “${commands[@]}”; do echo “$cmd”; done) is a command substitution that loops over the commands array, printing each command. This results in each command being executed on the remote server.
  • EOF marks the end of the here-document.

Example usage:

execute_commands "ls -l" "pwd" "whoami"

My Script to Conncet to the FortiGate

I saved it under: /opt/scripts/fortigate/fgt_gw_check.sh

nano fgt_gw_check.sh
#!/bin/bash

# Variables
HOST="HOST_IP"
USER="USER"
PASSWORD="PASSWORD" # Not recommended to hardcode passwords; consider using SSH keys or prompting for password
# Port, in my case I changed the port from 22 to 11022
PORT=11022 
LOG_FILE="/opt/scripts/log/fortigate_disable_enable_wan1_$(date +%Y-%m-%d).log" # Location for log file


# Commands to disable wan1 interface
disable_commands=(
    "config system interface"
    "edit wan1"
    "set status down"
    "end"
)

# Commands to enable wan1 interface
enable_commands=(
    "config system interface"
    "edit wan1"
    "set status up"
    "end"
)

# Function to send commands via SSH
execute_commands() {
    local commands=("$@")
    sshpass -p "$PASSWORD" ssh -o StrictHostKeyChecking=no -p $PORT $USER@$HOST << EOF
$(for cmd in "${commands[@]}"; do echo "$cmd"; done)
EOF
}

# Disable wan1 interface
#echo "Disabling wan1 interface..."
echo "$(date): Disabling wan1 interface..."  >> $LOG_FILE

execute_commands "${disable_commands[@]}"

# Wait for 60 seconds
#echo "Waiting for 60 seconds..."
echo "$(date): Waiting for 60 seconds..."  >> $LOG_FILE

sleep 60

# Enable wan1 interface
#echo "Enabling wan1 interface..."
echo "$(date): Enabling wan1 interface..." >> $LOG_FILE

execute_commands "${enable_commands[@]}"

echo "$(date): Done."  >> $LOG_FILE

In one of the following posts, I will publish my internet monitoring script.

GitHub Repository

The complete source code for this FortiGate automation script is publicly available in the GitHub repository. There you can review the project structure, download the latest version, or customize the script to match your operational workflow. The repository can be accessed at:

???? https://github.com/defence-dev/defencedev-public/tree/main/fortigate/fortigate_ubuntu_automation

Feel free to fork the repository, submit improvements, or adapt the script as a foundation for building your own automated FortiGate management solutions on Ubuntu systems.

UPDATE: My script to monitor the internet connection

defencedev.com

 

Tags: automationbashfortigatelinux
Previous Post

Linux Files Operation: chown (Change Ownership of the Files)

Next Post

Solution: CPU Temperature Monitoring on Raspberry Pi

neo

Related Posts

FortiGate Brute Force Protection: Ban IPs After Failed Admin Login
FortiGate

FortiGate Brute Force Protection: Ban IPs After Failed Admin Login

FortiGate Country Address Object Generator – Block or Allow Access by Country
FortiGate

FortiGate Country Address Object Generator – Block or Allow Access by Country

Home Lab with Fortinet FortiGate 60D Firewall
FortiGate

Home Lab with Fortinet FortiGate 60D Firewall

My Ultimate FortiGate Command Cheat Sheet
FortiGate

My Ultimate FortiGate Command Cheat Sheet

Setting Up IPS (Intrusion Detection System) Profiles on FortiGate to Detect Web Attacks
FortiGate

Setting Up IPS (Intrusion Detection System) Profiles on FortiGate to Detect Web Attacks

Protect a Self-Hosted Application from Brute Force Attacks with FortiGate
FortiGate

Protect a Self-Hosted Application from Brute Force Attacks with FortiGate

Next Post
Solution: CPU Temperature Monitoring on Raspberry Pi4

Solution: CPU Temperature Monitoring on Raspberry Pi

Recommended

Lesson 7: Bash Functions with Examples

Lesson 7: Bash Functions with Examples

MikroTik: Remote Access to Headquarter with OpenVPN

Configure OpenVPN Server on MikroTik Router for Remote Access

Briefly unavailable for scheduled maintenance. Check back in a minute.

WordPress Stuck in Maintenance Mode After Plugin Update

Promox Telegram Alert Example

Configure Telegram Notifications in Proxmox VE Using Webhooks

Fix Proxmox VM Snapshot “Out of Space” Errors

Fix Proxmox VM Snapshot “Out of Space” Errors

Categories

  • Linux
    • Bash Scripting
    • Commands&Tips
    • Monitoring
  • Networking
    • FortiGate
    • MikroTik
  • Projects & Case Studies
  • Self-Hosted
    • AdGuard
    • Immich
    • Nextcloud
    • WordPress
  • Virtualization
    • Proxmox
    • VMware
  • Web Tools

DefenceDev Tutorials

defencedev Logo

Whether you’re just starting or looking to expand your skills, I hope you find useful information and engaging discussions here. Let me take you through my journey and the goals behind this space!

Follow Us

Recent News

Briefly unavailable for scheduled maintenance. Check back in a minute.

WordPress Stuck in Maintenance Mode After Plugin Update

Promox Telegram Alert Example

Configure Telegram Notifications in Proxmox VE Using Webhooks

  • Site Map
  • Privacy Policy
  • Facebook Page
  • GitHub
  • Disclaimer
  • Contact
  • About Me

© 2025 defencedev.com - All rights reserved.

No Result
View All Result
  • Home
  • Linux
    • Bash Scripting
      • Bash Scripting Lessons
    • Commands&Tips
    • Monitoring
  • Networking
    • FortiGate
    • MikroTik
  • Projects & Case Studies
  • Self-Hosted
    • AdGuard
    • Immich
    • Nextcloud
    • WordPress
  • Virtualization
    • Proxmox
    • VMware
  • Tools

© 2025 defencedev.com - All rights reserved.