使用导入的函数执行脚本失败

时间:2018-01-10 13:06:46

标签: bash

我的/usr/local/bin目录中有四个文件:

handle_attack.sh  notify_about_attack.sh  stae_baned_ip.exp  stae.conf

notify_about_attack.sh将执行:

enter image description here

您在ban_ip()方法中看到快照,红色箭头是问题所在,它没有成功执行,甚至没有执行echo "加入stae.conf函数之前" >> /tmp/ban.log

代码如下:

    ...
    echo "FastNetMon Guard: IP $1 blocked because $2 attack with power $3 pps" >> /tmp/ban.log
    source ./handle_attack.sh
    echo "加载handle_attack.sh成功" >> /tmp/ban.log
    ban_ip $1 $2 $3
    # You can add ban code here!
    exit 0

handle_attack.sh代码:

#!/bin/bash

function ban_ip(){

        echo "加入stae.conf函数之前" >> /tmp/ban.log
        source ./stae.conf
        echo "加入了stae.conf到函数" >> /tmp/ban.log
        expect /usr/local/bin/stae_baned_ip.exp $USER $HOST $PASSWORD $TIMEOUT $baned_ip

        cur_date=$(date "+%G-%m-%d %H:%M:%S")
        echo "当前时间:$cur_date" >> /tmp/ban.log
        if [ $? == 0 ]; then
                # 追加到文件
                echo "[禁止成功] 攻击方向:$2 pps:$3  被攻击者:$1  时间:$cur_date" >> stae_record.log

        else
                # 追加到文件
                echo "[禁止失败] 攻击方向:$2 pps:$3  被攻击者:$1  时间:$cur_date" >> stae_record.log
        fi
        exit 0
}

stae_baned_ip.exp代码:

#!/usr/bin/expect

set user     [lindex $argv 0]
set host     [lindex $argv 1]
set password [lindex $argv 2]
set timeout  [lindex $argv 3]
set baned_ip [lindex $argv 4]

echo "${user}  ${host}  ${password}  ${timeout}  ${baned_ip}" >> /tmp/ban.log

spawn ssh $host -l $user

expect {
        "(yes/no)?" {
                send "yes\n"
                expect "password:"
                send "$password\n"
        }
        "password:" {
                send "$password\n"
        }
}

expect "#"
# 下面检测是否登录到host
#send "uname\n"
#expect "Linux"

send "IP route-stae ${baned_ip} 255.255.255.255 null tag666\n"

exit 0

stae.conf

USER="root"
HOST="201.48.116.74"
PASSWORD="asdasd!"
TIMEOUT=60

2 个答案:

答案 0 :(得分:0)

感谢您使用FastNetMon!你能分享FastNetMon的日志文件/var/log/fastnetmon.log的内容吗?通常,FastNetMon会使用通知脚本写入有关任何问题的日志消息。

应该读取脚本中的stdin内容的常见问题之一。你可以阅读它并以这种方式丢弃: 猫> / dev / null

答案 1 :(得分:0)

我的导入方式出错:

source ./handle_attack.sh

我使用它.

. /usr/local/bin/handle_attack.sh

我还删除了exit 0方法中的多余ban_ip