shell

expect shell脚本

admin · 7月2日 · 2020年

一个自用的expect脚本

#!/bin/bash
log='/Users/liupengfei/Documents/openwrt/luckin/log/update.log'
cat /dev/null > $log
cat list|while read line; do

number=`awk 'BEGIN {split("'"$line"'",arr,"*");print arr[1]}'`
ip=`awk 'BEGIN {split("'"$line"'",arr,"*");print arr[2]}'`
name=`awk 'BEGIN {split("'"$line"'",arr,"*");print arr[3]}'`
passwd='passwd'
port='22'
local_dir="path"
goal_dir="path"

if ping -c 1 $ip >/dev/null;then

/usr/bin/expect <<EOF
#spawn scp  -r -p$port $local_dir root@$ip:$goal_dir    
#update
#spawn scp  -r -p$port  root@$ip:$goal_dir $local_dir 
#download
spawn ssh root@$ip
expect {
        "*yes/no" { send "yes\r"; exp_continue }
        "*password:" { send "$passwd\r" }
        }
expect {
        ":~#" { send " " }
        }
expect {
        ":~#" { send "exit\r" }
        }
set timeout 10
expect eof
EOF
     if [ $? == 0 ]
        echo "$ip\t  $name\t 执行成功 "  >> $log
     else
        echo "$ip\t  $name\t 执行失败 "  >> $log
     fi
done