且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

ssh自动输入密码登陆

更新时间:2022-10-04 18:12:29

1  vi rm.sh

#!/bin/expect -f
set DATE [exec date +%Y%m%d%k]
set ipaddress [lindex $argv 0]
set port  [lindex $argv 1]
set logname [lindex $argv 2]
#set password xukeqiang
set date [exec date -d "-2 day" "+%Y%m%d"]
set fd [open /home/admin/passwd.sh r]
set password [read $fd]
close $fd
spawn ssh $ipaddress -p $port -l monitor
#set timeout 300
expect "monitor@${ipaddress}'s password:"
#set timeout 300
send "$password\r"
expect -re "\](\$|#)"
#send "rm -rf /tmp/${logname}${date}.tar\r"
send "sh /tmp/rm.sh\r"
expect -re "\](\$|#)"
send "exit\r"
expect eof

 2  vi ip.sh##此文件不能有多余的空行

192.168.1.101 22 nagiosa_

192.168.1.102 22 nagiosb_

192.168.1.103 22 nagiosc_

 

3  vi piliang.sh

#/bin/bash
time=`date +%Y%m%d_%H%M%S`
filename=/home/admin/ip.sh
while read line
do
        echo $line
        /bin/expect -f /home/admin/rm.sh $line
done<$filename |tee /home/admin/log.log$time
echo "run over" >>/home/admin/log.log$time 

 

4  将C:\cygwin64\bin追加到windows系统环境变量Path中,然后编辑批处理文件piliang.bat如下:

sh c:\cygwin64\home\admin\piliang.sh 
\cygwin64\bin\echo  ok

pause












本文转自shangshanyang51CTO博客,原文链接: http://blog.51cto.com/qqran/1905305,如需转载请自行联系原作者