方式一:
- # yum -y install expect
#!/usr/bin/expect ## 不是/bin/bash
set timeout 30
spawn ssh node -p 22
expect {
"(yes/no)?" { send "yes\r" }
}
expect {
"password:" { send "passwd\r" }
}
interact
[test@~]$ cat t.sh
#!/usr/bin/expect
set timeout 30
set password "ydfdadfa"
spawn su - hadoop
expect "Password:"
send "$password\r"
interact
方式二:
- 设置终端快捷方式
- 例如 su - 切换root用户
- 将root密码设置为快捷键免去手动输入
- 例如在 iTerm终端下