聊天工具

兩個異地用戶連接到同一臺linux設備時,經常需要溝通交流,尤其是排錯的過程中,工作中遇到好多次,每次都是用QQ電話等,總覺得不太方便,於是就用shell寫了個簡單的工具。

1、執行時,可以指定兩個參數,第一個是存放臨時文件的 目錄,第二個是用戶名,可以只指定目錄,如果都不指定,則會使用默認目錄,並使用whoami獲取用戶名; 對話雙方必須指定相同目錄
2、同一個目錄下,僅支持兩個用戶。
3、q鍵或ctrl+c退出,一方終止對話,另一方自動關閉。
4、對話過程中, 以“command:”開頭的,則在本地執行其後的指令,例如:command:ifconfig ,則會在當前執行ifconfig指令; 以“ly:”開頭的,則會將其後指令的執行結果發送給對方。

一方執行腳本後會等待另一方進入

 

 
可以對話了



當然支持中文
 

 

代碼專區:

#!/bin/bash
# LingYi
# 2016.03.23

#package: command
#coreutils: whoami


[[ -n $1 ]] && [[ -d $1 ]] && tmp_dir=${1%/}

username=${2:-$(whoami)}
username=${username:-NoBody}
tmp_dir=${tmp_dir:-/tmp}
message_file=.talk_${username}.$(date +%s)
message_judge_file=${message_file}.renew
lock_file=.talk.lock

#設置字體顏色
MY_COLOR='45;1;39'
my_color='32'
OTHER_COLOR='46;1;39'
other_color='34'

#同一個臨時目錄下,只能有兩個用戶同時使用
if [[ -e ${tmp_dir}/${lock_file} ]]; then
	echo there have been two guys using the directory [\"${tmp_dir}\"], you can change it, and run again !!
	echo Like this: sh talk.sh /mnt [UserName]
	exit 2
fi

#創建兩個臨時文件,一個用於存儲用戶交互信息,另一個用於判斷對方是否更新了信息
touch ${tmp_dir}/$message_file || exit 3
touch ${tmp_dir}/$message_judge_file || exit 3
chmod 666 ${tmp_dir}/$message_judge_file
echo 0 >${tmp_dir}/$message_judge_file

#判斷對方是否在線,若在線,則設置對方相關信息,否則等待
function get_other_man()
{
	if [[ $(ls -a ${tmp_dir} | grep talk |grep -E -v "$message_file|renew|lock" | wc -l) -eq 1 ]]; then
		other_message_file=$( ls -a ${tmp_dir} | grep talk | grep -E -v "$message_file|renew|lock")
		other_man=$( echo $other_message_file | awk -F '[._]' '{print $3}' )
		other_message_judge_file=${other_message_file}.renew
		other_message=$(cat ${tmp_dir}/$other_message_file)
		[[ ! -f ${tmp_dir}/${lock_file}	]] && touch ${tmp_dir}/${lock_file}
	fi
}

#後臺監控函數
#用戶監控對方是否仍然在線,以及是否更新了信息,如果更新了則打印對方發送的信息,並處理判斷文件
function monitor()
{
	local stop_monitor=false
	trap 'stop_monitor=true' 20
	while ! $stop_monitor
	do
	    #若對方的臨時文件消失,則標示對方已離線,自己退出對話
	 	if [[ ! -e ${tmp_dir}/$other_message_judge_file ]]; then
			echo -e "\033[1;31m${other_man} is offline !!!\033[0m"
			echo -e "\033[1;32mBye!!!\033[0m"
			kill -20 $MYPID
			break
		fi
		#對方寫入信息到自己的信息存儲文件之後,會將此文件置爲“1”,表示更新了信息
		if [[ $(cat ${tmp_dir}/$other_message_judge_file) -eq 1 ]]; then
			echo -e "\033[${OTHER_COLOR}mFrom $other_man\033[0m\033[1;31m:\033[0m"
			echo -e "\033[${other_color}m$(cat ${tmp_dir}/$other_message_file)\n\033[0m"
			echo -ne "\033[${my_color}m"
			#打印了對方的信息之後,將對方的判斷文件置爲“0”,標示爲已讀
			echo 0 >${tmp_dir}/$other_message_judge_file
		fi
		sleep 0.2
	done
	echo -ne "\033[0m"
}

#對方離線或自行退出後執行
function talk_over()
{
    kill -20 $monitor_pid &>/dev/null
    rm -fr ${tmp_dir}/$message_file
    rm -fr ${tmp_dir}/$message_judge_file
	rm -fr ${tmp_dir}/$lock_file &>/dev/null
	sleep 0.2
	STOP=true
	echo
	exit
}

trap 'talk_over' 2
clear


#判斷對方是否在線,若不在線則一直等待,指導對方上線
get_other_man
[[ -z $other_man ]] && echo -e "\033[1;31mNo man to talk, please wait ...\033[0m"
while [[ -z $other_man ]]; do get_other_man; done
echo -e "\033[32m${other_man} is online, you can talk now.\033[0m"

MYPID=$$
monitor &
monitor_pid=$!

STOP=false
trap 'talk_over; STOP=true' 20
message="this is initialized words"
#stty erase ^H
#stty erase ^?

#對話主程序
while ! $STOP
do 
	[[ -z $other_man ]] && get_other_man
	[[ -n $message ]] && echo -e "\033[${MY_COLOR}mI say\033[0m\033[1;31m:\033[0m"
	echo -ne "\033[${my_color}m"
	read message
	echo -ne "\033[0m"
	[[ $message == 'q' ]] && talk_over
	[[ -z  $message    ]] && continue
	#定義command:開頭的字符串處理方式
	#command:
	if echo $message | grep -q '^command:'; then
		$(echo $message | awk -F':' '{print $2}')
		continue
	fi
	#定義ly:開頭的字符串處理方式
	#ly:
	if echo $message | grep -q '^ly:'; then
		message=$($(echo $message | awk -F':' '{print $2}'))
	fi
	#若有信息輸入,則將信息寫入自己的信息存儲文件,並將判斷文件置“1”(表示更新了文件)
	echo "$message" >${tmp_dir}/$message_file
	echo 1 >${tmp_dir}/$message_judge_file
done
echo


代碼下載鏈接

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章