这个脚本是获取目标机器的db信息和os信息的。os信息很好获取,db的信息包含db名字,db版本以及所有的db instance,db信息的获取稍显复杂,下面是整个脚本代码:
关键字: awk, sed, grep, $, test -z, ftp, 以及重定向>,>>
#!/bin/sh
name=$(uname -s -n | awk '{{print $2}')
ip=$(ifconfig -a|grep inet|grep -v 127.0.0.1|grep -v inet6|awk '{{print $2}'|tr -d 'addr:' )
os=$(uname -s -n | awk '{{print $1}' )
dbinstpath=$(find /opt -name db2ilist|grep 'inst'|sed -n '1,1p')
#dbid=$($dbinstpath|sed -n '1,1p')
if test -z "$dbinstpath" //判断dbinstpath变量是否为空
then
comm="no db2ilist on target computer"
osdbinfo=$name","$ip","$os","null","null","null","$comm"
"$osdbinfo
else
dbinst=$($dbinstpath)
if test -z "$dbinst"
then
echo "there is no any db instance on that server, so can not retrieve db info" > getDBinfo.log 2>&1
else
for i in $dbinst
do
cmp=$(su - $i|grep "Unable to change directory")
if test -z "$cmp"
then
dbver=$(su - $i -c db2level | grep 'DB2 v' | awk '{{print $5}'| tr -d '"|,' )
dbname=$(su - $i -c db2 list db directory | grep 'Database name' | awk '{{print $4}' )
comm="_"
else
comm="can not change to directory for db instance:"$i
dbver="null"
dbname="null"
fi
osdbinfo=$name","$ip","$os","$dbver","$dbname","$i","$comm"
"$osdbinfo
done
fi
fi
csvtitle='0os_name, 0os_ip, 0os, 0db_version, 0db_name, 0db_inst'
echo $csvtitle"
"$osdbinfo > db2info.csv 2>&1
//ftp下载
ftpsvr="9.84.194.234"
ftp -i -v -n <<eof
open $ftpsvr
user ken 123456
lcd /tmp
get "db2info.csv" "db2info.txt"
bye
eof
cd /tmp
chmod u+rwx db2info.txt //修改文件权限
echo $osdbinfo>>db2info.txt 2>&1
sort -u db2info.txt -o db2info.txt //排序
sed '/^$/d' db2info.txt>db2info_.txt //删除空白行
//ftp上传文件
ftp -i -v -n <<eos
open $ftpsvr
user ken 123456
put db2info_.txt db2info.csv
bye
eos
rm db2info_.txt