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).\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的测试的更多相关文章
- shell脚本中文件测试
shell脚本中文件测试 author:headsen chen 2017-10-17 14:35:19 个人原创,转载请注明作者,否则 依法追究法律责任 [ -f filename ] ...
- Shell学习之条件测试(四)
Shell学习之条件测试 目录 逻辑测试 文件测试 数值比较 字符串比较 逻辑测试 格式: [ 表达式 ] 操作符 [ 表达式2 ] …… 命令1 操作符 命令2 …… 常用的操作符 ( 注意:-a和 ...
- Shell结合Expect实现自动输入密码
Shell结合Expect自动输入密码示例 #!/bin/bash cd /data/live /usr/bin/expect <<-EOF spawn git clone "s ...
- shell中expect介绍
expect介绍 借助Expect处理交互的命令,可以将交互 过程如:ssh登录,ftp登录等写在一个脚本上,使之自动化完成.尤其适用于需 要对多台服务器执行相同操作的环境中,可以大大提高系统管理人员 ...
- Linux命令(27):shell 结合expect,多服务器批量分发数据
shell 结合expect 写的批量scp脚本工具 except安装:http://www.cnblogs.com/lovychen/p/6525623.html expect用于自动化地执行lin ...
- (转)shell脚本之文件测试操作符及整数比较符
shell脚本之文件测试操作符及整数比较符 原文:http://www.cnblogs.com/Steward-Xu/p/6722592.html 一.文件测试操作符: 在书写测试表达式是,可以使用一 ...
- Shell脚本的条件测试与比较
Shell脚本的条件测试与比较 一.shell脚本的条件测试 通常,在bash的各种条件结构和流程控制结构中都要进行各种测试,然后根据测试结构执行不同的操作,有时也会与if等条件语句相结合,来完成测试 ...
- shell脚本之六:shell脚本的条件测试与比较
六.shell脚本的条件测试与比较 (一)条件表达式的常见语法 1.条件表达式6种写法(if,while) 语法1:test<测试表达式> 语法2:[ <测试表达式>] #中括 ...
- shell脚本之文件测试表达式
1.文件测试表达式的用法 我们在编程时处理一个对象时,需要对对象进行测试,只有符合要求的才采取操作处理:这样做的好处是避免程序出错以及无所畏的消耗系统资源,这个测试的对象可以是文件.字符串.数字等. ...
随机推荐
- hdu-1698(线段树,区间修改)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1698 注意:用位运算会更快,不然超时. #include<iostream> #inclu ...
- 42 Can stress be Avoided ? 压力能够回避吗 ?
Can stress be Avoided ? 压力能够回避吗 ? ①In the 1960s, medical researchers Thomas Holmes and Richard Rahe ...
- PCL(point cloud library) 学习——简介
Point Cloud Library (PCL) 是开源点云处理库, 包括 filtering, feature estimation, surface reconstruction, regist ...
- Memcached 应用场景
1. 把java对象序列化成base64 存入缓存,不同平台可以反序列化. 2. 通过key去重复 3. 保存一些信息,供所有平台使用
- MFC框架仿真<二>
- Hdu2952 Counting Sheep 2017-01-18 14:56 44人阅读 评论(0) 收藏
Counting Sheep Time Limit : 2000/1000ms (Java/Other) Memory Limit : 32768/32768K (Java/Other) Tota ...
- java web代码规范:
每个类前要有注释,类前的注释格式是: /** *类是干什么的 *@author 编写该类的作者 */ 类中的每个方法前也要有注释: /** *该方法是干什么的 *@param 该方法中传入的参数 * ...
- java web前端调试手段
1.console.log() 2. jQuery.ajax({ url:"/task1/com/guodiantong/servlet/JsonTo ...
- (二)从分布式一致性谈到CAP理论、BASE理论
问题的提出 在计算机科学领域,分布式一致性是一个相当重要且被广泛探索与论证问题,首先来看三种业务场景. 1.火车站售票 假如说我们的终端用户是一位经常坐火车的旅行家,通常他是去车站的售票处购买车票,然 ...
- Python学习-2.安装IDE
Python安装包中已经包含了一个IDE了,叫IDLE,可以在Python的安装目录内找到路径为 ./Lib/idlelib/idle.bat 或者可以在开始菜单中找到. 但是这个IDE功能很弱,缺少 ...