Posts Tagged ‘script’

X-Chat2 script to preserve nickname

Monday, September 26th, 2011

I have been using X-Chat2 for quite a while now, and since my ISP cuts my line every 24h, i rejoin all IRC networks once my client detects the disconnect. This happens before the server realises, that the old connection is invalid, so my nickname is occupied by a ghost (zombie connection). There is a command to tell the server, that this situation has happened (ghosting), but not all servers support it.

Thats why i wrote this script, which listens for some events (e.g. the QUIT command, which is sent when the server realises, the other connection is dead) and then tries to change the nick back to the primary nick. It also listens to some more events and has a nickchange delay (so you dont spam the server with nickchanges in case of a problem). One could probably reduce the eventlisteners to the QUIT one.

I wrote this in perl, because i was looking at other plugins how the xchat API works, but it should be fairly easy to port it to python or c++, or whatever you want. No special license.

Install by simply renaming it to nickChanger.pl and putting it into your

~/.xchat2/

folder and ensuring that perl is installed. X-Chat will try to load it on start.

nickChanger.pl

 

Using BASH for network socket operation

Tuesday, June 8th, 2010

In order to solve a programming assignment (send a “TEST” to an UDP service and readout the result [beeing “success”]) i looked into socket programming with the BASH command shell.

Here is the code:

# configuration
HOST="127.0.0.1"
PORT="1337"

# define functions
socksend ()
{
	SENDME="$1"
	echo "sending: $SENDME"
	echo -ne "$SENDME" >&5 &
}

sockread ()
{
	LENGTH="$1"
	RETURN=`dd bs=$1 count=1 <&5 2> /dev/null`
}

echo "trying to open socket"
# try to connect
if ! exec 5<> /dev/udp/$HOST/$PORT; then
  echo "`basename $0`: unable to connect to $HOST:$PORT"
  exit 1
fi
echo "socket is open"

# send request
socksend "TEST"

# read 7 bytes for "success"
sockread 7
echo "RETURN: $RETURN"

The procedure is fairly easy:
– create a filedescriptor (using number 5 because 0,1,2 are for system stdin/stdout/stderr, so i am on the safe side) and link it to the special device /dev/udp (/dev/tcp for tcp connections).
– read/write to the fd

Attention: your bash must be compiled with this feature, otherwise there wont be any /dev/udp or /dev/tcp devices.

My post should answer the question posted here:

http://www.linuxquestions.org/questions/programming-9/writing-to-and-reading-from-a-socket-from-bash-script-794812/

Also, i stumbled across those pages which where quite helpful for getting started:

http://blogmag.net/blog/read/49/Network_programing_with_bash

http://thesmithfam.org/blog/2006/05/23/bash-socket-programming-with-devtcp-2/