2020/05/26

Linux shell to access tcp connection.

Open a new connection
1
exec FILE_DESCRIPTOR<>/dev/PROTOCOL/HOST/PORT
FILE_DESCRIPTOR: 0=stdin, 1=stdout, 2=stderr. So you can use numbers larger than 3.
PROTOCOL: tcp or udp
HOST: Host name or IP address.
PORT: Host listen port.


Send message
1
echo -e "MESSAGE" >&FILE_DESCRIPTOR
MESSAGE: The message which you want to send.
FILE_DESCRIPTOR: File descriptor when creating the connection..


Receive message
1
timeout SECOND cat <&FILE_DESCRIPTOR
SECOND: Wait SECOND seconds and then time out.
FILE_DESCRIPTOR: File descriptor when creating the connection..


Close connection
1
exec FILE_DESCRIPTOR<&-
FILE_DESCRIPTOR: File descriptor when creating the connection..


Example
1
2
3
4
5
6
7
8
9
exec 3<>/dev/tcp/192.168.5.250/1539
sleep 1
echo -e "test" >&3
timeout 5 cat <&3
sleep 1
echo -e "exit" >&3
timeout 5 cat <&3
sleep 1
exec 3<&-

沒有留言: