F5 Automation - TCL & Bash

Problem this snippet solves:

This is a really simple way to automate CLI command execution on multiple F5 devices using Bash & TCL scripting.

How to use this snippet:

On a linux machine that is utilized to connect to the F5 device:

Create a directory

mkdir F5_Check

Within the "F5_Check" directory, create the following 3 files:

F5_Host.txt (This file contains F5's IP address)

F5_Bash_v1 (This is the bash script used to collect username/password for F5)

F5_Out_v1.exp (This is the TCL script executes the relevant commands on F5)

Explanation of the 3 files:

File Content: F5_Out_v1.exp is provided as code share. This is the main TCL script that is utiliezd to execute CLI commands on multiple F5 devices.

File Content: F5_Bash_v1

#!/bin/bash
# Collect the username and password for F5 access
echo -n "Enter the username "
read -s -e user
echo -ne '\n'
echo -n "Enter the password "
read -s -e password
echo -ne '\n'

# Feed the expect script a device list & the collected username & passwords
for device in `cat ~/F5_Check/F5_Host.txt`; do
 ./F5_Out_v1.exp $device $password $user ;
 done

File Contents: F5_Host.txt

This contains the management IP of the F5 devices.

Example:

cat F5_Host.txt 
    10.12.12.200
    10.12.12.201
    10.12.12.202 
    10.12.12.203

Code :

#!/usr/bin/expect -f

# Set variables
set hostname [lindex $argv 0]
set password [lindex $argv 1]
set username [lindex $argv 2]

# Log results
log_file -a ~/F5_Check/F5LOG.log

# Announce which device we are working on and the time
send_user "\n"
send_user ">>>>>  Working on $hostname @ [exec date] <<<<<\n"
send_user "\n"

# SSH access to device
spawn ssh $username@$hostname

expect {
"no)? " { 
send "yes\n"
expect "*assword: "
sleep 1
send "$password\r"
}
"*assword: " { 
sleep 1
send "$password\r"
 }
}

expect "(tmos)#"
send "sys\n"
expect "(tmos.sys)#"

send "show software\n"
expect "#"
send "exit\n"
expect "#"
send "quit\n"

expect ":~\$"
exit

Tested this on version:

11.5
Updated Jun 06, 2023
Version 2.0
  • Well I dont have /usr/bin/expect in my linux. Anyother alternative to this.

    F5_Bash_v1: ./F5_Out_v1.exp: /usr/bin/expect: bad interpreter: No such file or directory
    
  • Well I managed to execute this by installing expect package. But the F5LOG.log file its not appealing. Its has whole lot of entries (spawn SSH till running the commands and quiting the ssh session). Using expect can we just get the outputs alone.