1.安装expect
[root@master ~]# yum -y install expect
2.写myssh.sh脚本
[root@master ~]# vim myssh.sh
#!/bin/bash
# 用户名为所有主机用户名,用户密码同理,$1为位置变量值(ip)
ip=$1
expect -c "
set timeout 1;
spawn ssh 用户名@${ip};
expect {
*yes/no* { send \"yes\r\"; exp_continue }
*password:* { send \"用户密码\r\" }
}
interact
"
3.给myssh.sh执行权限
[root@master ~]# chmod 777 myssh.sh
4.在/etc/profile中添加别名并生效
[root@master ~]# echo 'alias myssh="/root/myssh.sh"' >> /etc/profile
[root@master ~]# source /etc/profile
5.测试是否生效
[root@master ~]# myssh 主机IP
spawn ssh root@主机ip
root@主机ip's password:
Last login: Fri Nov 15 01:00:56 2019 from xxxxxxx
Welcome to XXXX Cloud Service
[root@test ~]# exit
logout
Connection to xxxxxxxxxx closed.
[root@master ~]#
参考链接:添加链接描述