expect 真是一个好东西,可以解决免密登录服务器和免密下载的问题。

记录一下。

免密登录:from here

mac 安装brew
/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install 

mac安装expect(需要先安装brew,没有安装的话看上边)
brew install expect 

mac一键登录服务器脚本
set user "username"
set host "123.126.88.**"
set password "yourpassword"
set timeout -1

spawn ssh $user@$host
expect "*assword:*"
send "$password\r"
interact
# expect eof

另一个例子是这里

[root@localhost ~]# vim auto_login.sh

#!/usr/bin/expect
spawn ssh root@192.168.43.143
expect {
“(yes/no)”
{send “yes\r”; exp_continue}
“*password”
{send “123123\r”}
}
interact

 

免密拷贝:

例1

cat /opt/shell/expect_scp.sh
#!/bin/bash
ip="10.168.1.202"
user="root"
expect << EOF
set timeout -1
spawn bash -c “scp -pr /home/* $user@$ip:/home/”
expect {
“yes/no” {send “yes\r”;exp_continue}
“password” {send_user "come here"; send “123456\r”;}
}
expect eof
exit
EOF

 

posted on 2023-03-22 15:46  Certainly  阅读(77)  评论(0编辑  收藏  举报