这个用于SSH和SCP的小包装可以方便地进行管理和编写脚本。
特别推荐在不需要强大安全性的生产环境下开发和测试嵌入式系统和虚拟机。
用法
利用 嘘 和 scpp 完全与您将使用的 ssh 和 scp.
安装方式
易于安装!粘贴到您的zsh终端上
wget //raw.githubusercontent.com/nachoparker/sshh/master/sshh.sh -O - >> ~/.zshrc
,或重击
wget //raw.githubusercontent.com/nachoparker/sshh/master/sshh.sh -O - >> ~/.bashrc
这个需要 sshpass 安装在系统中。在Debian和衍生产品中,
sudo apt-get install sshpass
细节
我们大多数人已经知道,执行无密码SSH的理想方法是创建一个公钥-私钥对并将公钥添加到 〜/ .ssh / 授权密钥.
cat ~/.ssh/id_rsa.pub | ssh pi@192.168.0.145 "mkdir -p ~/.ssh && cat >> 〜/ .ssh / 授权密钥"
另外,我们第一次远程登录时,系统会提示您进行确认,并且连接将在 〜/ .ssh / 已知主机.
$ ssh pi@192.168.0.145 The authenticity of host '192.168.0.145 (192.168.0.145)' can't be established. ECDSA key fingerprint is SHA256:PkLRnkDFSQ8dVUs7RgEvHQnSqiwPWxbIWBaczLzMaCc. Are you sure you want to continue connecting (yes/no)?
这一切都是好的和安全的,但是当我们开发某种嵌入式系统,容器或虚拟机时,这实际上不是很实用。每次刷新主板或重新安装虚拟机时,我们都会遇到此欢迎消息
$ ssh pi@192.168.0.145 @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ @ WARNING: REMOTE HOST IDENTIFICATION HAS CHANGED! @ @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ IT IS POSSIBLE THAT SOMEONE IS DOING SOMETHING NASTY! Someone could be eavesdropping on you right now (man-in-the-middle attack)! It is also possible that a host key has just been changed. The fingerprint for the ECDSA key sent 通过 the remote host is SHA256:PkLRnkDFSQ8dVUs7RgEvHQnSqiwPWxbIWBaczLzMaCc. Please contact your system administrator. Add correct host key in /home/nacho/.ssh/known_hosts to get rid of this message. Offending ECDSA key in /home/nacho/.ssh/known_hosts:41 ECDSA host key for 192.168.0.145 has changed 和 you have requested strict checking. Host key verification failed.
对于这种工作,我们将在相同的默认IP地址中出现不同的系统,这意味着我们每次都必须从 授权密钥 并再次确认 已知主机。这不仅不方便,而且完全阻止了自动化脚本的工作。
下一步要做的逻辑是在以下位置重新配置我们的SSH客户端 〜/ .ssh / config 像这样
主持人* StrictHostKeyChecking否 利用rKnownHostsFile /dev/null
我们赢了’不要再被消息打扰了。只要我们每次都复制公钥,就可以开始编写脚本,直到重新刷新或运行新的VM映像为止。
我们仍然可以做得更好。从安全默认值更改我们的配置不是理想的,我们仍然需要复制密钥并至少输入一次密码。
我最喜欢的解决方案是致电 ssh 从命令行使用正确的选项
ssh -q -o 利用rKnownHostsFile=/dev/null -o StrictHostKeyChecking=no pi@192.168.0.145
至于密码,我们可以使用 sshpass。它会在命令行中接受密码,这对我们来说很好,因为在这种情况下,安全性不是问题。
sshpass -p raspberry ssh pi@192.168.0.145
我们可以将两种解决方案结合起来,但是当然这不是一本好书,更不用说打字了。我们需要一个包装器。
输入 嘘.
嘘 pi@192.168.0.145
嘘 将首先提示您输入密码,就像 ssh 。成功登录后,它将密码保存在环境变量中 SSH_PWD 。后续登录将使用 sshpass 与存储的密码。
如果我们脚本编写或懒惰,我们可以导出 SSH_PWD 作为脚本的一部分或 .zshrc 和 .bashrc.
SSH_PWD=raspberry
现在我们可以总结一些优点
- 无需更改配置。
- 无需生成密钥并将其保存为构建过程的一部分,每次我们要在新计算机上进行测试时,都必须对其进行更改。
- 公钥方法仍然有效,并且优先于 SSH_PWD.
- 注意要允许使用与SSH相同的用例:您可以通过管道传输到它,可以追加命令,使用here-strings和here-documents,等等。
- 脚本的理想默认值, ServerAliveInterval 和 连接超时 可以足够快地检测到连接问题,因此您的例程不会整夜呆在故障板上。
这一切都适用于 scpp 一样。
scpp file.txt pi@192.168.0.145:/tmp/
如果存储的密码失败,系统将提示您输入新密码,但是如果出于任何原因想要再次询问,请重写 SSH_PWD 或取消设置。
unset SSH_PWD
码
#!/bin/bash # SSH 和 SCP wrappers for easy workflow on embedded systems using sshpass # # 用法: just do 嘘 where you would do ssh. Same with scpp 和 scp # # Copyleft 2017 通过 Ignacio Nunez Hernanz <nacho _a_t_ ownyourbits _d_o_t_ com> # GPL licensed (see end of file) * 利用 at your own risk! #SSH_PWD="raspberry" # uncomment to work with this password 通过 default function run_only_interactive() { [ -z "$PS1" ] || $@; } function run_only_script() { [ -z "$PS1" ] && $@; } function 嘘() { local RED=$'\e[1;31m' local YE=$'\e[1;33m' local NC=$'\e[0m' local SSH=( ssh -q -o 利用rKnownHostsFile=/dev/null -o StrictHostKeyChecking=no -o ServerAliveInterval=5 -o 连接超时=1 -o LogLevel=quiet ) if [[ "$SSH_PWD" == "" ]]; then run_only_interactive echo -e "$1 password:" run_only_interactive read -s SSH_PWD [ "$SSH_PWD" != "" ] && run_only_interactive echo -e "saved ${BLD}$SSH_PWD${NC}" fi [[ "$SSH_PWD" != "" ]] && local SSHPASS=( sshpass -p $SSH_PWD ) ${SSHPASS[@]} ${SSH[@]} $@ local RET=$? if [ $RET -eq 5 ]; then echo -e "${RED}wrong SSH password ${YE}$SSH_PWD${NC} for ${YE}$1${NC}" >&2 run_only_interactive unset SSH_PWD run_only_interactive 嘘 $@ return 1 fi if [ $RET -eq 255 ]; then echo -e "${RED}error in SSH connection ${YE}$1${NC}" >&2 return 1 fi return 0 } function scpp() { local RED=$'\e[1;31m' local YE=$'\e[1;33m' local NC=$'\e[0m' local SCP=( scp -q -o 利用rKnownHostsFile=/dev/null -o StrictHostKeyChecking=no -o ServerAliveInterval=5 -o 连接超时=1 -o LogLevel=quiet ) if [[ "$SSH_PWD" == "" ]]; then echo -e "${YE}warning${NC}: parameter SSH_PWD not found. Running without sshpass" $SCP $@ else sshpass -p $SSH_PWD ${SCP[@]} $@ fi if [[ $? -ne 0 ]]; then echo -e "${RED}error in SCP connection${NC}" >&2 return 1 fi } # License # # This script is free 软件; you can redistribute it 和/or modify it # under the terms of the GNU General Public License as published 通过 # the Free Software Foundation; either version 2 of the License, or # (at your option) any later version. # # This script is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this script; if not, write to the # Free Software Foundation, Inc., 59 Temple Place, Suite 330, # Boston, MA 02111-1307 USA
参考文献
//www.digitalocean.com/community/tutorials/ssh-essentials-working-with-ssh-servers-clients-and-keys
非常好。而不是做‘Host *’在您的ssh配置中,您可以执行‘Host some-device’然后在其中放置设备特定的选项。喜欢:
主机被测设备
主机名192.168.1.1
用户pi
StrictHostKeyChecking否
您仍然有此解决的密码问题。