auto-ssh

使用expect工具实现自动注入账号密码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#!/bin/bash

if [ $# -ne 2 ]; then
echo "usage: sh copykey.sh hostip password "
exit 1
fi

# 判断id_rsa秘钥文件是否存在
if [ ! -f "/root/.ssh/id_rsa" ];then
ssh-keygen -t rsa -P "" -f /root/.ssh/id_rsa
else
echo "id_rsa has created ......"
fi

# 密码登录
user="root"
ip=$1
passwd=$2
expect <<EOF
set timeout 5
spawn ssh-copy-id $user@$ip
expect {
"yes/no" { send "yes\n";exp_continue }
"password" { send "$passwd\n" }
"installed" { } expect eof
}
EOF

auto-ssh
https://gjorn4389.github.io/2023/12/22/scripts/auto-ssh/
作者
Gjorn
发布于
2023年12月22日
许可协议