测试:./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