shell脚本实现ssh-copy-id批量自动发送公钥到远程主机
程序员文章站
2023-10-30 17:37:10
需求
批量实现自动发送公钥到远程主机
环境
firewalld:关闭
selinux:关闭
实现方式
sshpass命令
shell调用expe...
需求
批量实现自动发送公钥到远程主机
环境
firewalld:关闭
selinux:关闭
实现方式
- sshpass命令
- shell调用expect命令
sshpass命令
#!/bin/bash # #******************************************************************** #author: hechunping #qq: ××× #date: 2019-11-07 #filename: ssh-sshpass.sh #url: hexiaoshuai.blog.51cto.com #description: the test script #copyright (c): 2019 all rights reserved #******************************************************************** net=172.20.200 user=(root hechunping) password=123456 ssh-keygen -t rsa -p '' -f ~/.ssh/id_rsa &> /dev/null sed -i '/stricthostkeychecking/c stricthostkeychecking no' /etc/ssh/ssh_config rpm -q sshpass &> /dev/null || yum -y install sshpass &> /dev/null for i in {1..254} ; do { sshpass -p $password ssh-copy-id -i ${user[0]}@${net}.${i} &> /dev/null }& done wait
shell调用expect命令
#!/bin/bash # #******************************************************************** #author: hechunping #qq: ××× #date: 2019-11-07 #filename: ssh-expect.sh #url: hexiaoshuai.blog.51cto.com #description: the test script #copyright (c): 2019 all rights reserved #******************************************************************** net=172.20.200 user=(root hechunping) password=123456 ssh-keygen -t rsa -p '' -f ~/.ssh/id_rsa &> /dev/null for i in {1..254} ; do { expect <<eof spawn ssh-copy-id -i ${user[0]}@${net}.${i} expect { "yes/no" { send "yes\n";exp_continue } "password" { send "${password}\n" } } expect eof eof }& done wait
总结
以上所述是小编给大家介绍的shell脚本实现ssh-copy-id批量自动发送公钥到远程主机,希望对大家有所帮助
上一篇: 古代最鸡肋的几种冷兵器,用起来自己死得快