SSH 公钥检查是一个重要的安全机制,可以防范中间人劫持等黑客攻击。但是在特定情况下,严格的 SSH 公钥检查会破坏一些依赖 SSH 协议的自动化任务,就需要一种手段能够绕过 SSH 的公钥检查。

什么是SSH公钥检查
SSH 连接远程主机时,会检查主机的公钥。如果是第一次该主机,会显示该主机的公钥摘要,提示用户是否信任该主机:

The authenticity of host '10.0.0.1 (10.0.0.1)' can't be established.
ECDSA key fingerprint is 91:63:21:08:4a:96:23:5b:f6:98:c9:a8:cd:cb:8b:91.
Are you sure you want to continue connecting (yes/no)?

当选择接受,就会将该主机的公钥追加到文件 ~/.ssh/known_hosts 中。当再次连接该主机时,就不会再提示该问题了。

如何去掉公钥确认?
在首次连接服务器时,会弹出公钥确认的提示。这会导致某些自动化任务由于初次连接服务器而任务中断。或者由于~/.ssh/known_hosts 文件内容清空,导致自动化任务中断。 SSH 客户端的 StrictHostKeyChecking 配置指令,可以实现当第一次连接服务器时,自动接受新的公钥。只需要修改 /etc/ssh/ssh_config 文件,包含下列语句:

Host *
StrictHostKeyChecking no

或者在 ssh 命令行中用 -o 参数

$ ssh -o StrictHostKeyChecking=no 10.0.0.1
---------------------

1: 当通过ssh连接远程服务器的时候,可能会出现以下繁琐场景,需要手工输入yes:

ssh username@ip

这对于某些分布式集群来说是不行的,甚至导致集群都不能启动成功,对于像pssh,pscp这样的自动化工具来说,也不希望这一步的验证,如何在连接的时候不提示这个信息呢:

1
方法1、ssh -o "StrictHostKeyChecking no" username@hostname<br>方法2:修改/etc/ssh/ssh_config,在文件最后添加 StrictHostKeyChecking no,接着重启ssh服务

2:远程执行服务器上面的命令可以通过sshpass(当集群机器之间没有免密的时候),如果没有安装,,执行  yum install sshpass -y

用法:

1
2
3
4
5
6
7
8
9
10
11
12
13
[root@test ~]# sshpass -h
Usage: sshpass [-f|-d|-p|-e] [-hV] command parameters
   -f filename   Take password to use from file        -----指定一个文件,从文件中读取密码
   -d number     Use number as file descriptor for getting password
   -p password   Provide password as argument (security unwise)   ----命令行直接输入密码,不安全
   -e            Password is passed as env-var "SSHPASS"         -----通过设置环境变量SSHPASS来保存密码
   With no parameters - password will be taken from stdin
 
   -P prompt     Which string should sshpass search for to detect a password prompt
   -v            Be verbose about what you're doing
   -h            Show help (this screen)
   -V            Print version information
At most one of -f, -d, -p or -e should be used

Eg:

(1)使用 -f

1
2
3
4
[root@test ~]# sshpass -f passwd.txt ssh root@192.168.0.235 "free -m"
              total        used        free      shared  buff/cache   available
Mem:          96405       27169       12563        4066       56672       63775
Swap:           126          19         107

(2)使用 -p

1
2
3
4
[root@test ~]# sshpass -p "mypasswd" ssh root@192.168.0.235 "free -m"  
              total        used        free      shared  buff/cache   available
Mem:          96405       27168       12584        4066       56652       63777
Swap:           126          19         107  

注:生产环境不要使用这种方式,不安全

(3)使用 -e

1
2
3
4
5
6
7
[root@test ~]# export SSHPASS="mypasswd"
[root@test ~]# echo $SSHPASS
mypasswd
[root@test ~]# sshpass -e ssh hduser@192.168.0.235 "free -m"
              total        used        free      shared  buff/cache   available
Mem:          96405       27209       12561        4066       56634       63735
Swap:           126          19         107  

当然,这种方式只针对当前shell有用,如果要配置永久生效,请修改/etc/profile文件

(4)sshpass、ssh都支持多命令调用,只要在命令之间使用&&号就行。

1
2
3
4
5
6
7
8
[root@test ~]# sshpass -e ssh -l root -o 'StrictHostKeyChecking no' 192.168.4.50  ls /home && free -m
hadoop
mysql
yjt
zbc
              total        used        free      shared  buff/cache   available
Mem:            977         364          95          49         518         366
Swap:          4095          35        4060

  

3、如果想要远程机器调用本地脚本,那么可以如下实现

(1)ssh方式

1
2
[root@test ~]# ssh -l root -o 'StrictHostKeyChecking no' 192.168.4.50 bash -s < test46.sh
runstone.com

(2)sshpass方式

1
2
[root@test ~]# sshpass -e ssh -l root -o 'StrictHostKeyChecking no' 192.168.4.50 bash -s < test46.sh
runstone.com

4、支持sudo

有些命令需要权限才行,当不想重复输入密码的时候,可以通过这种方式。

(1)格式:cmd ---> 'echo password | sudo -S cmd'

eg:

1
[root@test ~]# sshpass -p 123456 ssh  -o 'StrictHostKeyChecking no' yjt@192.168.4.50  'echo 123456 | sudo -S  mkdir /backup'

  注:-S的意思是从标准输入读取密码

对于echo,dd等命令,可能会出现权限不够问题,如:

1
2
[root@test ~]# sshpass -p 123456 ssh  -o 'StrictHostKeyChecking no' yjt@192.168.4.50  'echo 123456 | sudo -S  echo hello > /backup/file'
bash/backup/file: Permission denied

对于上述方式,解决办法如下:

(2)格式:cmd ---> 'echo password | sudo -S  sh/bash -c "cmd"'

eg:

1
root@test ~]# sshpass -p 123456 ssh  -o 'StrictHostKeyChecking no' yjt@192.168.4.50  'echo 123456 | sudo -S bash -c "echo hello > /backup/file"'

SSH交互式脚本StrictHostKeyChecking选项 benchmode=yes的更多相关文章

  1. [转帖]自动交互式脚本--expect

    自动交互式脚本--expect https://www.cnblogs.com/zhuiluoyu/p/4873869.html 我们经常会遇到一些需要与服务器程序打交道的场景,比如,从登陆某个服务器 ...

  2. 20181225-Linux Shell Bash环境下自动化创建ssh互信脚本

    20181225-Linux Shell Bash环境下自动化创建ssh互信脚本 1. 我的Blog 博客园 https://www.cnblogs.com/piggybaba/ 个人网站 http: ...

  3. ansible api 调用出现ssh交互式输入

    发现在删掉 ~/.ssh/know_hosts 之后运行 ansible api 会出现以下提示 The authenticity of host '10.1.*.* (10.1.*.*)' can' ...

  4. TCL自动化之SSH交互式

    目前ssh工具很多,但是能够轻松运用到自动化脚本中,可以轻松适配任何环境,满足ssh交互式登录的tcl工具包很少 下面是个人在tcl自动化过程中比较满意的一款自动化脚本 通过使用管道方式分装plink ...

  5. shell脚本分为三类:登录脚本、交互式脚本、非交互式脚本

    shell脚本分为三类:登录脚本.交互式脚本.非交互式脚本 一. 登录脚本类似于windows下的计算机设置中的登录脚本和账户设置下的登录脚本的合集(我是这么理解的哈). 其配置文件的关键词为pref ...

  6. 自动交互式脚本--expect

    我们经常会遇到一些需要与服务器程序打交道的场景,比如,从登陆某个服务器,然后进行某项工作.这很平常,但是如果把这个工作自动化进行,你就需要一个程序能自动做你要告诉机器的事情,这样,我们的expect就 ...

  7. ssh 互通脚本

    实现了 主机到指定机器的ssh免密码登录. 若要实现互通, 则在机器列表的每台机器上执行该脚本. 192.168.1.22 root test 192.168.1.25 root test 192.1 ...

  8. expect实现交互式脚本

    #!/usr/bin/expect -f ##告诉解释器用expect来解释 set timeout 6 ##设置超时时间 ] ## 这个是传递给脚本的第一个参数,并把参数赋值给user ] ## 这 ...

  9. ssh爆破脚本

    前些天,基友发我一个ssh爆破工具,看起来很吊的样子.然后我就无聊自己写了个py脚本的. 单线程:慢成狗----- #coding:utf-8 #author:jwong import threadi ...

随机推荐

  1. MySQL学习笔记-MHA安装配置

    一.配置主从同步   1. 本例中主从ip及端口 Master:10.1.5.8:3306 Slave1:10.1.5.9:3306 (候选master) Slave2:10.1.5.195:3306 ...

  2. Python3+Appium学习笔记05-报错及解决方法

    记录一下使用期间各种报错和解决方法,毕竟搜了半天才找到解决方法.另外提示一下.优先看官方文档. 报错前面都是一样,就是说在处理的时候,服务器发生了一个未知的错误.然后才是具体报错信息 1)seleni ...

  3. Java&Selenium数据驱动【DataProvider+TestNG+Excel】

    Java&Selenium数据驱动[DataProvider+TestNG+Excel] package testNGWithDataDriven; import java.io.File; ...

  4. MySQL进阶 9: 联合查询 - 查询语句1 union 查询语句2 union ...

    #进阶 : 联合查询 /* union 联合 合并: 将多条查询语句的结果合并成一个结果 语法: 查询语句1 union 查询语句2 union ... 应用语境: 要查询的结果来自多个表,但查询的列 ...

  5. vimrc config and NERDTree

    nmap <C-N> :tabnext<CR> 下载和配置 NERDTree插件的官方地址如下,可以从这里获取最新的版本 https://github.com/scrooloo ...

  6. iar8.32版本关于cmsis的说明

    平台是cubemx5.3 keil5.26 带freertos,使用iar8.32,在上图中的use cmsis 打勾与否都能编译通过.

  7. jquery 下载

    jquery下载所有版本(实时更新) 摘自:http://www.jq22.com/jquery-info122 插件描述:jquery下载,实时更新jquery1.2到最新3.2.1所有版本下载 j ...

  8. P2587 [ZJOI2008]泡泡堂 神仙贪心

    思路:贪心 提交:1次(看了题解$QwQ$) 题解: 若我方最弱可以干掉对方最弱,则干: 否则若我方最强可以干掉对方最强,则干: 否则若我方最弱与对方最强平手,则平: 其实貌似一二条是可以互换的,主要 ...

  9. 002_Python基础学习网站

    (一)电脑端:Python 基础教程 (二)手机端:Python 基础教程

  10. ES6-21.class基本语法

    1.简介(详情参考) class是构造函数的语法糖. class的constructor方法内的实现,就是原来构造函数的实现. class内的所有方法都是在prototype上的,就是原来构造函数的p ...