SSH交互式腳本StrictHostKeyChecking選項 benchmode=yes


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"'


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM