Shell脚本远程连接DB2,执行语句


 


点击(此处)折叠或打开

  1. #!/bin/bash

  2. filepath=$(cd "$(dirname "$0")"; pwd)
  3. name=$HOSTNAME
  4. mysqluser=mysql
  5. server_ip=192.168.207.212
  6. mysqlip=10.27.81.100
  7. echo "insert into MONITOR3.BASE_INFO(IP,HOST_NAME,INS_NAME)values ($mysqlip,'$name','$mysqluser');">$filepath/insert.sql
  8. echo "select ip from MONITOR3.BASE_INFO where ip='$mysqlip';">>$filepath/insert.sql
  9. su - $mysqluser -c "scp -r $filepath/insert.sql db2inst1@$server_ip:/home/db2inst1" 2>&1 >2.log
  10. check_insert=`su - $mysqluser -c "ssh -o ServerAliveInterval=60 -o ServerAliveCountMax=20 -o StrictHostKeyChecking=no -o ConnectionAttempts=5 -o PasswordAuthentication=no -o BatchMode=yes -p 22 db2inst1@$server_ip 'db2 connect to mondb;db2 -tf /home/db2inst1/insert.sql'"` 2>&1 >2.log
  11. check_insert=`echo "$check_insert" |grep $mysqlip`
  12.  
  13. if [[ "${check_insert}" == "" ]
  14. then
  15.         echo "`date` - [error] '$mysqlip' insert into db2 server($server_ip) error ,Please check the script!Error code:701" 2>&1 >2.log
  16.         exit 701
  17. else
  18.         echo "`date` - [info] '$mysqlip' insert into db2 server($server_ip) successfully!" 2>&1 >2.log
  19. fi

autossh 需要提前配置好。 

来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/29071259/viewspace-1379761/,如需转载,请注明出处,否则将追究法律责任。

转载于:http://blog.itpub.net/29071259/viewspace-1379761/

你可能感兴趣的:(shell,数据库,运维)