Register now and start sharing your code snippets.
-->

Using expect for automation of bulk scp copying.

Shell Script (Bash) posted 3 months ago by marko

Expect can come in handy when you can’t configure ssh public key authentication on the servers :) (and the system “architect” hasn’t yet realized the wonderfulness of a log host).

   1  #!/bin/bash
   2  #
   3  # Usage: script <username> <password> <build>
   4  #
   5  # Example ./copy_logs_from_production.sh marko hubbabubba current
   6  #
   7  
   8  username=$1
   9  password=$2
  10  build=$3
  11  mkdir $build
  12  
  13  instance_1_server=10.0.0.1
  14  instance_2_server=10.0.0.1
  15  instance_3_server=10.0.0.2
  16  instance_4_server=10.0.0.2
  17  instance_5_server=10.0.0.3
  18  instance_6_server=10.0.0.3
  19  instance_7_server=10.0.0.4
  20  instance_8_server=10.0.0.4
  21  instance_9_server=10.0.0.5
  22  
  23  servers=("$instance_1_server" "$instance_2_server" "$instance_3_server" "$instance_4_server" "$instance_5_server" "$instance_6_server" "$instance_7_server" "$instance_8_server" "$instance_9_server" )
  24  i=1
  25  for server in ${servers[@]}; do
  26    expect -c "
  27              # exp_internal 1 # uncomment for debugging
  28              spawn /usr/bin/scp $username@$server:/var/logs/application/$build/server${i}/error.log $build/error-${i}.log
  29              expect { 
  30                "*password:*" { send $password\r\n; interact } 
  31                eof { exit }
  32              }
  33              exit
  34              "
  35    let "i=i+1"
  36  done
  37  

Tagged expect, bash, automation, scp, bulk