CentOS6、CentOS7、Ubuntu 一键部署 ssh 免密登录

CentOS6、CentOS7、Ubuntu 一键部署 ssh 免密登录(ssh.py 文件)
192.168.1.5 为主机器,其他为控制机。
vim /home/shad.py

from fabric.api import run,env,parallel
env.hosts = ['192.168.1.1', '192.168.1.2', '192.168.1.3']
env.user = 'root'
env.parssword = '123456'
#@parallel
def name():
        run('apt install -y wget || yum install -y wget')
        run('wget http://192.168.1.5/ssh.sh')
        run('sh shad.sh')
CentOS6、CentOS7、Ubuntu 一键部署 ssh 免密登录(ssh.py 文件)
vim /var/www/html/ssh.sh
#!/bin/bash
name=`awk 'NR==1{print $1}' /etc/issue`
if [ "$name" = "Ubuntu" ];then
sed -i 's@#AuthorizedKeysFile@AuthorizedKeysFile@g' /etc/ssh/sshd_config
mkdir -p ~/.ssh
cat >>~/.ssh/authorized_keys<<EOF
**公钥**
EOF

sed -i 's@#PasswordAuthentication yes@PasswordAuthentication no@g' /etc/ssh/sshd_config
service sshd restart
elif [ "$name" = "CentOS" ];then
sed -i 's@#PermitRootLogin yes@PermitRootLogin yes@g' /etc/ssh/sshd_config
sed -i 's@#RSAAuthentication@RSAAuthentication@g' /etc/ssh/sshd_config
sed -i 's@#PubkeyAuthentication@PubkeyAuthentication@g' /etc/ssh/sshd_config
sed -i 's@#AuthorizedKeysFile@AuthorizedKeysFile@g' /etc/ssh/sshd_config
mkdir -p ~/.ssh
cat >>~/.ssh/authorized_keys<<EOF
**公钥**
EOF

sed -i 's@PasswordAuthentication yes@PasswordAuthentication no@g' /etc/ssh/sshd_config
service sshd restart
else
sed -i 's@#PermitRootLogin yes@PermitRootLogin yes@g' /etc/ssh/sshd_config
sed -i 's@#PubkeyAuthentication@PubkeyAuthentication@g' /etc/ssh/sshd_config
mkdir -p ~/.ssh
cat >>~/.ssh/authorized_keys<<EOF
公钥
EOF
sed -i 's@PasswordAuthentication yes@PasswordAuthentication no@g' /etc/ssh/sshd_config
service sshd restart
fi

猜你喜欢

转载自blog.51cto.com/12384628/2151115