1)使用expect命令在shell脚本中实现的简单登陆
#!/bin/bash
#!/usr/bin/expect -f
if [ $# != 3 ];then
echo "Usage: cmd [host] [user] [passwd]"
fi
host=$1
user=$2
password=$3
echo
echo $user@$host
echo
#eof {exit 0}
auto_ssh_copy_id ()
{
expect -c "set timeout -1;
spawn ssh $2;
expect
{
"*yes/no" {send "yes\r"; exp_continue}
"*password:" {send "$1\r"}
};
interact
"
}
#auto_ssh_copy_id $password $user@$host
set ip [lindex $argv 0 ]
set password [lindex $argv 1 ]
set timeout 10
spawn ssh root@$ip
expect {
"*yes/no" { send "yes\r"; exp_continue}
"*password:" { send "$password\r" }
}
interact
exit 0
2)expect脚本简单使用(*.exp 脚本)
#!/usr/bin/expect -f
set ip 192.168.2.101
set password "321"
spawn ssh root@$ip
expect {
"*yes/no" {send "yes\r"; exp_continue}
"*password:" {send "$password\r" }
}
expect "#*"
send "passwd\r"
expect {
"Enter*" { send "123\r"; exp_continue}
"Retype*" { send "123\r"; exp_continue}
}
send "exit\r"
expect eof
exit
3)自动登陆shell代码段
……
auto_ssh_login ()
{
expect -c "
set timeout 5
spawn sshpass -p $2 ssh -o StrictHostKeyChecking=no -p 39000 root@$1
send "passwd\\r"
expect {
Enter* {send -- $3\r; exp_continue}
Retype* {send -- $3\r; exp_continue}
}
send "exit\\r"
expect eof
"
}
……
暂时更新到这里, 继续研究