• Contact
  • About Me
  • Privacy Policy
DefenceDev
  • Home
  • Blog
  • Linux Tutorials
    • Bash Scripting Lessons
    • Commands
    • Networking
  • Solutions
    • Docker
  • Network Tutorials
    • FortiGate
    • MikroTik
  • Projects
    • AdGuard
    • Immich
    • Nextcloud
    • WordPress
  • Cloud
  • Video Tutorials
    • YouTube Channel
    • MikroTik Videos
No Result
View All Result
  • Home
  • Blog
  • Linux Tutorials
    • Bash Scripting Lessons
    • Commands
    • Networking
  • Solutions
    • Docker
  • Network Tutorials
    • FortiGate
    • MikroTik
  • Projects
    • AdGuard
    • Immich
    • Nextcloud
    • WordPress
  • Cloud
  • Video Tutorials
    • YouTube Channel
    • MikroTik Videos
No Result
View All Result
DefenceDev
No Result
View All Result
ADVERTISEMENT
Home Network Tutorials FortiGate

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

neo by neo
May 27, 2025
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
87
VIEWS
Share on FacebookShare on Twitter
ADVERTISEMENT

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.

UPDATE: My script to monitor the internet connection

ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT

defencedev.com

 

About The Author

neo

See author's posts

Tags: fortigatelinux
ADVERTISEMENT
Previous Post

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

Next Post

Solution: Ubuntu Internet Connection Monitoring Script (Create a Log File, Possible to Send per Email)

neo

neo

Next Post
Solution: Ubuntu Internet Connection Monitoring Script (Create a Log File, Possible to Send per Email)

Solution: Ubuntu Internet Connection Monitoring Script (Create a Log File, Possible to Send per Email)

Follow Us

  • Trending
  • Comments
  • Latest
MikroTik: Export Configuration in Text File

MikroTik: Export Configuration in Text File

Fortinet FortiGate: Static Route Configuration via GUI and CLI

Fortinet FortiGate: Static Route Configuration via GUI and CLI

Immich: Installation on Ubuntu 22.04

Immich: Installation on Ubuntu 22.04

Fortinet FortiGate Upgrade Path Tool

Fortinet FortiGate Upgrade Path Tool

NextCloud: Port Forwarding - Essential Ports for Smooth Functionality

NextCloud: Port Forwarding – Essential Ports for Smooth Functionality and Remote Access

Organizing and Managing Photos with Immich: Features I Use

Organizing and Managing Photos with Immich: Features I Use

Install Ubuntu 22.04 on Oracle VM Virtual Box

Install Ubuntu 22.04 on Oracle VM Virtual Box

Linux Directories: cd (Change Directory), pwd (Print Working Directory), mkdir (Make Directory)

Linux Directories: cd (Change Directory), pwd (Print Working Directory), mkdir (Make Directory)

Install WordPress on Raspberry Pi 4 with Docker (Ubuntu 22.04)

Install WordPress on Raspberry Pi 4 with Docker (Ubuntu 22.04)

Install Nextcloud on Raspberry Pi 4 with Docker (Ubuntu 22.04)

Install Nextcloud on Raspberry Pi 4 with Docker (Ubuntu 22.04)

Install Nginx Proxy Manager on Raspberry Pi 4 with Docker (Ubuntu 22.04)

Install Nginx Proxy Manager on Raspberry Pi 4 with Docker (Ubuntu 22.04)

Install Docker on Raspberry Pi 4 with Ubuntu 22.04

Install Docker on Raspberry Pi 4 with Ubuntu 22.04

Recent News

Install WordPress on Raspberry Pi 4 with Docker (Ubuntu 22.04)

Install WordPress on Raspberry Pi 4 with Docker (Ubuntu 22.04)

Install Nextcloud on Raspberry Pi 4 with Docker (Ubuntu 22.04)

Install Nextcloud on Raspberry Pi 4 with Docker (Ubuntu 22.04)

Install Nginx Proxy Manager on Raspberry Pi 4 with Docker (Ubuntu 22.04)

Install Nginx Proxy Manager on Raspberry Pi 4 with Docker (Ubuntu 22.04)

Install Docker on Raspberry Pi 4 with Ubuntu 22.04

Install Docker on Raspberry Pi 4 with Ubuntu 22.04

MikroTik: Check Your Wireless Password

MikroTik: Check Your Wireless Password

ADVERTISEMENT

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

Browse by Category

  • Blog
  • Cloud
    • Private
  • Linux Tutorials
    • Bash Scripting Tutorials
    • Commands
    • Networking
  • Network Tutorials
    • FortiGate
    • MikroTik
  • Projects
    • AdGuard
    • Immich
    • Nextcloud
    • WordPress
  • Solutions
    • Docker
  • Video Tutorials
    • MikroTik Videos

Recent News

Install WordPress on Raspberry Pi 4 with Docker (Ubuntu 22.04)

Install WordPress on Raspberry Pi 4 with Docker (Ubuntu 22.04)

Install Nextcloud on Raspberry Pi 4 with Docker (Ubuntu 22.04)

Install Nextcloud on Raspberry Pi 4 with Docker (Ubuntu 22.04)

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

© 2025 defencedev.com - All rights reserved.

No Result
View All Result
  • Home
  • Blog
  • Linux Tutorials
    • Bash Scripting Lessons
    • Commands
    • Networking
  • Solutions
    • Docker
  • Network Tutorials
    • FortiGate
    • MikroTik
  • Projects
    • AdGuard
    • Immich
    • Nextcloud
    • WordPress
  • Cloud
  • Video Tutorials
    • YouTube Channel
    • MikroTik Videos

© 2025 defencedev.com - All rights reserved.