author:headsen chen
date : 2017-11-29 15:46:39
notice:created by headsen chen himself and not allowed to copy,or will done by low.
sshpass全國超級使用第一人
應用范圍:可以在命令行直接使用密碼來進行遠程連接和遠程拉取文件。
使用前提:對於未連接過的主機。而又不輸入yes進行確認,需要進行sshd服務的優化:
# vim /etc/ssh/ssh_config StrictHostKeyChecking no
# vim /etc/ssh/sshd_config GSSAPIAuthentication no UseDNS no
# service sshd restart
sshpass 命令的安裝:
# yum -y install sshpass
實例1:直接遠程連接某台主機:
命令:sshpass -p xxx ssh root@192.168.11.11
sshpass -p xxx ssh root@192.168.11.11 'w' -o StrictHostChecking=no 免輸入yes。
實例:本地執行遠程機器的命令:
命令: sshpass -p xxx ssh root@192.168.11.11 "ethtool eth0"
實例2:遠程連接指定ssh的端口:
命令:sshpass -p 123456 ssh -p 1000 root@192.168.11.11 (當遠程主機不是默認的22端口時候)
實例3:從密碼文件讀取文件內容作為密碼去遠程連接主機
命令:sshpass -f xxx.txt ssh root@192.168.11.11
實例4:從遠程主機上拉取文件到本地
命令: sshpass -p '123456' scp root@host_ip:/home/test/t ./tmp/
實例5:sshpass不可以直接接帶特殊字符意義的密碼,需要轉義或者用小括號,或者指定文件的方式來連接都可以規避這個問題。
附錄:sshpass的安裝
Ubuntu:
apt-get install sshpass
centos:
# 源碼包安裝 wget http://sourceforge.net/projects/sshpass/files/sshpass/1.05/sshpass-1.05.tar.gz tar xvzf sshpass-1.05.tar.gz cd sshpass-1.05.tar.gz ./configure make make install # yum安裝 yum -y install sshpass