19 ms·
Testing if a port can be reached, using built-in tools other than ol' telnet
- Snawoot 3y agoNot sure if I'd like to send anything into tested port just to tell if it is open. I'd rather stick with telnet or whatever is available. Also worth mentioning that bash also has network capabilities, you can check port like this: : < /dev/tcp/google.com/80 && echo OK || echo ERROR
- edvinbesic 3y agoThis is very useful, will save me some hassle. Thanks!
- politelemon 3y agoThis is what I was trying to remember, I've seen this before but never understood it. What does the colon do?
- eMPee584 3y agoit makes the smiley face look very sad : < while giving an exit status of zero (in this case only if bash can resolve the input redirection by opening that tcp port)
- arp242 3y ago: is a special built-in that always succeeds and always returns an exit of 0. You typically use it when you need a command for syntax reasons, but don't actually need/want to run a command.
- matt-attack 3y agoExample?
- semi 3y agoit's equivalent to 'true'. It returns a successful value
- Eduard 3y ago> Not sure if I'd like to send anything into tested port just to tell if it is open. can you elaborate? From a stealth perspective, probing for a port will always reveal to the other side that they are being probed
- winrid 3y agoCould crash the server if it's badly written
- Snawoot 3y agoIt's not about stealth, it's about a bad habit of sending some bytes without thinking how it will be interpreted. Examples: - Syslog-ng server will interpret it as valid log messages - HP JetDirect on port 9100 will print HTTP request: https://twitter.com/AviKivity/status/1405147699557638145 https://twitter.com/AviKivity/status/1405147699557638145 - What if service is protected by fail2ban and accounting protocol errors? After all, sending anything was not my intention, so I won't send.
- void-star 3y agoYour concern about not wanting to send bytes is totally valid. As it happens (if memory serves) telnet can send some bytes on connection also, in attempting to negotiate terminal settings with the remote “telnet server”. That said the /dev/tcp trick is indeed great for bash though!
- keepamovin 3y agothat is so cool, my god! Bash is this awesome underrated thing (relative to how I perceive it being used). It's so cool that there's basically a virtual filesystem that maps the internet to your disk...haha. This even works on my Mac. But one issue I encountered was timeout. I just tried: : < /dev/tcp/google.com/8080 && echo OK || echo ERROR and it hung. So I asked ChatGPT and it suggested timeout 5 bash -c ': < /dev/tcp/google.com/8080' && echo OK || echo ERROR
- BenjiWiebe 3y agoThere isn't a virtual filesystem; bash just pretends there is. You can't use the /dev/tcp path in your own program as it doesn't exist.
- keepamovin 3y agoUnless...I program in bash! ^^ haha :) Yeah, I didn't know that. I definitely had a suspicion it was how you say, and "basically" was my fudge word, because I didn't know. I hadn't thought much about it, but I did not know for sure. Thanks for tellin me. I've thought about this idea: mapping the internet to the Unix filesystem, a la proc. I know TabFS, but I think the mapping could be better. I think it's an interesting problem to consider what a good mapping would be. But it's all tradeoffs I guess. I haven't thought that much about it. But it did seem interesting. Maybe not interesting enough to really commit to, because it seemed kinda like a big project. But definitely interesting to consider. What do you think? What kind of mapping would you do?
- indigodaddy 3y agoI’ve never used ‘curl host:port’ but use ‘curl -v telnet://host:port’ all the time on Linux boxes that don’t happen to have telnet installed. Perhaps he omits the telnet on the curl because I believe the windows curl doesn’t support it..
- CrimsonChapulin 3y agoMost developers will have git and git bash installed nowadays which support standard cURL (along with all the basic useful bash utilities (sed, find, grep, tr, sort, uniq, etc.) We’ve given up on writing anything windows specific at work as the format and options are pretty limited. The most we will do is call setx or similar if we need to setup a user environment variable in our scripts. It has saved so much of our sanity and time as bash scripts are so much easier to write and maintain.
- zee2345 3y agoWindows can install full Linux distro via WSL. You can have a nmap in two minutes! I like it much better than MacBook with its incompatible ARM cpu, and outdated commands from some ancient BSD!
- renewiltord 3y agoBig fan of using Orbstack for this. Full Linux VM with transparent access to Mac disk. I love it.
- bluedino 3y agoIt's been a year or two but last time I tried networking tools like nmap in WSL I would get all kinds of errors.
- zee2345 3y agoI was getting errors from ping. Update fixed that, my guess is WSL2 is much better
- medellin 3y agoYes ever since the M1 chip it’s just annoying to work on the M1. Day to day it doesn’t matter much but it wastes days of times the few times i ran into an issue. I wish companies would stop defaulting to macs for development
- zee2345 3y agoHe, I asked for Windows laptop. I am the only one in like 30 people. My laptop has 64GB RAM and 2TB ssd. And unlike Mac, it drives 4x4k displays via USB4 hub. Even with all those extras, it costs less than Macbook, and I have some hardware budget still available:) And it came with unlocked bios, no spy software... Windows are basically unsupported by our IT...
- bradknowles 3y agoDepends on what Mac you get, but 64GB of RAM and 2TB of SSD with support for four 4k displays is pretty easy with the current MacBook Pro. It might be more expensive than your garden variety Windows laptop, or maybe not if you find one of the great deals that are frequently offered by B&H Photo or Adorama. No spy software will be installed, and the latest version of macOS is making it harder and harder for people to create malware that can easily infect Macs.
- mkirchner 3y agoHa! This was probably the first serious problem I ever tackled with an open source contribution! The year was 2002, the 2.4 Linux kernel had just been released and I was making money on the side building monitoring software for a few thousand (mostly Solaris) hosts owned by a large German car manufacturer. Everything was built in parallel ksh code, “deployed” to Solaris 8 on Sun E10Ks, and mostly kicked off by cron. Keeping total script runtime down to avoid process buildup and delay was critical. The biggest offender: long timeouts for host/port combinations that would sporadically not be available. Eventually, I grabbed W. Richard Stevens’ UNIX network programming book and created tcping [0]. FreeBSD, NetBSD, a series of Linux distros picked it up at the time and it was steady decline from there… good times! [0]: https://github.com/mkirchner/tcping https://github.com/mkirchner/tcping edit: grammar
- macintux 3y agoRIP WRS. Such a fantastic book.
- deleted 3y ago[deleted]
- gwright 3y agoI worked for Rich when he was writing UNP. Seeing comments like this 30+ years later reminds me how fortunate I was to spend time with him early in my career.
- kennu 3y agoAwesome, I think I had the same book in 1990s. Things were so simple back then, when the default approach to anything was to start writing some C code with socket calls.
- AceyMan 3y agoThe syntax is different and I don't remember who compiles the binaries, but as a primarily Windows sysadmin, tcping is literally the first thing I put in my %PATH% on a new jumpbox; even ahead of dig, believe it or not. (After that it's Russinovich's sysinternala suite.)
- iramiller 3y ago`nc -Z x.x.x.x nnnn` works well enough for me.
- winrid 3y agoIf netcat is installed
- amelius 3y agoI sometimes wonder why we still use port numbers. Wouldn't it make much more sense to use strings to name ports?
- hollerith 3y ago/etc/services assigns a string to every port number. It's ancient, but still present on every Linux and MacOS system.
- amelius 3y agoYeah, but it's still port numbers under the hood. The big downside of this is that you can get clashes. For example, VNC uses 5901 and up on my system. What if I have some other service that uses ports in the same range? A more sane approach would be to call those ports e.g. vnc/work, vnc/meeting, etc. so there would be no conflicts and you would know what each port is used for. And it would work even if you don't have write-access to /etc/services.
- HL33tibCe7 3y agoThe reason this hasn't been done is that it would require changes to the headers of TCP and UDP, which would be a massive undertaking (for similar reasons to why IPv4 -> IPv6 is such a pain). Would both the source and destination ports both be strings? Normally, for a client->server packet, the source port is meaningless, so what string would you use? Or would your new protocol support both string ports and integer ports? Having ports be strings in the TCP header would make the header considerably longer - supposing your protocol had one fixed length 32 ASCII char string port, you'd be looking at something in the region of a 2x increase in header length.
- Wingy 3y agoCouldn't the port string be mapped to a temporary port number when the TCP connection is initiated? It'd still be necessary for UDP though. It'd still be a massive undertaking though.
- whirlwin 3y agoI use curl inside a lot of Linux containers for this matter because curl is more frequently installed, and you cannot easily install new things unless you're root, which is not possible in many cases.
- yagizdegirmenci 3y agoI built a tool for this purpose, inspired by a comment on lobste.rs[0]. You can check a port like this, saves a few round trips on a good day. $ dstp google.com --port 80 Ping: 27.44ms DNS: resolving 216.58.212.14 SystemDNS: resolving 2a00:1450:4017:804::200e, 142.250.187.174 TLS: certificate is valid for 64 more days HTTPS: got 200 OK [0]: https://github.com/ycd/dstp#motivation https://github.com/ycd/dstp#motivation
- nonrandomstring 3y agohping3 is a much under-rated utility for all such needs ;)
- geraldcombs 3y agoThe traceroute command on modern Linux and macOS supports TCP, which I've found to be quite useful since you can see where in the path a connection is failing.
- Dylan16807 3y ago> Another beneficial difference of this over the old telnet approach is that with that, a successful connection would show a blank screen if the connection, awaiting commands. You'd have to know telnet keystrokes and commands to break out of that Not when it says "Escape Character is 'CTRL+]'" every time you run it. Or does that depend on how you run it with the windows version?
- pfyra 3y agoI've tried many times but never succeeded breaking out of a telnet session using that key combo. Perhaps my swedish keyboard layout is to blame, but that combo is simply not working. I've similar issues with the espressif idf.py tool too...
- aaron695 3y ago[dead]
- keepamovin 3y agoKind of unrelated but here's a little bit of bash I use for checking this locally: # Check if the port is available is_port_free() { if [[ "$1" =~ ^[0-9]+$ ]] && [ "$1" -ge 1024 ] && [ "$1" -le 65535 ]; then echo "Valid port number." >&2 else echo "Invalid port number." >&2 return 1 fi if netstat -lnt | awk '$6 == "LISTEN" && $4 ~ ":'$1'$" {exit 1}'; then # Return a truthy value if the port is available return 0 else # Return a falsey value if the port is in use return 1 fi }
- another2another 3y agonetstat seems to be disappearing from some linux distros - pretty sure it's not on my current Ubuntu by default, so I have to use 'ss' instead (which does seem to be quicker).
- keepamovin 3y agoCool, thanks for the tip! Yeah, I noticed I had to install netstat some times. Maybe I'll update my scripts to use ss instead. Do you have the conversion? I guess I could ask ChatGPT, but...while you're here? :) heh
- jdblair 3y agoI’m surprised there is no mention of netcat (nc).
- fotta 3y agoThere is near the bottom
- jdblair 3y agoOops, so there is!
- kitd 3y agoYes nc -vz host port is my go-to.
- jdblair 3y agoand, the -u option lets you send and receive UDP!
- bradknowles 3y agoNetcat got replaced by socat, right?
- jdblair 3y agoI use socat for serial-line things (like simulating a serial port for unit testing). I didn't occur to me that it can be a netcat replacement.
- est 3y agoIIRC there's also `ftp` command shipped with every Windows.
- ossusermivami 3y agoI use something like this using python, since python in its simple form is available on most distro/container by default and should be robust enough: python3 -c "import socket, sys; host, port = sys.argv[1], 80; s = socket.socket(socket.AF_INET, socket.SOCK_STREAM); s.settimeout(10); result = s.connect((host, port)); s.close(); exit(result == 0)" unknownlxlaxkck.com 2>/dev/null && echo connected || echo fail
- vdm 3y agonetcat, (v)erbose, (z)ero payload with 1 second timeout: $ nc -vzw1 host port
- oddly 3y agoI worked at a very locked down environment without possibility of installing other tools and I found that the openssl command was always available. $ openssl s_client -connect <IP>:<PORT>