Testing with root user permissions

This commit is contained in:
Emran Billah 2024-08-08 14:17:17 -03:00
parent da8f682ffd
commit 9c530434a5
1 changed files with 44 additions and 26 deletions

View File

@ -1,7 +1,4 @@
#!/bin/bash
#
# Usage: eg. ./test-run.sh -p /dev/ttyUSB0 -s 192.168.10.1 -d 192.168.10.2
#
# Function to display usage instructions
usage() {
@ -27,10 +24,12 @@ if [ -z "$xbee_port" ] || [ -z "$xbee_net_src_ip" ] || [ -z "$xbee_net_dst_ip" ]
usage
fi
# Function to install dependencies, build xbnet, and configure the XBee network interface
run_as_root() {
# Update package list and install necessary packages
echo "Installing necessary packages..."
sudo apt-get update
sudo apt-get install -y git build-essential libudev-dev iproute2 iputils-ping cargo
apt-get update
apt-get install -y git build-essential libudev-dev iproute2 iputils-ping cargo
# Clone the xbnet repository
echo "Cloning xbnet repository..."
@ -43,16 +42,35 @@ cargo build --release
# Copy the built binary to /usr/local/bin
echo "Installing xbnet..."
sudo cp target/release/xbnet /usr/local/bin/xbnet
cp target/release/xbnet /usr/local/bin/xbnet
# Run xbnet in the background
echo "Starting xbnet on $xbee_port..."
xbnet $xbee_port tun &
# Wait for the xbnet interface to be created
echo "Waiting for xbnet0 interface to be created..."
while ! ip link show xbnet0 > /dev/null 2>&1; do
sleep 1
done
# Configure the XBee network interface
echo "Configuring XBee network interface..."
sudo xbnet $xbee_port tun
sudo ip addr add $xbee_net_src_ip/24 dev xbnet0
sudo ip link set dev xbnet0 up
ip addr add $xbee_net_src_ip/24 dev xbnet0
ip link set dev xbnet0 up
# Ping the destination IP to verify connectivity
echo "Pinging destination IP $xbee_net_dst_ip..."
ping -c 4 $xbee_net_dst_ip
echo "Setup complete. The XBee network interface is configured and tested."
}
# Check if the script is running as root
if [ "$EUID" -ne 0 ]; then
echo "This script must be run as root. Re-running with sudo..."
sudo bash -c "$(declare -f run_as_root); run_as_root" "$@"
exit
else
run_as_root
fi