Automate scp transfers using password

Code is community submitted, community supported, and recognized as ‘Use At Your Own Risk’.

Article K175: Transferring files to or from an F5 system discuss the secure transfer of files, whereas almost all the remote host machine will have password-based authentication or public key authentication, In this article demonstrates using the remote host using the password to transfer UCS/any other files. The below expect script demonstrates.

 

#!/usr/bin/expect -f

set timeout -1

# set fileName as hostname
set fileName "[exec /bin/hostname]-[exec date +%d%b%Y]"

######################################
# The below involves multiple steps
# 1. spawn a local shell
# 2. save ucs file to /var/local/ucs
# 3. Do scp to remote host
# 4. remove the ucs file
# 5. Accept Yes
# 6. Provide password
#######################################
spawn bash -c "tmsh save sys ucs $fileName && scp /var/local/ucs/$fileName.ucs root@10.218.49.137:/var/local/ && rm -rf /var/local/ucs/$fileName.ucs"
expect {
  -re ".*es.*o.*" {
    exp_send "yes\r"
    exp_continue
  }
  -re ".*sword.*" {
    exp_send "default\r"
  }
}

expect eof

 

Save the script into separate file ``scp_ucs.sh`` and chang execution permission of the script ``chmod +x scp_ucs.sh``

If you want to execute the script periodically add the script to the cron utility on the BIG-IP system that will execute based on your configuration.

Updated Jan 31, 2023
Version 2.0