#!/usr/bin/env bash LOCALIP=$(ipconfig getifaddr en0) #setup foundfriend=0 #functions mainMenu () { printf "\n ===== MAIN MENU ===== \n" printf "\n" echo "Hello, pick an option:" printf "\n" select mainMenu in "Who is online?" "Chat" do #echo $mainMenu case $mainMenu in "Who is online?") scan ;; Chat) chat ;; # if [ '1' = "$REPLY" ]; then # scan # fi #echo "Selected number: $REPLY" esac done } while : ; do unset ALLCLIENTS unset TEMPCLIENTS unset REALCLIENTS printf "\n" echo "Waiting for friends..." sleep 2 #printf "\n" #scanOutput=$(arp -a | awk -F'[()]' '{print $2}' | head -1) # find all machines on the network while IFS= read -r LINE; do ALLCLIENTS+=("${LINE}") #REALCLIENTS=( $(nc -zv -G 1 ${LINE} 1234 2>&1 | grep --line-buffered succeeded | awk '{print $3}') ) #see if port of client is open done < <(arp -a -x | awk '{print $1}' | tail -n +2) # for each machine, see if we can connect to it for i in "${ALLCLIENTS[@]}" do : TEMPCLIENTS=( $(nc -zv -G 1 $i 1234 2>&1 | grep --line-buffered succeeded | awk '{print $3}' )) if [ ${#TEMPCLIENTS[@]} -eq 0 ]; then echo -ne "." #printf "\n" else #echo "Found the following clients:" #echo "$i" REALCLIENTS+=("${i}") fi done #remove own IP from array (breaks the 'no clients found' script below...somehow) #echo ${LOCALIP} delete=${LOCALIP} #echo "localhost:.." echo "${REALCLIENTS[@]/$delete}" if [ ${#REALCLIENTS[@]} -eq 0 ]; then echo "No clients found :(" printf "\n" else if [ ${#REALCLIENTS[@]} -ne 0 ] && [ "${REALCLIENTS}" != "${LOCALIP}" ] ; then #remove your own ip, make a list you can choose from here, and add nickname echo "Found a friend!:" select IP in "${REALCLIENTS[@]}" "Back to main menu"; do case ${IP} in 1*) #limitation: ip must start with a 1.... echo "Client ${IP} selected" IP+=("${IP}") printf "\n" echo "${IP}" printf "\n" printf "Connecting..." printf "\n" sleep 1 printf "Connected! Type your message and press [enter], CTRL+D to quit:" printf "\n\n" #construct a string to be send to nc...todo # read -p 'Please choose a nickname: ' nickname # echo -ne "$nickname" # how to prepend a nickname and send it # https://superuser.com/questions/261900/how-can-i-pipe-commands-to-a-netcat-that-will-stay-alive nc ${IP} 1234 ;; "Back to main menu") printf "\n" mainMenu #break ;; *) echo "Not available" ;; esac done break fi sleep 5 fi done