unix自动登录Telnet,实现查看多台服务器硬盘及数据表空间使用情况

以下代码在Sun Solaris 10 系统上运行成功。

  1. #######################Auto Login Telnet###########################
  2. # 在网上找到了一些自动登录的例子,现在用于更实用的例子。          #
  3. #                                                                 #
  4. # 下面代码主要用于,简单的实现了,在一台服务上运行一个脚本,       #
  5. # 就可以查看其它服务器上的硬盘及数据表空间使用情况,              #
  6. # 想必还有一些更好的办法,或更安全的方法来实现。                  #
  7. ###################################################################
  8. #!/bin/bash
  9. ipAddressList="192.168.1.1 192.168.1.2 192.168.1.3 192.168.1.5"     #IP地址列表;
  10. rm LookResult.log  #删除原来的日志。这里如何能做个判断会再好点。
  11. tmptty=`tty`              ;echo 1:$tmptty
  12. tmptty=`basename $tmptty` ;echo 2:$tmptty
  13. tmpname=`whoami`          ;echo 3:$tmpname
  14. inp1="telnetUserName"     ;echo 4:$inp1           #telnet 登录时用户名
  15. inp2="telnetPassword"     ;echo 5:$inp2           #telnet 登录时用户密码
  16. inp3="df -h"              ;echo 6:$inp3           #查看磁盘空间使用情况。 
  17. inp4="sqlplus userName/password<<!"               #登录数据。
  18. inp5=" select t1.tablespace_name ""表空间"","      #表空间使用情况
  19. inp51=" t1.r ""可用空间"","
  20. inp52=" t2.a ""总大小"" from "
  21. inp53=" (select tablespace_name ,sum(bytes)/1024/1024 as r from dba_free_space t1 group by tablespace_name)t1,"
  22. inp54=" (select tablespace_name , sum(bytes)/1024/1024 as a from dba_data_files group by tablespace_name) t2 "
  23. inp55=" where t1.tablespace_name=t2.tablespace_name;"
  24. inp6="!"
  25. echo 8:$inp4
  26. echo $inp5 $inp51 $inp52 $inp53 $inp54 $inp55
  27. echo $inp6
  28. inputfile=in 
  29. outputfile=out.log
  30. for ipAddress in $ipAddressList   #循环IP得址列表
  31. do
  32.     echo "******************************$ipAddress Looking Beging **********************" >>LookResult.log
  33.     echo "******************************$ipAddress Looking Beging **********************"
  34.     rm -fr $inputfile 
  35.     rm -fr $outputfile 
  36.     mknod $inputfile p 
  37.     touch $outputfile 
  38.     
  39.     exec 7<>$outputfile 
  40.     exec 8<>$inputfile 
  41.     telnet $ipAddress <&8 >&7 &       #开始Telent 登录
  42.     
  43.     sleep 1; echo $inp1 >> $inputfile #用户名
  44.     sleep 1; echo $inp2 >> $inputfile #密码
  45.     sleep 1; echo $inp3 >> $inputfile #查看磁盘空间使用情况命令。
  46.     sleep 1; echo $inp4 >> $inputfile #登录Sqlplus
  47.     sleep 1; echo $inp5$inp51$inp52$inp53$inp54$inp55 >> $inputfile #查看表空间使用情况语句
  48.     sleep 1; echo $inp6 >> $inputfile #语句结束退出Sqlplus
  49.     sleep 1; echo exit >> $inputfile  #退出Telnet登录
  50.     
  51.     ps -ef | grep telnet | grep -v grep | grep -v telnetd | grep $tmptty | grep $tmpname | awk '{print " kill -9", $2}' | sh #结束相关进程
  52.     ps -ef | grep tail | grep -v grep | grep -v telnetd | grep $tmptty | grep $tmpname | awk '{print " kill -9", $2}' | sh   #结束相关进程
  53.     
  54.     cat $outputfile
  55.     more $outputfile >> LookResult.log
  56.     echo "******************************$ipAddress Looking over ************************" >> LookResult.log
  57.     echo "******************************$ipAddress Looking over ************************"
  58. done
  59. exit #退出.sh


posted on 2008-12-01 20:39  小土泥  阅读(622)  评论(0编辑  收藏  举报

导航