Write bash script which check if one of two ip addresses exists in output of "ifconfig"…
- User
Write bash script which check if one of two ip addresses exists in output of "ifconfig" command and run sshd with specified config file
- Assistant
Certainly! Below is a simple Bash script that checks if either of two given IP addresses exists in the output of the
ifconfigcommand. If one of the IPs is found, it will start thesshdservice using a specified configuration file.Make sure you provide the correct path to your SSHD configuration file in the script.
#!/bin/bash # Specify the IP addresses to check IP1="192.168.1.10" # Change this to your first IP address IP2="192.168.1.20" # Change this to your second IP address # Specify your sshd config file SSHD_CONFIG="/etc/ssh/sshd_config" # Change this to your config file path # Check if ifconfig exists if ! command -v ifconfig &> /dev/null then echo "ifconfig could not be found. Please install net-tools." exit 1 fi # Check if either of the IP addresses is present in the ifconfig output if ifconfig | grep -q -E "$IP1|$IP2"; then echo "One of the IP addresses ($IP1 or $IP2) exists in the output of ifconfig." # Start sshd with the specified config file echo "Starting sshd with the specified config file: $SSHD_CONFIG" /usr/sbin/sshd -f "$SSHD_CONFIG" else echo "Neither $IP1 nor $IP2 is found in the output of ifconfig." exit 1 fi - User
I mean different sshd configurations for each ip
- Assistant
Certainly! If you want to run different
sshdconfigurations based on which IP address is active, you can modify the script accordingly. Below is an updated version of the script that checks for each IP address and starts with a different configuration file depending on which IP is found.