SSH passing password from script.
Sometimes you want to automate the execution of commands on each DB2 server you own from a computer where there is no db2 client installed. Here is a little trick showing you how to do it.
From Ubuntu:
sudo apt-get install expect
Create a script like this one: sshlogin.exp
#!/usr/bin/expect -f
set password [lrange $argv 0 0]
set ipaddr [lrange $argv 1 1]
set username [lrange $argv 2 2]
set command [lrange $argv 3 3]
set timeout -1
spawn ssh $username@$ipaddr $command
match_max 100000
expect "*?assword:*"
send -- "$password\r"
send -- "\r"
expect eof
Create your automation script: fixpack.sh
#!/bin/bash
arrServers=(db2srv1 db2srv2 db2srv3 db2srv4 db2srv5 db2srv6 db2srv7 db2srv8 db2srv9 db2srv10)
username=db2usr
password=123456
command=db2ls
for server in ${arrServers[@]}
do
echo $server
echo "--------"
./sshlogin.exp $password $server $username $command
echo " "
done
Then just run fixpack.sh, it will show you the result of db2ls !