cecche revised this gist . Go to revision
1 file changed, 7 insertions
example.json(file created)
@@ -0,0 +1,7 @@ | |||
1 | + | { | |
2 | + | "eth0": { | |
3 | + | "high": [22,80,443], | |
4 | + | "medium": [8096,9091], | |
5 | + | "low": [51413] | |
6 | + | } | |
7 | + | } |
cecche revised this gist . Go to revision
No changes
cecche revised this gist . Go to revision
1 file changed, 51 insertions
netprio.sh(file created)
@@ -0,0 +1,51 @@ | |||
1 | + | #!/bin/bash | |
2 | + | ||
3 | + | # Check if jq is installed | |
4 | + | if ! command -v jq &> /dev/null | |
5 | + | then | |
6 | + | echo "jq is not installed. Please install it before running the script." | |
7 | + | exit 1 | |
8 | + | fi | |
9 | + | ||
10 | + | # Check if a JSON file is provided as an argument | |
11 | + | if [ -z "$1" ]; then | |
12 | + | echo "Please provide the JSON configuration file as an argument." | |
13 | + | exit 1 | |
14 | + | fi | |
15 | + | ||
16 | + | # Read the JSON file | |
17 | + | JSON_FILE=$1 | |
18 | + | ||
19 | + | # Apply rules for each network interface (eno1, wg0, etc.) | |
20 | + | for interface in $(jq -r 'keys[]' "$JSON_FILE"); do | |
21 | + | echo "Applying rules for interface $interface..." | |
22 | + | ||
23 | + | # Delete existing qdisc if it exists | |
24 | + | tc qdisc del dev "$interface" root 2>/dev/null | |
25 | + | ||
26 | + | # Set the prio queue for the interface | |
27 | + | tc qdisc add dev "$interface" root handle 1: prio | |
28 | + | ||
29 | + | # Set high priority ports | |
30 | + | high_ports=$(jq -r ".\"$interface\".high[]" "$JSON_FILE") | |
31 | + | for port in $high_ports; do | |
32 | + | echo " Setting port $port as high priority on $interface..." | |
33 | + | tc filter add dev "$interface" protocol ip parent 1: prio 0 u32 match ip dport "$port" 0xffff flowid 1:0 | |
34 | + | done | |
35 | + | ||
36 | + | # Set medium priority ports | |
37 | + | medium_ports=$(jq -r ".\"$interface\".medium[]" "$JSON_FILE") | |
38 | + | for port in $medium_ports; do | |
39 | + | echo " Setting port $port as medium priority on $interface..." | |
40 | + | tc filter add dev "$interface" protocol ip parent 1: prio 1 u32 match ip dport "$port" 0xffff flowid 1:1 | |
41 | + | done | |
42 | + | ||
43 | + | # Set low priority ports | |
44 | + | low_ports=$(jq -r ".\"$interface\".low[]" "$JSON_FILE") | |
45 | + | for port in $low_ports; do | |
46 | + | echo " Setting port $port as low priority on $interface..." | |
47 | + | tc filter add dev "$interface" protocol ip parent 1: prio 2 u32 match ip dport "$port" 0xffff flowid 1:2 | |
48 | + | done | |
49 | + | done | |
50 | + | ||
51 | + | echo "Rules have been successfully applied." |