Monday, September 14, 2015

Python as a tftp server

There is a python package called tftpy which implements a tftp client and server. It's more handy for me to set up a temporary tftp server, compared with Fedora's tftp-server package, which relies on systemd.

In order to listen on port 69, python must be running as root. Then, type these lines in the python interactive shell. 

from tftpy import TftpServer
server = TftpServer("tftproot")
serve.listen()

tftproot is the path to the tftproot directory.

Tuesday, September 1, 2015

Redshift on Fedora 22

Redshift adjusts the screen's color temperature according to the time. It relieves your eyes when working at night. It's homepage is http://jonls.dk/redshift

To install redshift on Fedora 22, type sudo dnf install redshift in terminal. You may also install redshift-gtk, a GUI front-end for gnome desktop. Then you should enable location service in privacy settings. Also add these lines to /etc/geoclue/geoclue.conf to allow redshift to get location from geoclue.

[redshift]
allowed=true
system=false
users=

Finally, type redshift in terminal, and you should notice the change of the color temperature of your screen. However, the shipped systemd service does not work (redshift starts, but no change of color temperature, quite mysterious.) So I installed redshift-gtk and make it autostart when I login. 

Saturday, August 29, 2015

Openconnect Server (ocserv) on Openwrt

I'd like to set up a VPN server on a router running Openwrt. There are several choices, L2TP, PPTP, OpenVPN and openconnect. PPTP is not very secure. L2TP must be used with IPSec for security. While OpenVPN and openconnect are both SSL-VPN. They are easy to configure and adaptive to the restrictions of ISP. Because there is a luci app for openconnect server. It's easier to set up than OpenVPN. I would set up an openconnect server, which operates in pseudo-bridge mode, meaning remote clients are on the same subnet as the computers at home.

Tuesday, July 28, 2015

Openwrt Synchronized Wifi Hotspot

There are three wireless routers at my home. All run Openwrt. One acts as gateway. The other two are APs. Sometimes, my mother wants to turn off WiFi before sleep. She would power off the gateway router. As a result, all clients will connect to the other two APs, and cannot access the Internet.

I want to make things more elegant. When the two APs detect that the gateway is down, they will turn off their wifi as well, and turn it on when the gateway is online again. Fortunately, the two APs have enough ROM for python. So I need not mess around with shell scripts.

Friday, July 24, 2015

Netgear WNR2000 v4 刷 OpenWrt 和 Gargoyle

最近淘了一个二手的WNR2000,硬件版本v4,使用AR9341单芯片方案,4MB闪存,32MB内存。发现自带固件功能太弱,可玩性不高,故刷Openwrt.

WNR2000是Openwrt支持的硬件,可以参考http://wiki.openwrt.org/toh/netgear/wnr2000,但是这个网页里的信息过时了。telnetenable已经不适用于这台WNR2000。可喜的是电路板上留有TTL焊盘,焊上排针就可以使用。离网络口隔离变压器最远的是GND,然后是RX,TX,波特率是115200。

连接上TTL即可看到终端,官方固件是基于Openwrt改造的,进入终端还有Openwrt的字符画。不过可用的指令很少。

Monday, July 6, 2015

用netcat传送文件

一些特殊情况下,比如ssh挂掉了,可以用netcat传输文件。当然,是明文传输。

首先在接收端listen,输入
nc -l 6000 > filename
然后在发送端输入
nc IP 6000 < file_to_send

IP是接收端IP地址,filename是文件保存的名字。

也可以改成在发送端Listen,适用于BusyBox里不支持-l的netcat

发送端:
cat file_to_send | pv -b | nc -l -p 3333
接收端:
nc IP 3333 > filename


还有一个办法,用Python的SimpleHTTPServer和wget

服务端:
python -m SimpleHTTPServer
会在当前目录下运行一个HTTP服务器,端口号为8000

客户端:
wget http://IP:8000/filename

Saturday, May 16, 2015

Python实现A记录查询

最近在学计算机网络,用的教材是Computer Networking: A Top Down Approach. 第二章节介绍了DNS。以前用Python实现过基于TCP和文本应答的协议,不过没有处理过DNS这样结构紧凑的协议。

由于Python本身是没有类型的,而DNS对每一项的字长有严格要求(这种协议用C实现相当直接),因此要先生成固定长度的数据。有多种实现方法,比如:
bytearray, struct.pack, binascii.unhexlify, 直接在string中使用\x转义