zoukankan      html  css  js  c++  java
  • Shell之expect的测试

     测试:./sshLogin.sh Slave1 caipeichao 1qaz@WSX hadoop lk198981 HadoopCluster

    #!/usr/bin/expect -f

    #auto ssh login

    if {$argc<6} {
        puts stderr "Usage: $argv0(host_name) $argv1(login_name) $argv2(login_pwd) $argv3(newuser) $argv4(newuserpwd) $argv5(newuserGroup). "
     exit 1
    }

    set timeout 20

    set host_name [lindex $argv 0]
    set login_name [lindex $argv 1]
    set login_pwd [lindex $argv 2]
    set newuser [lindex $argv 3]
    set newuserpwd [lindex $argv 4]
    set newuserGroup [lindex $argv 5]

    set IsExistHCGroup 1
    set IsExistHUser 1


    #spawn ssh -l $USER $IP        #用spawn启动一个ssh客户端
    spawn ssh $login_name@$host_name
    #如果是第一次连接,要保存密钥再输入密码,如果不是第一次连接则输入密码
    expect {
    "yes/no" { send "yes ";exp_continue }
    "password:" { send "$login_pwd " }
    }

    expect "*$"
    #cat /etc/passwd|cut -f 1 -d:|grep hadoop
    #set ip [exec sh -c {ifconfig eth0 | grep "Mask" | cut -d: -f2 | awk '{print $1}'}]
    #puts "$ip "

    set HCInfo [exec sh -c { grep "HadoopCluster" /etc/group | cut -d: -f1 | awk '{print $1}' }]
    set UserInfo [exec sh -c { grep "hadoop" /etc/passwd | cut -d: -f1 | awk '{print $1}' }]

    puts "$HCInfo "
    puts "$UserInfo "

    if { $HCInfo != "" } {
          set IsExistHCGroup 0
    } else {
          set IsExistHCGroup 1
    }

    if { $UserInfo != "" } {
          set IsExistHUser 0   
    } else {
          set IsExistHUser 1
    }

    puts "$IsExistHCGroup "
    puts "$IsExistHUser "


    if { $IsExistHCGroup == 1 } {
         send "sudo groupadd $newuserGroup "
         expect "password*"
         send "$login_pwd "
         expect "*$"
    }

    switch -- $IsExistHUser {
        1 { send "sudo useradd $newuser -m -d /home/$newuser -g $newuserGroup -s /bin/bash "
            expect "*$" }
        0 {
           send "sudo usermod -g $newuserGroup $newuser "
           expect "*password*"
           send "$login_pwd "
           expect "*$" }    
    }


    expect "*$"
    send "sudo passwd $newuser "

    expect {
    "*password:" { send "$newuserpwd ";exp_continue }
    "*password:" { send "$newuserpwd " }
    }

    expect "*$"
    send "su $newuser "
    expect "*assword:"
    send "$newuserpwd "
    expect "*$"
    send "cd ~ "
    expect "*$"
    send "mkdir UntarFile "
    expect "*$"
    send "exit "

    expect eof


    #expect {
    #"already exists" { send "usermod -g HadoopCluster hadoop ";exp_continue }
    #"*$" { send "sudo useradd hadoop -m -d /home/hadoop -g HadoopCluster -s /bin/bash " }

    #}
    #set HCInfo [exec grep HadoopCluster /etc/group]
    #set UserInfo [exec grep hadoop /etc/passwd]
    #if { $HCInfo == "" } {
        
    #    send "sudo groupadd HadoopCluster "
         
    #    if { $UserInfo == 0 } {
    #   }
       
    #}
    #expect "*$"
    #send "sudo groupadd HadoopCluster "
    #expect "*password*"
    #send "$login_pwd "
    #expect "HadoopCluster*"
    #expect "*$"
    #send "cat /etc/passwd|cut -f 1 -d:|grep hadoop "
    #expect "*$"
    #send "sudo useradd hadoop -m -d /home/hadoop -g HadoopCluster -s /bin/bash "

    #expect {
    #   "*$" { send "sudo useradd hadoop -m -d /home/hadoop -g HadoopCluster -s /bin/bash ";exp_continue }
    #   "*password*" { send "$login_pwd " }
    #}

    #expect {
    #   "HadoopCluster*" {
    #      send "cat /etc/passwd|cut -f 1 -d:|grep hadoop "
    #      expect {
    #         "*$" { send "sudo useradd hadoop -m -d /home/hadoop -g HadoopCluster -s /bin/bash ";exp_continue }
     #        "*password*" { send "$login_pwd " }
    #         "hadoop*" { }
    #      }
    #   }
    #   "*$" {
    #      send "sudo groupadd HadoopCluster "
    #      expect "*password*" { send "$login_pwd " }
     #  }
             
              
    #}


    #expect {
    #"*$" { send " if grep HadoopCluster /etc/group;then sudo groupadd HadoopCluster;fi ";exp_continue }
    #"*password*" { send "$login_pwd " }
    #}

    #expect {
    #"*$" { send "sudo groupadd HadoopCluster ";exp_continue }
    #"*password*" { send "$login_pwd " }

    #}

    #expect "*$"
    #send "cat /etc/passwd|cut -f 1 -d:|grep hadoop"
    #expect {
    #"hadoop*" { exit 2 }
    #"*$" { send "sudo useradd hadoop -m -d /home/hadoop -g HadoopCluster -s /bin/bash ";exp_continue }
    #"*password*" { send "$login_pwd " }

    #}


    #此时,输入密码后并未使用expect eof,因为ssh这个spawn并没有结束,除非你exit.
    #Note:不要用spawn mkdir tmpdir,这样会使得上一个spawn即ssh结束,so你的tmpdir将在本机建立
    #send "mkdir tmpdir "
    #interact

  • 相关阅读:
    Java 重写(Override)与重载(Overload)
    Java 继承
    Java 异常处理
    Java Scanner 类
    Java 流(Stream)、文件(File)和IO
    Java 方法
    Java 正则表达式
    Beta冲刺——代码规范、冲刺任务与计划
    Beta冲刺——凡事预则立
    Beta冲刺——问题总结博客(事后诸葛亮和组员交换事宜)
  • 原文地址:https://www.cnblogs.com/likai198981/p/3707409.html
Copyright © 2011-2022 走看看