简体   繁体   English

Bash脚本来推送ssh_keys

[英]Bash script to push ssh_keys

I'm trying to read a file with the names of approx 500 server names on their own individual lines, and then for each of those, ssh in and append the roots authorized_keys file for each. 我试图读取一个单独的行上带有大约500个服务器名称的文件,然后对其中的每个ssh输入ssh并为每个文件附加roots authorized_keys文件。 I keep getting errors each time I run the script and/or modify it. 每次运行脚本和/或修改脚本时,都会不断出现错误。 Can you please help me figure out what's wrong? 您能帮我找出问题所在吗? My OS is Mac OS X: 我的操作系统是Mac OS X:

#!/usr/bin/expect
set timeout 60
set SERVERS "cat /Users/macuser/server.lst"
set USER "myuser"
set MY_PASS "mypasswordhere"

for EACH in $SERVERS; do
cat /Users/macuser/.ssh/id_rsa.pub | ssh $USER@$EACH "tee -a .ssh/authorized_keys"
expect {
    eof                          {break}
    "The authenticity of host"   {send "yes\r"}
    "password:"                  {send "$MY_PASS\r"}
    }

interact
done

here is the error: 这是错误:

wrong # args: should be "for start test next command"
while executing
"for EACH in $SERVERS"
(file "./keyssh_push.sh" line 7)

From Use expect in bash script to provide password to SSH command , sshpass looks like the easiest way to do this. 使用bash脚本中的 sshpass 为SSH命令提供密码中sshpass看起来是最简单的方法。 I would do: 我会做:

#!/bin/sh
servers=`cat /Users/macuser/server.lst`
user="myuser"
my_pass="mypasswordhere"

for server in $servers
do
  </Users/macuser/.ssh/id_rsa.pub sshpass -p"$my_pass" \
    ssh -o StrictHostKeyChecking=no $user@$server cat '>>.ssh/authorized_keys'
done

Update 更新

With @alvits's suggestion: 随着@alvits的建议:

#!/bin/sh
servers=`cat /Users/macuser/server.lst`
user="myuser"
my_pass="mypasswordhere"

for server in $servers
do
  sshpass -p"$my_pass" ssh-copy-id -o StrictHostKeyChecking=no \
    -i /Users/macuser/.ssh/id_rsa $user@$server
done

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM