From 9e6485da41da7464a0253e1a83261bf1fa6e3ba0 Mon Sep 17 00:00:00 2001 From: then Date: Sat, 21 May 2022 15:39:01 +0200 Subject: [PATCH] added scripts and readme --- README.md | 26 ++++++++- netchatte.sh | 151 +++++++++++++++++++++++++++++++++++++++++++++++++++ rx.sh | 8 +++ screenrc | 6 ++ tx.sh | 131 ++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 320 insertions(+), 2 deletions(-) create mode 100644 netchatte.sh create mode 100644 rx.sh create mode 100644 screenrc create mode 100644 tx.sh diff --git a/README.md b/README.md index ad113a1..30d79c2 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,25 @@ -# netchatte +``` + ,-. _,---._ __ / \ + / ) .-' `./ / \ +( ( ,' `/ /| + \ `-" \'\ / | + `. , \ \ / | + /`. ,'-`----Y | + ( ; | ' + | ,-. ,-' | / + | | ( | hjw | / + ) | \ `.___________|/ + `--' `--'` + ``` + +copyleft, netchatte is a netcat based terminal +chat program, inspired by 'unnamed project' +and made during the first tangible clouds worksession 18th → 21st May 2022 in Brussels +(https://tangible-cloud.be/) + + +# usage: chmod all .sh files, be on the same network and run ./netchatte.sh + + + -netchatte is a netcat based terminal chat program, inspired by 'unnamed project' and made during the first tangible clouds worksession 18th → 21st May 2022 in Brussels (https://tangible-cloud.be/) diff --git a/netchatte.sh b/netchatte.sh new file mode 100644 index 0000000..b5d70e1 --- /dev/null +++ b/netchatte.sh @@ -0,0 +1,151 @@ +#!/usr/bin/env bash +# usage: chmod all .sh files, be on the same network and run ./netchatte.sh +# todo: +# nicknames +# filesharing + + +# copyleft, netchatte is a netcat based terminal chat +# program, inspired by 'unnamed project' and made during the first tangible clouds worksession +# 18th → 21st May 2022 in Brussels (https://tangible-cloud.be/) +# + +#init +unset ALLCLIENTS +unset TEMPCLIENTS +unset IP +#define listen port +#define server port.... + + +#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 +} + + + + +scan () +{ + printf "\n" + sleep 0 + echo "Searching 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 "." + #printf "\n" + + else + #echo "Found the following clients:" + #echo "$i" + REALCLIENTS+=("${i}") + fi + + + + + done + + + # Iterate over an array to create select menu + if [ ${#REALCLIENTS[@]} -eq 0 ]; then + echo "No clients found :(" + printf "\n" + else + echo "Found a friend!:" + printf "\n" + #echo "${#REALCLIENTS[@]}" + fi + 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}") + # Further processing + + ;; + "Back to main menu") + printf "\n" + mainMenu + #break + ;; + *) + echo "Not available" + ;; + esac + done + +} + + +chat () +{ + screen -c screenrc #launch split screen session with tx.sh and rx.sh +} + +#Main 'loop' +echo "booting...netchatte" +sleep 1 +printf "\n" + +cat << "EOF" + ,-. _,---._ __ / \ + / ) .-' `./ / \ +( ( ,' `/ /| + \ `-" \'\ / | + `. , \ \ / | + /`. ,'-`----Y | + ( ; | ' + | ,-. ,-' | / + | | ( | hjw | / + ) | \ `.___________|/ + `--' `--' +EOF + + +killall nc &> /dev/null # clean up (maybe add killall screen..haha dirty) +mainMenu #start main function + + + + diff --git a/rx.sh b/rx.sh new file mode 100644 index 0000000..d45e7f0 --- /dev/null +++ b/rx.sh @@ -0,0 +1,8 @@ +#!/usr/bin/env bash +LOCALIP=$(ipconfig getifaddr en0) + + +while true +do + nc -lk ${LOCALIP} 1234 #this shoul be remote ip +done diff --git a/screenrc b/screenrc new file mode 100644 index 0000000..4924327 --- /dev/null +++ b/screenrc @@ -0,0 +1,6 @@ +bindkey ^D quit +screen -t you bash ./tx.sh +split +focus down +screen -t friend bash ./rx.sh +focus up \ No newline at end of file diff --git a/tx.sh b/tx.sh new file mode 100644 index 0000000..26f9281 --- /dev/null +++ b/tx.sh @@ -0,0 +1,131 @@ +#!/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 + + + +