yum install expect -y
expect 參考:http://blog.csdn.net/snow_114/article/details/53245466
yum install sshpass -y
ssh登錄不能在命令行中指定密碼。sshpass的出現,解決了這一問題。sshpass用於非交互SSH的密碼驗證,通常用在sh腳本中,無須再次輸入密碼。服務器
它容許你用 -p 參數指定明文密碼,而後直接登陸遠程服務器,它支持密碼從命令行、文件、環境變量中讀取。ssh
參考:http://blog.csdn.net/wangjunjun2008/article/details/19993395spa
先利用expect 傳文件過去 能夠實現免yes確認和指定傳參密碼.net
#!/usr/bin/expect -f set timeout 10 set host [lindex $argv 0] set dport [lindex $argv 1] set username [lindex $argv 2] set password [lindex $argv 3] set src_file [lindex $argv 4] set dest_file [lindex $argv 5] spawn scp -P $dport -r $src_file $username@$host:$dest_file expect { "(yes/no)?" { send "yes\n" expect "*assword:" { send "$password\n"} } } expect eof expect { "*assword:" { send "$password\n" } } expect "100%" expect eof
執行的時候要傳參 傳參格式:
[root@sxzros ~]# ./222.sh 172.20.1.6 229 root passwd /root/222 /root/
而後利用sshpass指定密碼免交互對遠端機器進行文件解壓或者進行文件操做
#sshpass -p passwd ssh -t -p 229 root@172.20.1.6 'tar xf /root/ddd1.tar.lzma -C /tmp' sshpass -p passwd ssh -t -p 229 root@172.20.1.6 'cd ~;touch 222.txt'