Skip to content

Commit e779998

Browse files
committed
create_update, arp-ip.sh, sue-flash
1 parent 4b987ad commit e779998

File tree

4 files changed

+238
-0
lines changed

4 files changed

+238
-0
lines changed

script/arp-compare/arp-compare.py

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
import subprocess
2+
import curses
3+
import sys
4+
5+
def run_arp_scan(interface):
6+
cmd = ["arp-scan", "-l", "-I", interface]
7+
output = subprocess.check_output(cmd, universal_newlines=True)
8+
devices = parse_arp_scan_output(output)
9+
return devices
10+
11+
def parse_arp_scan_output(output):
12+
devices = {}
13+
lines = output.strip().split('\n')
14+
for line in lines:
15+
if line.startswith('10.1'): # Modify this to match your network IP range
16+
parts = line.split('\t')
17+
ip = parts[0]
18+
mac = parts[1]
19+
name = parts[2] if len(parts) >= 3 else "Unknown"
20+
devices[mac] = (ip, name)
21+
return devices
22+
23+
def compare_scans(scan1, scan2):
24+
devices_only_in_scan1 = {mac: scan1[mac] for mac in scan1 if mac not in scan2}
25+
devices_only_in_scan2 = {mac: scan2[mac] for mac in scan2 if mac not in scan1}
26+
return devices_only_in_scan1, devices_only_in_scan2
27+
28+
def wait_for_keypress():
29+
stdscr = curses.initscr()
30+
stdscr.addstr("Press any key to continue...")
31+
stdscr.refresh()
32+
stdscr.getch()
33+
curses.endwin()
34+
35+
def print_devices(devices):
36+
for mac, (ip, name) in devices.items():
37+
print(f"IP: {ip}\tMAC: {mac}\tName: {name}")
38+
39+
def main():
40+
if len(sys.argv) < 2:
41+
print("Usage: python arp_scan.py <interface>")
42+
return
43+
44+
interface = sys.argv[1]
45+
print("Running first ARP scan...")
46+
scan1 = run_arp_scan(interface)
47+
print("First ARP scan completed.\n")
48+
49+
wait_for_keypress()
50+
51+
print("Running second ARP scan...")
52+
scan2 = run_arp_scan(interface)
53+
print("Second ARP scan completed.\n")
54+
55+
devices_only_in_scan1, devices_only_in_scan2 = compare_scans(scan1, scan2)
56+
57+
print("Devices only in first scan:")
58+
print_devices(devices_only_in_scan1)
59+
print("\nDevices only in second scan:")
60+
print_devices(devices_only_in_scan2)
61+
62+
if __name__ == "__main__":
63+
main()
64+

script/arp-ip.sh

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
#!/bin/bash
2+
3+
interface=$1
4+
5+
if [[ -z "$interface" ]]; then
6+
# Use the ifconfig command to get network interface information
7+
ifconfig_output=$(ifconfig)
8+
9+
# Use grep and awk to extract the name of the active interface
10+
interface=$(ip -o link show | awk '$2 != "lo:" && $9 == "UP" {print $2}' | cut -d ":" -f 1)
11+
12+
if [[ -z "$interface" ]]; then
13+
echo "Error: failed to auto-detect active network interface."
14+
exit 1
15+
fi
16+
fi
17+
18+
# Scan the local network interface for devices
19+
devices="`sudo arp-scan --interface=$interface --localnet | sed '1,2d'`"
20+
selected="`(echo "$devices" | fzf)`"
21+
22+
if [[ -z "$selected" ]]; then
23+
exit 1
24+
fi
25+
26+
# Get the IP address of the selected device
27+
ip=$(sudo arp-scan --interface=$interface --localnet | grep "$selected" | awk '{print $1}')
28+
echo $ip
29+

script/create_update_json.sh

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
#!/bin/bash
2+
3+
# Define the file name
4+
file="image.swu"
5+
url=""
6+
json_file="info.json"
7+
forced_update=false
8+
9+
# Parse command line arguments using a loop
10+
while [[ $# -gt 0 ]]; do
11+
case "$1" in
12+
--file=*)
13+
file="${1#*=}"
14+
;;
15+
--url=*)
16+
url="${1#*=}"
17+
;;
18+
--force=*)
19+
forced_update="${1#*=}"
20+
if [[ "$forced_update" != "true" && "$forced_update" != "false" ]]; then
21+
echo "Error: Invalid argument for --force. Please use 'true' or 'false'."
22+
exit 1
23+
fi
24+
;;
25+
--mac=*)
26+
mac="${1#*=}"
27+
;;
28+
*)
29+
echo "Error: Invalid argument: $1"
30+
exit 1
31+
;;
32+
esac
33+
shift
34+
done
35+
36+
# Check if the --url argument is provided and not empty
37+
if [ -z "$url" ]; then
38+
echo "Error: Missing or empty --url argument. Please provide a valid URL."
39+
exit 1
40+
fi
41+
42+
# Use sed to extract the third line of the file
43+
third_line=$(sed -n '3p' "$file")
44+
45+
# Use grep and regular expressions to extract the version variable
46+
if [[ $third_line =~ version\ =\ \"([^\"]+)\" ]]; then
47+
version="${BASH_REMATCH[1]}"
48+
echo "Found version in $file: $version"
49+
else
50+
echo "Error: Unable to extract version from the third line of $file."
51+
exit 1
52+
fi
53+
54+
# Use stat to get the size of the file
55+
file_size=$(stat -c %s "$file")
56+
echo "File size of $file is $file_size bytes."
57+
58+
# If a MAC address is provided, append it to the file name (after extracting the info)
59+
if [ -n "$mac" ]; then
60+
json_file="info.json_mac_$mac"
61+
fi
62+
63+
# Generate the JSON content
64+
json_content=$(cat <<EOF
65+
{
66+
"version": "$version",
67+
"url": "$url",
68+
"imageSize": $file_size,
69+
"forcedUpdate": $forced_update
70+
}
71+
EOF
72+
)
73+
74+
# Write the JSON content to the JSON file
75+
echo "$json_content" > "$json_file"
76+
77+
echo "JSON file '$json_file' created with the following content:"
78+
cat "$json_file"

script/sue-flash.sh

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
#!/bin/bash
2+
3+
# Function to check if a string is a valid IP address
4+
is_valid_ip() {
5+
local ip="$1"
6+
if [[ "$ip" =~ ^[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+$ ]]; then
7+
# Split the IP address into octets
8+
IFS='.' read -ra octets <<< "$ip"
9+
for octet in "${octets[@]}"; do
10+
if ((octet < 0 || octet > 255)); then
11+
return 1 # Invalid octet value
12+
fi
13+
done
14+
return 0 # Valid IP address
15+
else
16+
return 1 # Invalid format
17+
fi
18+
}
19+
20+
# Check if fzf is installed
21+
if ! command -v fzf &>/dev/null; then
22+
echo "Error: fzf is not installed. Please install fzf."
23+
exit 1
24+
fi
25+
26+
# Function to find .swu files
27+
find_swu_file() {
28+
local selected_file
29+
selected_file=$(find . -type l -name "image.swu" 2>/dev/null | fzf --prompt "Select .swu file: ")
30+
31+
if [ -z "$selected_file" ]; then
32+
return 1
33+
fi
34+
35+
echo "$selected_file"
36+
return 0
37+
}
38+
39+
# Attempt to find .swu file in the current directory or its subdirectories
40+
if selected_file=$(find_swu_file); then
41+
echo "Selected file: $selected_file"
42+
fi
43+
44+
# arg can be ip or interface for discovery
45+
if is_valid_ip "$1"; then
46+
echo "valid ip given, using"
47+
device_ip="$1"
48+
else
49+
echo "no valid ip, assuming it's an interface"
50+
device_ip=$(arp-ip $1) # can be empty for interface auto-try
51+
fi
52+
53+
# SSH to the device and run fwup
54+
ssh root@$device_ip "nsdk_cli invoke firmwareupdate:startLocalUpdate"
55+
56+
# Wait for the device to reboot (you might need to customize this)
57+
sleep 15
58+
59+
# Use curl to simulate the file upload through the web interface
60+
curl -F "image=@$selected_file" http://$device_ip/handle_post_request
61+
62+
# You might need to customize the URL and form field names depending on your device's web interface
63+
64+
# Add additional logic to verify the update process if needed
65+
# For example, you can check the device's status page or log files
66+
67+
echo "Firmware update completed."

0 commit comments

Comments
 (0)