实现免密登陆脚本, expect登陆远程主机,将生成的密钥写入到目标主机, 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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#!/bin/bash
RED="echo -e \E[1;31m"
GREEN="echo -e \E[1;32m"
END="\E[0m"
 
 
os_release(){
if grep -i -q ubuntu /etc/os-release;then
            echo ubuntu
        elif grep -i -q centos /etc/os-release;then
            echo centos
        else
            echo "os can not be supported!"
        fi
}
 
install_mysql(){
if [ `os_type` = centos ] ;then
        yum install mysql -y
elif [ `os_type` = ubuntu ] ;then
        apt install mysql -y
else
        echo "os can not be supported"
fi
${GREEN}安装成功$END
}
 
 
install_httpd(){
if [ `os_type` = centos ] ;then
        yum install httpd -y
elif [ `os_type` = ubuntu ] ;then
        apt install apache2 -y
else
        echo "os can not be supported"
fi
 
 
${GREEN}安装成功$END
}
 
 
 
ssh_expect(){
    NET=10.0.0
    user=root
    password=qq123456
    IPLIST="
    137
    101
    238
    "
    for ID in $IPLIST ;do
        ip=$NET.$ID
 
    expect <<eof
spawn ssh $user@$ip
expect {
    "yes/no" { send "yes\n";exp_continue }
    "password" {send "$password\n" }
        "#" { send "exit\n" }  
}
eof
done
}
 
 
PS3="请选择功能(1-4): "
select menu in 安装mysql 安装apache 免密钥登录主机 退出;do
       case $REPLY in
       1)
       install_mysql
       ;;
       2)
           install_httpd
              ;;
       3)
           ssh_expect
       ;;
       4) 
             break
        ;;      
       *)
       echo "输出错误"
 esac
done

  

posted @   tgycaghujkl  阅读(14)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通
点击右上角即可分享
微信分享提示