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 #!/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 # Function to display usage instructions
usage() { usage() {
@ -27,32 +24,53 @@ if [ -z "$xbee_port" ] || [ -z "$xbee_net_src_ip" ] || [ -z "$xbee_net_dst_ip" ]
usage usage
fi fi
# Update package list and install necessary packages # Function to install dependencies, build xbnet, and configure the XBee network interface
echo "Installing necessary packages..." run_as_root() {
sudo apt-get update # Update package list and install necessary packages
sudo apt-get install -y git build-essential libudev-dev iproute2 iputils-ping cargo echo "Installing necessary packages..."
apt-get update
apt-get install -y git build-essential libudev-dev iproute2 iputils-ping cargo
# Clone the xbnet repository # Clone the xbnet repository
echo "Cloning xbnet repository..." echo "Cloning xbnet repository..."
git clone https://github.com/jgoerzen/xbnet.git /usr/src/xbnet git clone https://github.com/jgoerzen/xbnet.git /usr/src/xbnet
# Build xbnet # Build xbnet
echo "Building xbnet..." echo "Building xbnet..."
cd /usr/src/xbnet cd /usr/src/xbnet
cargo build --release cargo build --release
# Copy the built binary to /usr/local/bin # Copy the built binary to /usr/local/bin
echo "Installing xbnet..." echo "Installing xbnet..."
sudo cp target/release/xbnet /usr/local/bin/xbnet cp target/release/xbnet /usr/local/bin/xbnet
# Configure the XBee network interface # Run xbnet in the background
echo "Configuring XBee network interface..." echo "Starting xbnet on $xbee_port..."
sudo xbnet $xbee_port tun xbnet $xbee_port tun &
sudo ip addr add $xbee_net_src_ip/24 dev xbnet0
sudo ip link set dev xbnet0 up
# Ping the destination IP to verify connectivity # Wait for the xbnet interface to be created
echo "Pinging destination IP $xbee_net_dst_ip..." echo "Waiting for xbnet0 interface to be created..."
ping -c 4 $xbee_net_dst_ip while ! ip link show xbnet0 > /dev/null 2>&1; do
sleep 1
done
echo "Setup complete. The XBee network interface is configured and tested." # Configure the XBee network interface
echo "Configuring XBee network interface..."
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