查看: 4928|回复: 0

[经验] 在树莓派上部署ATC网络模拟工具的自动化脚本

[复制链接]

该用户从未签到

发表于 2018-3-29 15:01:57 | 显示全部楼层 |阅读模式
分享到:
尝试了下在树莓派上搭建Augmented Traffic Control (ATC) 来模拟弱网测试环境,主要分为两个部分:

将树莓派设置成具有发射AP热点的能力
在树莓派上安装ATC
网上手动配置的教程方式很多,步骤比较多也容易出错,这里整理成一份自动化脚本配置的方式,主要参考Raspberry Pi 3 access-point-setup · GitHub

首先将树莓派设置成具有发射AP热点的能力

手动配置AP的步骤比较繁琐,可以使用rPi3-ap-setup.sh脚本进行自动化配置,执行以下命令,其中password rPi3AP可以自定义,对应热点密码和热点名称。以下脚本主要包括:
1.1. 给wlan0配置static ip
1.2. 使用hostapd配置一个热点AP的帐号密码等
1.3. 使用dnsmasq配置DHCP和DNS服务
  1. curl -sSL https://gist.githubusercontent.com/jeffreyzh/e7f29c01cd1f07e9ff58a9f7b57f3187/raw/f620e2b3a878fdae361010efeabefca4c3b06c52/rPi3-ap-setup.sh | sudo bash $0 password rPi3AP
复制代码

以上完成后,正常情况下可以搜索到热点 ,但热点可能无法连接,或者提示密码错误,那么可以执行以下命令:
  1. sudo wget -q https://gist.githubusercontent.com/Lewiscowles1986/390d4d423a08c4663c0ada0adfe04cdb/raw/5b41bc95d1d483b48e119db64e0603eefaec57ff/dhcpcd.sh -O /usr/lib/dhcpcd5/dhcpcd
  2. sudo chmod +x /usr/lib/dhcpcd5/dhcpcd
复制代码


此时设置热点AP已完成,但连接后无法上网,所以还需要配置热点AP使用有线网卡的链接网络,以下脚本主要包括:
3.1. 打开ipv4转发
3.2. 在wlan0和eth0之间配置NAT规则

  1. curl -sSL https://gist.githubusercontent.com/jeffreyzh/f6c03de9af9a9799a7725d0d0e917946/raw/f1d2477b2919c8887cfe578587f550c8883baedd/adapter-passthrough.sh | sudo bash $0
复制代码

以上的规则需要设置为重启时自动配置,可以执行:

  1. sudo sh -c "iptables-save > /etc/iptables.ipv4.nat"
复制代码

然后打开sudo nano /etc/rc.local,在exit 0之前添加一行:

  1. iptables-restore < /etc/iptables.ipv4.nat  
复制代码

此时AP配置完成,可以通过连接树莓派的热点来上网了!
在树莓派上配置AP的一些参考资料:

手动配置AP参考:Using your new Raspberry Pi 3 as a WiFi access point with hostapd
自动配置AP参考:Raspberry Pi 3 access-point-setup · GitHub
设置AP,设置完成后能手机能搜索到热点,但无法上网 Raspberry Pi 3 access-point-setup · GitHub
-dhcpcd5 Raspberry pi stretch allow dhcpcd5 with /etc/network/interfaces · GitHub
允许AP使用有线网卡来链接网络 :Allows passthrough for bridges, wireless access-point’s and range extenders · GitHub


安装ATC

以上AP设置完成后,可以开始部署atc,步骤参考官方文档Augmented Traffic Control

https://github.com/facebook/augmented-traffic-control

这里需要注意的是ATC所需的Python 和django版本
  1. Python 2.7: Currently, ATC is only supported on python version 2.7.
  2. Django 1.10: Currently, ATC is only supported using django version 1.10.
复制代码

下面记录部署时踩过的坑

pip instasll atc时提示 AttributeError: ‘module’ object has no attribute ‘lru_cache'

issues地址:get "'module' object has no attribute 'lru_cache'" issues when install django-atc-demo-ui

首先确定python和django的版本是否符合ATC要求:

  1. Python 2.7: Currently, ATC is only supported on python version 2.7.
  2. Django 1.10: Currently, ATC is only supported using django version 1.10.
复制代码

查看当前django版本的方式:

  1. pi@raspberrypi:~ $ python
  2. Python 2.7.13 (default, Nov 24 2017, 17:33:09)
  3. [GCC 6.3.0 20170516] on linux2
  4. Type "help", "copyright", "credits" or "license" for more information.
  5. >>> import django
  6. >>> django.get_version()
  7. '1.10'
  8. >>>
复制代码


如果不符合,使用pip安装指定版本

  1. sudo pip uninstall Django
  2. sudo pip install django==1.10.6
复制代码


pip会有缓存,可能发现安装了指定版本后,当前版本并没有改变,这样情况下可以尝试清理缓存: python - Removing pip’s cache? - Stack Overflow
卸载老的django版本:How to install Django
python -c "import django; print(django.__path__)"
如果问题仍然存在,可以强制指定Django的版本为1.10,这也是我最终解决问题的方式:
sudo pip install django-atc-demo-ui Django==1.10 --no-cache-dir

atc is not running:ATC和最新版django-rest-framework不兼容

issues地址 atc is not running
atc 和最新版本的django-rest-framework 不兼容
3294575-c306bc8ed8d551f7.png

查看atc的changelog发现从0.1.3版本支持django-rest-framework 3.2
CHANGELOG
https://github.com/facebook/augm ... 94199d/CHANGELOG.md

3294575-d8349f76830ef039.png

但是我安装3.2后,在使用sudo bash restore-profiles.sh设置默认profile时,还是会有兼容问题,会提示AttributeError: 'Options' object has no attribute 'get_all_related_objects'的错误Issue #351, 所以我最后使用3.4.0的版本,可以正常运行,但还未详细验证是否会起其他问题。 重新安装djangorestframework:

sudo pip install djangorestframework==3.4

atc not running:atcd启动时没有设置正确的网卡

执行完python manage.py runserver 0.0.0.0:8000后打开http://localhost:8000页面上提示atc not running
默认情况下执行sudo atcd是使用eth0外网网卡和eth1作为局域网网卡,如果网卡不同,需要按自身情况指定。举个例子:首先通过ifconfig 查看网卡情况

3294575-952c6d7fab2a4b97.png

可以看到eth0是网线,wlan0是ap无线热点,所以这里使用—atcd-wan和—atcd-lan来指定:
sudo atcd --atcd-wan eth0 --atcd-lan wlan0
执行成功会显示:
DEBUG:AtcdVService:All tasks started
DEBUG:AtcdVService:VService Active. Awaiting graceful shutdown.
然后新再开启一个终端窗口执行:
sudo python manage.py runserver 0.0.0.0:8000
最后打开地址即可:
http://localhost:8000/




回复

使用道具 举报

您需要登录后才可以回帖 注册/登录

本版积分规则

关闭

站长推荐上一条 /2 下一条



手机版|小黑屋|与非网

GMT+8, 2024-3-29 02:15 , Processed in 0.110329 second(s), 16 queries , MemCache On.

ICP经营许可证 苏B2-20140176  苏ICP备14012660号-2   苏州灵动帧格网络科技有限公司 版权所有.

苏公网安备 32059002001037号

Powered by Discuz! X3.4

Copyright © 2001-2020, Tencent Cloud.