• Contact
  • About Me
  • Privacy Policy
  • Disclaimer
DefenceDev
  • Home
  • Blog
  • Linux Tutorials
    • Bash Scripting Lessons
    • Commands
    • Networking
    • Bash Scripts
  • 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
    • Bash Scripts
  • 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
August 2, 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
128
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.

ADVERTISEMENT

UPDATE: My script to monitor the internet connection

ADVERTISEMENT

defencedev.com

ADVERTISEMENT

 

About The Author

neo

See author's posts

Tags: bash-scriptingfortigatelinux
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

Related Posts

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

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

FortiGate DDoS Protection: Configure DoS Policies to Secure Self-Hosted Applications
FortiGate

FortiGate DDoS Protection: Configure DoS Policies to Secure Self-Hosted Applications

Fortinet FortiGate Site-to-Site IPsec VPN Troubleshooting
FortiGate

Fortinet FortiGate Site-to-Site IPsec VPN Troubleshooting

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)

Recommended

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

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

Nextcloud: Security Concept with FortiGate Firewall

Nextcloud: Security Concept with FortiGate Firewall

MikroTik OpenVPN: Sound Alert When VPN Tunnel Fails

MikroTik OpenVPN: Sound Alert When VPN Tunnel Fails

MikroTik: OpenVPN Client Failover Script

MikroTik: OpenVPN Client Failover Script

GitHub: How to Add a Script or a Folder to Your Repository

GitHub: How to Add a Script or a Folder to Your Repository

Categories

  • 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
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

Recent News

MikroTik OpenVPN: Sound Alert When VPN Tunnel Fails

MikroTik OpenVPN: Sound Alert When VPN Tunnel Fails

MikroTik: OpenVPN Client Failover Script

MikroTik: OpenVPN Client Failover Script

  • 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
    • Bash Scripts
  • Solutions
    • Docker
  • Network Tutorials
    • FortiGate
    • MikroTik
  • Projects
    • AdGuard
    • Immich
    • Nextcloud
    • WordPress
  • Cloud
  • Video Tutorials
    • YouTube Channel
    • MikroTik Videos

© 2025 defencedev.com - All rights reserved.