Something I learned recently and I thought was amazing – you can create sockets straight from your shell! Well, assuming you use bash or zsh – from some surface level digging, I couldnt find anything for fish.
Heres how it works:
Bash supports tcp and udp connections out of the box, and does so with an imaginary device in /dev. Enter
$ echo”text!”> /dev/$PROTO/$HOST/$PORT
And youll create a connection to HOST:PORT. $PROTO can be tcp or udp. If the connection cant be made, writing to/reading the file will fail.
Along with being easy to access from the terminal, its very handy for scripts, especially if you dont have nc/telnet. For example, if a local build of a web app runs on port 8000, you can check if its running with:
#!/bin/bashif[exec 1>/dev/null 2>/dev/null 3>/dev/tcp/localhost/4000 ];then
echo”server up!”else
echo”server down.”fi
And then use that information somewhere else.
If youre unfamiliar, exec is used here to create the file to write to (>), with file descriptor 3 referring to it and thus the connection.
We can use exec to create a connection we can read and write to with <>, and create a super simple curl:
#!/bin/bashexec 3<>/dev/tcp/”$1″/80
echo-e”GET / HTTP/1.1\n”>&3
cat <&3
$ ./simplecurl
HTTP/1.1 200 OK
Date: Thu, 03 Dec 2020 00:57:30 GMT
Expires: -1
<google website>
Im sure you can see the power of being able to open sockets with bash alone. Go play around with it!
zsh has an external module you can load in order to use its socket capabilities. It doesnt support udp like bash, but its more powerful in a few ways!
To load the module, put the following in your .zshrc or run it in your shell:
We now have access to the zsh networking builtin – ztcp!
ztcp allows creating connections, like bash, but also allows listening for connections.
Straight from the zsh docs, we can create a connection between two machines with ztcp:
# host machine:
ztcp -l 7128
ztcp -a$lfdtalkfd=$REPLY# client machine
ztcp HOST 7128
The $REPLY variable here is a file descriptor returned by the last ztcp command, referring to the socket/connection it just created.
So, talkfd on both machines is a file descriptor for talking to the other:
# host machineecho-e”hello!”>&$talkfd# client machineread-r line <&$talkfd; print -r – $line> hello!
Again, theres a lot more you can do, especially with the ability to listen for connections.
Hope this was as interesting to you as it was to me!