expect snippets

Using expect for automation of bulk scp copying.

Tagged bash, expect, automation, scp, bulk  Languages bash

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).

#!/bin/bash
#
# Usage: script <username> <password> <build>
#
# Example ./copy_logs_from_production.sh marko hubbabubba current
#

username=$1
password=$2
build=$3
mkdir $build

instance_1_server=10.0.0.1
instance_2_server=10.0.0.1
instance_3_server=10.0.0.2
instance_4_server=10.0.0.2
instance_5_server=10.0.0.3
instance_6_server=10.0.0.3
instance_7_server=10.0.0.4
instance_8_server=10.0.0.4
instance_9_server=10.0.0.5

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" )
i=1
for server in ${servers[@]}; do
  expect -c "
            # exp_internal 1 # uncomment for debugging
            spawn /usr/bin/scp $username@$server:/var/logs/application/$build/server${i}/error.log $build/error-${i}.log
            expect { 
              "*password:*" { send $password\r\n; interact } 
              eof { exit }
            }
            exit
            "
  let "i=i+1"
done

How to back up all MySQL database instances with Ruby

Tagged mysqldump, mysql, backup, expect, pty  Languages ruby
require 'rubygems'
require 'sequel'
require 'expect'
require 'pty'
require 'pp'

#$expect_verbose = true 

def exec_pty(cmd)
  PTY.spawn(cmd) do |reader, writer, pid|
    reader.expect(/Enter password/) do |line|
      writer.puts ''
    end 

    while line=reader.gets
#      print line
    end
  end 
end

user      = 'fuuuuuck'
password  = ''
host      = 'localhost'
encoding  = 'utf8'
dir       = '/tmp'
timestamp = Time.now.strftime('%d-%m-%y')

connection = Sequel.mysql nil, :user => user, :password => password, :host => host, :encoding => encoding

databases = []
connection['show databases;'].each do |db|
  databases << db[:Database]
end
databases = databases - ['mysql', 'test', 'information_schema']


databases.each do |db|

  file = File.join(dir, "#{db}_#{timestamp}.sql")
  p file
  cmd = "mysqldump -u#{user} -p#{password} -h#{host} -Q -c -C --add-drop-table --add-locks --quick --lock-tables #{db} > #{file}"

  
  result = exec_pty(cmd)
end