【问题标题】:Creating an SSH Key Pair for User Authentication with Expect in Bash在 Bash 中使用 Expect 创建用于用户身份验证的 SSH 密钥对
【发布时间】:2019-12-17 23:06:03
【问题描述】:

我已尝试以下步骤设置 ssh 无密码(SSH 密钥对身份验证)登录。

在 bash 中设置 ip 和端口。

ip="xxxx"
port="xxxx"

在客户端设置 ssh 配置文件

cat > $HOME/.ssh/config <<EOF 
Host $ip
IdentityFile $HOME/.ssh/id_rsa
User root
EOF

在客户端创建一个 ssh 密钥对

ssh-keygen -t rsa -f $HOME/.ssh/id_rsa -q -b 2048 -N ""

将 id_rsa 从客户端推送到 ssh 服务器。
准备ssh服务器

ssh -p $port  root@$ip  "mkdir -p  /root/.ssh"

将授权文件推送到ssh服务器

scp -P $port id_rsa.pub root@$ip:/root/.ssh/authorized_keys

为授权文件设置权限

ssh -p $port root@$ip "chmod 700 .ssh; chmod 640 .ssh/authorized_keys"

成功了!

现在我想将所有步骤写入作业的一键式 bash 脚本。
这是我的尝试。

#! /bin/bash
ip="xxxx"
port="xxxx"
pass="yyyy"

cat > $HOME/.ssh/config <<EOF 
Host $ip
IdentityFile $HOME/.ssh/id_rsa.bwg_root
User root
EOF

ssh-keygen -t rsa -f $HOME/.ssh/id_rsa.bwg_root -q -b 2048 -N ""
cd  $HOME/.ssh

    /usr/bin/expect <<EOF
    spawn ssh -p $port  root@$ip  "mkdir -p  /root/.ssh"
    expect "password:"
    send "$pass\r"
    spawn scp -P $port id_rsa.pub root@$ip:/root/.ssh/authorized_keys
    expect "password:"
    send "$pass\r"
    spawn ssh -p $port root@$ip "chmod 700 .ssh; chmod 640 .ssh/authorized_keys"
    expect "password:"
    send "$pass\r"
EOF

得到以下输出信息:

spawn ssh -p xxxx root@yyyy mkdir -p  /root/.ssh
root@yyyy's password: spawn scp -P xxxx id_rsa.bwg.pub root@yyyy:/root/.ssh/authorized_keys
root@yyyy's password: spawn ssh -p xxxx root@yyyy chmod 700 .ssh; chmod 640 .ssh/authorized_keys

为什么以及如何解决它?

【问题讨论】:

    标签: bash ssh expect


    【解决方案1】:

    我会用sshpass 来简化它。

    #!/bin/bash
    ip="x.x.x.x"
    port="xx"
    export SSHPASS="yyy"
    
    cat >$HOME/.ssh/config <<EOF
    Host $ip
    IdentityFile $HOME/.ssh/id_rsa.bwg_root
    User root
    EOF
    
    ssh-keygen -t rsa -f "$HOME/.ssh/id_rsa.bwg_root" -q -b 2048 -N ""
    cd "$HOME/.ssh" || exit 1
    
    sshpass -e ssh -oStrictHostKeyChecking=no -p "$port" "root@$ip" "mkdir -p -m 700 /root/.ssh"
    sshpass -e scp -oStrictHostKeyChecking=no -P "$port" id_rsa.bwg_root.pub "root@$ip:/root/.ssh/authorized_keys"
    sshpass -e ssh -oStrictHostKeyChecking=no -p "$port" "root@$ip" "chmod 640 .ssh/authorized_keys"
    

    顺便说一句:我将最后一个 id_rsa.pub 替换为 id_rsa.bwg_root.pub 并将 -m 700 添加到 mkdir 并删除了 chmod 700 .ssh

    【讨论】:

      【解决方案2】:

      使用ssh-copy-id 将新密钥推送到远程主机。当然,您需要输入 登录密码,但这是您最后一次必须使用它。

      #!/bin/bash
      ip="x.x.x.x"
      port="xx"
      id_file=$HOME/.ssh/id_rsa_$ip
      
      cat > $HOME/.ssh/config <<EOF
      HOST $ip
      IdentityFile $id_file
      User root
      EOF
      
      ssh-keygen -t rsa -f "$HOME/.ssh/id_rsa_$ip" -q -b 2048 -N ""
      
      ssh-copy-id -i "$id_file" -p "$port" root@"$ip"
      

      作为一般规则,总是在尝试except之前使用现有工具寻找非(或更少)交互式解决方案。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-08-26
        • 1970-01-01
        • 1970-01-01
        • 2016-12-11
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多