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'