Linux自动SSH SCP脚本

来源:互联网 发布:php self static 编辑:程序博客网 时间:2024/06/04 13:23
  • 本地创建list文件,格式如下

127.0.0.1 passwd
127.0.0.2 passwd


  • 默认为ssh登录设备执行命令,修改shell变量即可
  • 需要使用scp功能,将spawn ssh注释添加#
  • 去除#spawn scp 前的#,修改源目即可。
#!/bin/shcp /dev/null failed.logcp /dev/null success.logtotal=$(cat list | wc -l)shell='uname -r'curr=0while read ip passworddo    curr=$(($curr + 1))    echo "=====================================[$total,$curr]Begin to process $ip ...."    if [ "$ip" != "" ]; then        ping -c 1 $ip > /dev/null        ret=$?        if [ $ret -eq 1 ]; then            printf "%03d: %s\r\n" $iCount "ping $ip Failed!"            echo $ip >> failed.log        else            expect -c "set timeout 20             set passwderror 0             #spawn scp $ip:/root/1.sh /root/2.sh             spawn ssh -o ConnectTimeout=3 root@$ip \"$shell\"             expect {                 \"*assword:*\" {                     if { \$passwderror == 1 } {                     puts \"passwd is error\"                     exit 2                     }                     set timeout 1000                     set passwderror 1                     send \"$password\r\"                     exp_continue                 }                 \"*es/no)?*\" {                     send \"yes\r\"                     exp_continue                 }                 timeout {                     puts \"connect is timeout\"                     exit 3                 }             }"            if [ $? -ne 0 ]; then                echo $ip >> failed.log            else                echo $ip >> success.log            fi        fi    fidone < list
原创粉丝点击