• expect 批量监控主机


    [oracle@OAPRIMARY shell]$ cat expect.sh 
    while read line
    do
    user=`echo $line | awk '{print $1}'`
    ip=`echo $line |awk '{print $2}'`
    passwd=`echo $line | awk '{print $3}'`
    #把shell中的$user $ip $passwd参数传递给expect脚本
    expect   expect2.exp $user $ip $passwd
    done < name.tmp
    
    
    
    [oracle@OAPRIMARY shell]$ cat expect2.exp 
    #!/usr/bin/expect
     # 设置超时时间为 60 秒
    # set timeout  60
     # 设置要登录的主机 IP 地址
    # set host=$host
     # 设置以什么名字的用户登录
    # set ip=$ip
     # 设置用户名的登录密码
    # set passwd=$passwd
     
    #把shell的参数传递给expect脚本
    set user [lindex $argv 0]
    set ip  [lindex $argv 1]
    set passwd  [lindex $argv 2]
    
     #spawn 一个 ssh 登录进程
     spawn  ssh $user@$ip
     # 等待响应,第一次登录往往会提示是否永久保存 RSA 到本机的 know hosts 列表中;等到回答后,在提示输出密码;之后就直接提示输入密码
    expect {
        "(yes/no)?" {
            send "yes
    "
            expect "assword:"
            send "$passwd
    "
        }
            "assword:" {
            send "$passwd
    "
        }
     }
    
    
    # 下面测试是否登录到 
    expect "$" 
    send "df -h
    "
    send "tail -10 /oracle/app/admin/perass/bdump/alert_perass.log
    "
    send "exit
    "
    expect eof
    #send "uname
    "
    # expect "Linux"
    # send "df -h
    "
     # 这里使用了 interact 命令,使执行完程序后,用户可以在 $host 终端进行交互操作。
    [oracle@OAPRIMARY shell]$
    
    [oracle@OAPRIMARY shell]$ cat name.tmp 
    oracle 10.3.1.51  xxx
    oracle 10.3.1.52 xxx
    oracle 10.2.1.42  xxx
    root   10.3.1.54   xxx
    root   10.3.1.55  xxx
    

  • 相关阅读:
    自动装配有哪些局限性?
    zk 节点宕机如何处理?
    Zookeeper 的 java 客户端都有哪些?
    BeanFactory – BeanFactory 实现举例?
    Spring 配置文件?
    什么是 Spring IOC 容器?
    Spring 框架中的单例 bean 是线程安全的吗?
    ApplicationContext 通常的实现是什么?
    什么是通知Advice?
    spring 支持哪些 ORM 框架?
  • 原文地址:https://www.cnblogs.com/hzcya1995/p/13352310.html
Copyright © 2020-2023  润新知