在执行脚本时,用到ssh 和scp的时候,需要手动输入密码登录,这不符合自动化执行,当然也可以使用密钥对自动登录,
这里简单介绍一下使用expect自动登录。
1.安装expect
apt-get -y install expect
2.编写脚本
test.exp
#!/usr/bin/expect -f
set ip 192.168.1.10
set password 881234
set timeout 10
spawn ssh root@$ip
expect {
"*yes/no" { send "yes\r"; exp_continue}
"*password:" { send "$password\r" }
}
expect "#*"
send "pwd\r" //执行的linux 操作
send "ls\r"
send "exit\r"
expect eof
3.执行脚本
./test.exp