使用脚本,定时检测线路是否可达,并管理策略路由

因为公司有两条日常使用线路,电信和香港,香港有时因为运营商问题导致丢包率高,或者不可达时。需要手动切换防火墙上策略路由进行停用线路,使业务可以正常使用。为了方便,使用python进行线路可达判断和操作策略路由。
import paramiko #paramiko远程控制模块
import time
import os

def Firewall():
    status1 = "!!!!!!!!!!!!!!!!!!!HK line alive!!!!!!!!!!!!!!!!!!!!"
    status2 = "!!!!!!!!!!!!!!!!!!!HK line unreachable!!!!!!!!!!!!!!!!!"

    fping = os.popen("/usr/bin/ping -c 10 IP | grep loss | cut -d  ' ' -f 6")
    line = fping.read()
    pingloss = line.split('%')
    pingloss = int(pingloss[0])
    standardPing = 25

    sshFirewall = paramiko.SSHClient()
    sshFirewall.set_missing_host_key_policy(paramiko.AutoAddPolicy())
    sshFirewall.connect(hostname='防火墙IP', port=端口, username='账号', password='密码',allow_agent=False,look_for_keys=False)
    sshConnect = sshFirewall.invoke_shell()

    if pingloss < standardPing:
        print status1
        sshConnect.send('dis policy-based-route rule name hk \n')
        sshConnect.send('dis policy-based-route rule name hk_gfw \n')
        time.sleep(1)
        result = sshConnect.recv(5000).decode()
        print result
        if 'disable'in result:
            sshConnect.send('sys \n')
            sshConnect.send('policy-based-route \n')
            sshConnect.send('rule name hk \n')
            sshConnect.send('enable \n')
            sshConnect.send('rule name hk_gfw \n')
            sshConnect.send('enable \n')
            print '\n ----HK line alive,enable hk and hk_gfw policy routing---- \n'

            time.sleep(1)
            sshFirewall.close()
        else:
            print '\n ----HK line alive,hk and hk_gfw policy routing has been enabled,no operation is required---- \n'
            sshFirewall.close()
            exit()
    else:
        print status2
        sshConnect.send('sys \n')
        sshConnect.send('policy-based-route \n')
        sshConnect.send('rule name hk \n')
        sshConnect.send('disable \n')
        sshConnect.send('Y \n')
        sshConnect.send('rule name hk_gfw \n')
        sshConnect.send('disable \n')
        sshConnect.send('Y \n')
        time.sleep(1)
        result = sshConnect.recv(5000).decode()
        print result
        print '\n ----HK line unreachable,hk and hk_gfw policy routing disable successful---- \n'
        sshFirewall.close()

if __name__ == '__main__':
    Firewall()

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值