测试:./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).\n "
 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\r";exp_continue }
"password:" { send "$login_pwd\r" }
}

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

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\r"
puts "$UserInfo\r"

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

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

puts "$IsExistHCGroup\r"
puts "$IsExistHUser\r"

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

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

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

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

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

expect eof

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

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

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

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

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

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

#}

#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\r";exp_continue }
#"*password*" { send "$login_pwd\r" }

#}

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

Shell之expect的测试的更多相关文章

  1. shell脚本中文件测试

    shell脚本中文件测试 author:headsen chen  2017-10-17  14:35:19 个人原创,转载请注明作者,否则 依法追究法律责任 [ -f  filename  ]   ...

  2. Shell学习之条件测试(四)

    Shell学习之条件测试 目录 逻辑测试 文件测试 数值比较 字符串比较 逻辑测试 格式: [ 表达式 ] 操作符 [ 表达式2 ] …… 命令1 操作符 命令2 …… 常用的操作符 ( 注意:-a和 ...

  3. Shell结合Expect实现自动输入密码

    Shell结合Expect自动输入密码示例 #!/bin/bash cd /data/live /usr/bin/expect <<-EOF spawn git clone "s ...

  4. shell中expect介绍

    expect介绍 借助Expect处理交互的命令,可以将交互 过程如:ssh登录,ftp登录等写在一个脚本上,使之自动化完成.尤其适用于需 要对多台服务器执行相同操作的环境中,可以大大提高系统管理人员 ...

  5. Linux命令(27):shell 结合expect,多服务器批量分发数据

    shell 结合expect 写的批量scp脚本工具 except安装:http://www.cnblogs.com/lovychen/p/6525623.html expect用于自动化地执行lin ...

  6. (转)shell脚本之文件测试操作符及整数比较符

    shell脚本之文件测试操作符及整数比较符 原文:http://www.cnblogs.com/Steward-Xu/p/6722592.html 一.文件测试操作符: 在书写测试表达式是,可以使用一 ...

  7. Shell脚本的条件测试与比较

    Shell脚本的条件测试与比较 一.shell脚本的条件测试 通常,在bash的各种条件结构和流程控制结构中都要进行各种测试,然后根据测试结构执行不同的操作,有时也会与if等条件语句相结合,来完成测试 ...

  8. shell脚本之六:shell脚本的条件测试与比较

    六.shell脚本的条件测试与比较 (一)条件表达式的常见语法 1.条件表达式6种写法(if,while) 语法1:test<测试表达式> 语法2:[ <测试表达式>] #中括 ...

  9. shell脚本之文件测试表达式

    1.文件测试表达式的用法 我们在编程时处理一个对象时,需要对对象进行测试,只有符合要求的才采取操作处理:这样做的好处是避免程序出错以及无所畏的消耗系统资源,这个测试的对象可以是文件.字符串.数字等. ...

随机推荐

  1. spring cloud--------------------HystrixCommand使用

    一.注解使用: (一)注解同步执行 1.注解开启断路器功能 @EnableCircuitBreaker 2.方法事例 @HystrixCommand(fallbackMethod = "er ...

  2. Django介绍(3)

    https://www.cnblogs.com/yuanchenqi/articles/5786089.html

  3. Stacktraces java.lang.NoSuchMethodException: com.liuyang.action.UserAction.add()

    Struts Problem Report Struts has detected an unhandled exception: Messages: com.liuyang.action.UserA ...

  4. day16(jdbc进阶,jdbc之dbUtils)

    1.jdbc进阶 jdbc事务管理 jdbc中的事务管理其实就是交给了连接对象去管理.先写一个简单的事务管理 public class Demo01 { private static Connecti ...

  5. (最小生成树) Borg Maze -- POJ -- 3026

    链接: http://poj.org/problem?id=3026 http://acm.hust.edu.cn/vjudge/contest/view.action?cid=82831#probl ...

  6. (网络流) Sabotage -- UVA -- 10480

    链接: http://acm.hust.edu.cn/vjudge/contest/view.action?cid=82835#problem/J 代码: #include<cstdio> ...

  7. [翻译] Using Custom Functions in a Report 在报表中使用自己义函数

    Using Custom Functions in a Report  在报表中使用自己义函数   FastReport has a large number of built-in standard ...

  8. Android 体系架构

    什么是Android? 答:Android就是移动设备的软件栈,包括(一个完整的操作系统,中间件,关键应用程序), 底层是Linux内核,包括(安全管理, 内存管理,进程管理 ,电源管理,硬件驱动-) ...

  9. Linux 安装PAE内核

      客户软件是部署在32位的CentOS5服务器当中,CentOS5目前只能识别4G内存,需要安装PAE内核,让系统支持PAE物理地址扩展. 1.安装PAE内核 yum -y install kern ...

  10. [ACM_水题] UVA 12502 Three Families [2人干3人的活后分钱,水]

      Three Families  Three families share a garden. They usually clean the garden together at the end o ...