背景:
服務中調用了外部的接口,需要模擬網絡不通的情況,驗證程序因為網絡不通不能成功調用外部接口的場景。
方法:
修改iptables文件,見注釋描述
# Generated by iptables-save v1.4.21 on Mon Mar 9 12:05:45 2020 *filter :INPUT ACCEPT [0:0] :FORWARD ACCEPT [0:0] :OUTPUT ACCEPT [723:227286] -A INPUT -m state --state RELATED,ESTABLISHED -j ACCEPT -A INPUT -p icmp -j ACCEPT -A INPUT -i lo -j ACCEPT -A INPUT -p tcp -m state --state NEW -m tcp --dport 22 -j ACCEPT -A INPUT -j REJECT --reject-with icmp-host-prohibited -A FORWARD -j REJECT --reject-with icmp-host-prohibited #限制訪問某個外網URL,取消限制后需要重啟iptables服務service iptables restart #限制網址,默認端口80 #-A OUTPUT -p tcp -d www.baidu.com --dport 80 -j DROP #限制ip+端口 #-A OUTPUT -p tcp -d 120.**.**.*** --dport 8090 -j DROP COMMIT # Completed on Mon Mar 9 12:05:45 2020
限制和取消后都需要重啟iptables服務:service iptables restart
測試:
封鎖前:
禁用后:
沒有任何返回