Skip to content

rConfig - Expect Script Example

2 mins V7 Pro

Overview

The example below is based on an Expect script to connect to a Radware Alteon load balancer, and dump its configuration.

SIE Feature setup

To get started with the SIE feature, you must first enable it in rConfigs .env file. Otherwise any scripts that are called using the methods below will not run. This must be completed by someone with root access to the rConfig Server.

  1. Login to the rConfig Server
  2. Edit the .env file
/var/www/html/rconfig7/current/.env
cd /var/www/html/rconfig7/current && vim .env
# Add or edit the line below
SCRIPT_FEATURE_ENABLED=true
  1. Save and exit
  2. Clear the app cache
rConfig Server Cli
cd /var/www/html/rconfig7/current && php artisan rconfig:clear-all

Script parameters

As of V7.0 rConfig support 5 parameters that can be sent to the script for use within your code. These are;

  • {script_path}
  • {device_ip}
  • {device_username}
  • {device_password}
  • {device_enable_password}

We will see later how to use these when calling the script.

Deploy script to server

When deploying a script to the server it is assumed 1) that is was tested on the rConfig server itself, and 2) the binary to run the script is present. You will need the binary path later.

Finding Binary paths on Rocky Linux

To locate the binaries for expect, perl, bash, and Python 3 on your Rocky Linux server, you can use the which command. Here’s how to do it for each:

Expect:

rConfig Server Cli
which expect
$ /usr/bin/expect
Where to deploy scripts

All scripts must be located in the following directory. Please create it if it does not exist.

rConfig Server Cli
/var/www/html/rconfig7/current/storage/app/rconfig/scripts/

Add a connection template

We have a brand new type of connection template available specifically for devices that will use scripts. See an example below. To add the new template, in rConfig UI go to Devices > Device Templates and add a new template. Clear out the default template, and paste in the template below. We will use this when creating our device that will use a custom script. You may need to adjust the timeout values depending on your scripts runtime.

# rConfig connection template - DO NOT EDIT DIRECTLY
## Notes:
## Remember to update permissions for the templates folder after uploading new template files.
## run 'chown -R apache /var/www/html/rconfig7' on your rconfig server CLI
## all items below that contain free text should be contained within quotation marks " "
## For new community submitted templates visit: https://github.com/rconfig/rConfig-templates
main:
name: "alteon_test_script_template"
desc: "This is an expect script"
connect:
timeout: 30
idleTimeout: 30
protocol: script

Add command to Category

The next step in the provcess is to add the command to the category. To do this, in rConfig UI go to Devices > Commands and add a new command. The command will take the following format. It is advisable to set the ‘Alternate Filemane’ for this command, otherwise the resulting config file names maybe lenghty and unreadable. See screehsot below.

Terminal window
/usr/bin/expect {script_path}alteon_cdump_test_script.exp {device_ip} {device_username} {device_password}

Lets break down the command. The first part if the path to the binary, the second is the name of the script which must be prafaced by the variable {script_path}. Following the script name, you can enter the above variables in any order, and none are mandatory. They just depend on the parameters that your script expect. You will see in this example, the parameters are {device_ip}, {device_username}, and {device_password}, and the {device_enable_password} is not called.

We have a sample expect script below, and the above command will call that script with the parameters that we set in the script.

/var/www/html/rconfig7/current/storage/app/rconfig/scripts/alteon_cdump_test_script.exp
#!/usr/bin/expect -f
# Alteon Virtual IPs
set timeout 10
set devip [lindex $argv 0]
set user [lindex $argv 1]
set pwd [lindex $argv 2]
send_user "IP: $devip Connect as $user\n"
spawn ssh -o "KexAlgorithms +diffie-hellman-group1-sha1" -o "StrictHostKeyChecking no" -o "UserKnownHostsFile junk" $user@$devip
expect {
timeout { send_user "\nERR: Timeout Exceeded - Check Host\n"; exit 1 }
eof { send_user "\nERR: SSH Connection To $devip Failed\n"; exit 1 }
"*#" {}
"*assword:" {
send -- "$pwd\r"
}
}
expect {
"*Confirm seeing above note" {
send -- "y\r"
}
"*#" {}
}
send -- "lines 0\r"
expect "*#"
send -- "cdump\r"
expect -ex {[y/n]:}
send -- "n\r"
set timeout 80
expect "script end"
set timeout 10
expect "*#"
send -- "quit\r"
send_user "\n"

There are several great tutorials online on how to develop expect scripts, this is a good one by Digital Ocean that explains the basics. https://www.digitalocean.com/community/tutorials/expect-script-ssh-example-tutorial

New command with script

Add to the device

Now, all thats requied is to add the connection template and the correct cateogry (which contains the command we just added) to the device.

Run tests

We have tried to keep the setup and tests as ‘rConfig’ as possible. This means, to test the script, you can simply run the debug command in rConfig as you would with SSH and telnet connections.

Github Repo

As with all rConfig open standards, we will keep as many examples of the scripts and templates in the repo as possible. Please visit our public templates repo at https://github.com/rconfig/rConfig-templates. We will preface all SIE scripts and templates with SIE in the directory name.