在openwrt路由中加入n2n,並在ubuntu里加入n2n服務,實現開機聯網互通互訪
leekwenty發表於2019-02-18
版權宣告:本文為LeeKwen的原創文章,請聯絡微信@leekwenty後轉載。 https://blog.csdn.net/leekwen/article/details/45244551
在openwrt路由中加入n2n,並在ubuntu里加入n2n服務,實現開機聯網互通互訪
1、openwrt的硬體環境:
將自己的WR703N刷入openwrt(這裡需要修改FLASH晶片,換個8M的FLASH);
刷機後,安裝openwrt,此教程網上一大堆,無需說明了,TELNET進入,並開啟SSH後,root登陸。
2、安裝n2n到openwrt中:
root@LeeKwen:~# opkg update <-----如果不能升級,請執行下條命令,檢視源是否正確:root@LeeKwen:~# cat /etc/opkg.conf
root@LeeKwen:~# opkg install n2n
執行上述命令即可安裝n2n。
3、修改n2n的配置檔案:
root@LeeKwen:~# cat /etc/config/n2n
config edge
option ipaddr '10.10.100.191'
option supernode '106.186.30.16'
option port '9876'
option community 'leekwen'
option key 'leekwen'
option route '1'
4、修改n2n服務的啟動指令碼:
root@LeeKwen:~# vi /etc/init.d/n2n
#!/bin/sh /etc/rc.common
# Copyright (C) 2008-2012 OpenWrt.org
START=90
start_instance() {
local cfg="$1"
config_get type "$cfg" TYPE
case "$type" in
edge)
config_get ipaddr "$cfg" 'ipaddr'
[ -n "$ipaddr" ] || return 1
config_get supernode "$cfg" 'supernode'
config_get port "$cfg" 'port'
config_get community "$cfg" 'community'
config_get key "$cfg" 'key'
config_get_bool route "$cfg" 'route' '0'
[ "$route" = "1" ] && args='-r'
service_start /usr/sbin/edge -f $args -a $ipaddr -c $community -k $key -l ${supernode}:${port} -M 1300
;;
supernode)
config_get port "$cfg" port
[ -n "$port" ] || return 1
service_start /usr/sbin/supernode -l $port
;;
esac
}
stop_instance() {
local cfg="$1"
config_get type "$cfg" TYPE
case "$type" in
edge)
service_stop /usr/sbin/edge
;;
supernode)
service_stop /usr/sbin/supernode
;;
esac
}
start() {
config_load 'n2n'
config_foreach start_instance 'edge'
config_foreach start_instance 'supernode'
}
stop() {
config_load 'n2n'
config_foreach stop_instance 'edge'
config_foreach stop_instance 'supernode'
}
5、將n2n加入openwrt系統啟動項:
root@LeeKwen:~# /etc/init.d/n2n enable6、啟動openwrt中的n2n服務:
root@LeeKwen:~# /etc/init.d/n2n startroot@LeeKwen:~# ifconfig edge0
edge0 Link encap:Ethernet HWaddr 86:CC:1B:E9:1A:DC
inet addr:10.10.100.191 Bcast:10.10.100.255 Mask:255.255.255.0
UP BROADCAST RUNNING MULTICAST MTU:1300 Metric:1
RX packets:1117 errors:0 dropped:22 overruns:0 frame:0
TX packets:698 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:500
RX bytes:104415 (101.9 KiB) TX bytes:98732 (96.4 KiB)
7、互通測試:與我的手機端 n2n互ping下
root@LeeKwen:~# ping 10.10.100.103
PING 10.10.100.103 (10.10.100.103): 56 data bytes
64 bytes from 10.10.100.103: seq=0 ttl=64 time=31.738 ms
64 bytes from 10.10.100.103: seq=1 ttl=64 time=25.347 ms
64 bytes from 10.10.100.103: seq=2 ttl=64 time=22.565 ms
64 bytes from 10.10.100.103: seq=3 ttl=64 time=40.063 ms
64 bytes from 10.10.100.103: seq=4 ttl=64 time=23.577 ms
^C
--- 10.10.100.103 ping statistics ---
6 packets transmitted, 5 packets received, 16% packet loss
round-trip min/avg/max = 22.565/19.838/31.738 ms
在ubuntu系統中增加n2n服務:
1、我已經在上篇部落格中有寫如何在ubuntu安裝n2n
(未安裝過的使用者請移駕http://blog.csdn.net/leekwen/article/details/45095887)
這裡只說明增加n2n開機服務,具體步驟如下:
Leekwen@Leekwen:~$ svn co https://svn.ntop.org/svn/ntop/trunk/n2n
Leekwen@Leekwen:~$ cd n2n/
Leekwen@Leekwen:~/n2n$ ls
n2n_v1 n2n_v2
Leekwen@Leekwen:~/n2n$ cd n2n_v1/
Leekwen@Leekwen:~/n2n/n2n_v1$ ls
COPYING edge.8 HACKING lzoconf.h Makefile minilzo.h n2n.h openwrt scripts supernode.c tuntap_linux.c twofish.c win32
debian edge.c INSTALL lzodefs.h minilzo.c n2n.c n2n.spec README supernode.1 tuntap_freebsd.c tuntap_osx.c twofish.h
Leekwen@Leekwen:~/n2n/n2n_v1$ make
Leekwen@Leekwen:~/n2n/n2n_v1$ ./edge -h
Welcome to n2n v.1.3.2 for i686
Built on 04/23/15 06:11:56 PM
Copyright 2007-08 - http://www.ntop.org
edge -d <tun device> -a <tun IP address> -c <community> -k <encrypt key> -s <netmask> [-u <uid> -g <gid>][-f][-m <MAC address>]
-l <supernode host:port> [-p <local port>] [-M <mtu>] [-t] [-r] [-v] [-b] [-h]
-d <tun device> | tun device name
-a <tun IP address> | n2n IP address
-c <community> | n2n community name
-k <encrypt key> | Encryption key (ASCII) - also N2N_KEY=<encrypt key>
-s <netmask> | Edge interface netmask in dotted decimal notation (255.255.255.0)
-l <supernode host:port> | Supernode IP:port
-b | Periodically resolve supernode IP
| (when supernodes are running on dynamic IPs)
-p <local port> | Local port used for connecting to supernode
-u <UID> | User ID (numeric) to use when privileges are dropped
-g <GID> | Group ID (numeric) to use when privileges are dropped
-f | Fork and run as a daemon. Use syslog.
-m <MAC address> | Choose a MAC address for the TAP interface
| eg. -m 01:02:03:04:05:06
-M <mtu> | Specify n2n MTU (default 1400)
-t | Use http tunneling (experimental)
-r | Enable packet forwarding through n2n community
-v | Verbose
Environment variables:
N2N_KEY | Encryption key (ASCII)
Leekwen@Leekwen:~/n2n/n2n_v1$ pwd
/home/Leekwen/n2n/n2n_v1
2、下載並編譯完畢後,編寫啟動服務檔案n2n:
Leekwen@Leekwen:~/n2n/n2n_v1$ vi n2n#!/bin/bash
##Some Variable define here ##
PROG="edge"
n2n_IP="10.10.100.103"
n2n_cname="leekwen"
n2n_pass="leekwen"
SNODE_Host="106.186.30.16"
SNODE_Port="9876"
LOGS="/var/log/$PROG.log"
PROG_PATH="/home/leekwen/n2n/n2n_v1" # edge binary put on here for testing this script, Not Needed if the system binary.
PROG_ARGS="-f -d edge0 -a $n2n_IP -c $n2n_cname -k $n2n_pass -l $SNODE_Host:$SNODE_Port -M 1300"
PID_PATH="/var/run"
RETVAL=0
start() {
pid=`ps ax | grep $PROG | grep -v "grep" | awk '{print $1}'`
if [ ! -z "$pid" ]; then
## Program is running, exit with error.
echo "Error! $PROG is currently running!" 1>&2
exit 1
else
## Change from /dev/null to something like /var/log/$PROG if you want to save output.
$PROG_PATH/$PROG $PROG_ARGS 2>&1 > $LOGS &
echo "$PROG started"
echo $pid > "$PID_PATH/$PROG.pid"
fi
RETVAL=$?
if [ $RETVAL -eq 0 ]; then
echo -e "\\033[60G\c"
echo -e "[ \\033[1;32m\c"
echo -e "OK\c"
echo -e "\\033[0;39m\c"
echo " ]"
else
echo -e "\\033[60G\c"
echo -e "[ \\033[1;31m\c"
echo -e "FAILED\c"
echo -e "\\033[0;39m\c"
echo " ]"
fi
return $RETVAL
}
stop() {
pid=`ps ax | grep $PROG | grep -v "grep"| awk '{print $1}'`
if [ ! -z "$pid" ]; then
## Program is running, so stop it
kill $pid
rm -f "$PID_PATH/$PROG.pid"
echo "$PROG stopped"
else
## Program is not running, exit with error.
echo "Error! $PROG not started!" 1>&2
exit 1
fi
RETVAL=$?
if [ $RETVAL -eq 0 ]; then
echo -e "\\033[60G\c"
echo -e "[ \\033[1;32m\c"
echo -e "OK\c"
echo -e "\\033[0;39m\c"
echo " ]"
else
echo -e "\\033[60G\c"
echo -e "[ \\033[1;31m\c"
echo -e "FAILED\c"
echo -e "\\033[0;39m\c"
echo " ]"
fi
return $RETVAL
}
## Check to see if we are running as root first.
## Found at http://www.cyberciti.biz/tips/shell-root-user-check-script.html
if [ "$(id -u)" != "0" ]; then
echo "This script must be run as root, you can use sudo command to run it." 1>&2
exit 1
fi
case "$1" in
start)
start
exit 0
;;
stop)
stop
exit 0
;;
reload|restart|force-reload)
stop
start
exit 0
;;
**)
echo "Usage: $0 {start|stop|reload}" 1>&2
exit 1
;;
esac
3、增加可執行許可權,移動n2n服務到/etc/init.d/,並更新rc.d
Leekwen@Leekwen:~/n2n/n2n_v1$ chmod 755 n2n
Leekwen@Leekwen:~/n2n/n2n_v1$ sudo mv n2n /etc/init.d/
Leekwen@Leekwen:~/n2n/n2n_v1$ sudo update-rc.d n2n defaults
4、對n2n服務進行測試:
Leekwen@Leekwen:~/n2n/n2n_v1$ sudo /etc/init.d/n2n start
Leekwen@Leekwen:~/n2n/n2n_v1$ ifconfig edge0
Leekwen@Leekwen:~/n2n/n2n_v1$ sudo /etc/init.d/n2n stop
Leekwen@Leekwen:~/n2n/n2n_v1$ ifconfig edge0
Leekwen@Leekwen:~/n2n/n2n_v1$ sudo /etc/init.d/n2n restart
Leekwen@Leekwen:~/n2n/n2n_v1$ ifconfig edge0
5、對n2n服務進行Ping連通性測試:
Leekwen@Leekwen:~/n2n/n2n_v1$ ping 10.10.100.103
PING 10.10.100.103 (10.10.100.103): 56 data bytes
64 bytes from 10.10.100.103: seq=0 ttl=64 time=11.738 ms
64 bytes from 10.10.100.103: seq=1 ttl=64 time=15.347 ms
64 bytes from 10.10.100.103: seq=2 ttl=64 time=12.565 ms
64 bytes from 10.10.100.103: seq=3 ttl=64 time=10.063 ms
64 bytes from 10.10.100.103: seq=4 ttl=64 time=13.577 ms
^C
--- 10.10.100.103 ping statistics ---
5 packets transmitted, 5 packets received, 0% packet loss
round-trip min/avg/max = 10.063/12.838/15.347 ms
到此客戶端的已經安裝成功,在網路連線的情況下,每次重啟ubuntu及openwrt路由後,他們都會自動連線上supernode節點,以構建一個p2p的網路。
相關文章
- openwrt加入leveldb包編譯2020-10-23編譯
- 聯易融加入區塊鏈服務網路發展聯盟2022-06-21區塊鏈
- 配置ACL在網路服務中訪問控制2020-11-07
- 在Ubuntu上開啟SSH服務2024-07-02Ubuntu
- 百望雲加入區塊鏈服務網路發展聯盟2022-05-31區塊鏈
- 如何將MongoDB加入到Windows服務中2018-10-12MongoDBWindows
- Istio為什麼現在申請加入CNCF?2022-05-06
- Discovery加入AKOYA全球服務提供商網路2023-05-19
- 在Ubuntu中檢視網路路由表的方法2020-09-27Ubuntu路由
- linux中透過systemctl建立服務並實現開機啟動2024-04-11Linux
- 使用ensp搭建路由拓撲,並使用ospf協議實現網路互通實操2023-10-08路由協議
- 使用ensp搭建路由拓撲,並使用isis協議實現網路互通實操2023-10-05路由協議
- nacos 加入開機啟動2024-12-06
- [譯] 在 JSX 程式碼中可以加入 console.log 嗎?2019-03-03JS
- 在Ubuntu上配置SSH訪問並關閉防火牆2024-10-17Ubuntu防火牆
- 達策加入ACloudster聯盟,攜手SAP開展全球業務2020-03-09Cloud
- 中移物聯網在車聯網場景的 TiDB 探索和實現2020-10-14TiDB
- 亞馬遜雲服務(AWS)助力豐田互聯中國車聯網服務全面落地2020-11-05亞馬遜
- 如何實現在家訪問公司內網服務2020-09-09內網
- 在自己的域名網站安裝Kubernetes並暴露服務2018-09-30網站
- 系統整合解決方案,實現企業內部數字化業務互聯互通2023-04-27
- Ubuntu 20.04使用PBIS工具加入AD域2022-04-08Ubuntu
- 安全服務廠商安全狗加入龍蜥社群2022-06-16
- 在虛擬機器上搭建主機可訪問的web服務2019-03-29虛擬機Web
- 強強聯合!Flat Ads出席雲棲大會,加入阿里雲“橙”雲出海服務聯盟2023-11-08阿里
- 重磅 | 騰訊雲服務網格開源專案 Aeraki Mesh 加入 CNCF 雲原生全景圖2022-03-18
- 乾淨架構在 Web 服務開發中的實踐2019-01-10架構Web
- 在Spring Boot中實現API閘道器與路由2024-07-22Spring BootAPI路由
- 加入購物車動畫效果實現2018-08-15動畫
- Mininet主機與真實網路互通方案實現2018-07-29
- 在網站中實現 React tsarticles2024-09-22網站React
- netty服務端監聽客戶端連線加入和斷開事件2020-11-12Netty服務端客戶端事件
- 開源OA系統搭建教程:在服務或代理中實現sftp的上傳2021-10-11FTP
- 在物聯網中應用機器學習2019-02-28機器學習
- 在K8S中,Worker節點加入叢集的全過程?2024-10-15K8S
- 實驗9.單臂路由實現Vlan互通實驗2024-06-25路由
- 【GeoScene】一、建立、釋出路網服務,並在程式碼中測試最短路徑分析2024-08-02
- 百度點石隱私計算平臺與FATE開源框架實現互聯互通2023-03-09框架