Hacker News new | ask | show | jobs
by Calzifer 1312 days ago
Bash / Shell is one of the last things I would trust a typical AI considering how much bad shell code/examples exist out there. (it always surprise me how many unquoted variables exist in most shell scripts; quoting fixes many problems, doesn't hurt and still developers are often to lazy to do it)

The example in the project readme is imo already a bad example.

  $ ai ask "Check process running on port"
Answer

  Command is `lsof -i tcp:8000`
Apart from the facts that

- I would say the chances that lsof is installed are significant smaller than ss or netstat

- is lsof notably slower than ss

it is also inferior for the requested task because

- it returns any process connecting to this port not only the listening one. Which process is running on port 443 on my desktop? None. How many processes does 'lsof -i tcp:443` return? 20.

- it does not work if the listening process is from another user (if lsof is not run by root). In this case lsof -i returns nothing while ss -nltp returns the information that the port is listened and only cannot tell by which process because of missing permissions.

My answer to "which process uses port x"

  ss -nltp sport = x
11 comments

I sort of agree wrt quoting. Most people's solution is just "well don't use spaces or dollars or whatever in your filenames!" which is obviously shit.

But on the other hand I think the ultimate blame lies with Bash for being so shitily designed that you can make basic quoting mistakes and it works most of the time. Real programming languages don't have that issue.

Yes, the convention is shit.

Alas, the things that force it are also shit. Overall, it is an improvement.

Want real fun. touch -- --help # or similar. :)

It's not a real programming language tho
Yeah! REAL programmers program only in the REAL programming languages like ${MY_FAVORITE_LANG}. Bash? More like - Bah!.
It's not a real programming language in the same way Brainfuck isn't a real programming language, because it makes life too hard.
So does Java, yet it is still considered a real programming language...
I always have to share this video on threads like these: https://youtu.be/kdMG40wUCm4. Maybe you’ve seen it - interview with a senior Java developer in 2022.
true, but nothing (besides how entrenched sh is) prevents replacing it with a real language. you don't need to use a posix shell in your terminal, any REPL that can launch executables will do. i used tclsh as my shell for a while, for instance.
So why do people write real production code in it?
Another good example is asking: `ai ask "how to delete a file safely in linux"`

> Command is `shred -u /path/to/file`

A bit of a mix up between safely/securely

Safe is an ambiguous word!
Perhaps the next most important feature of AI will be the ability to ask follow up / clarifying questions.
Not even "NI" (natural intelligence) is able to do that reliably.

People and AI seem to prefer assuming things.

Most of the time wrong things…

The other alternative is the top result for the Google search of that question.

It's not clear to me that this tool is a win over that method.

I've got a shell alias that does a web search with lynx, so I can just type: "? which process uses port x" And quickly find a result, that provably has a better explanation attaches to it than when asking GPT3.
Geez you use this tool for quick answers, it's not a manual specification. Do you also hate the CPU cache because it doesn't always point to correct memory?
lsof is available on Mac's by default, ss is not, so significantly smaller depends on your platform I guess. I believe ss is not available for Mac at all, where as lsof is widely available.
Well that is one of the problems when suggesting shell snippets. There are many similar ways to do the same thing¹ but most have some caveats or work on one system but not the other. And when asking the AI there might be not enough context to provide a good answer.

In this case the AI answer might be somewhat better for Mac. On Linux I have more often manually installed lsof than ss (iproute2). Maybe Mac provides the similar command netstat which is yet another tool to check ports

  netstat -nltp | grep -P ':8000\b'
Regarding available tools. Another very common Bash question is how to get the directory the executed script is located. A common answer involves `readlink -f` which (as far as I know) does not work on Mac (without extra steps).

¹ How to get current users name. Maybe whoami or id -un or logname or echo $USER or who -m or who am i (learnt this last one just today; apparently works with any combination of two arguments after 'who')

No -p flag for netstat on Mac and -l has different purpose. Closest you can get is

  netstat -anv
And then you need to filter for LISTEN.

I'm sure there is a few ways to skin the cat, but on Mac the most equivalent to the ss command would probably be:

  sudo lsof -iTCP:8000 -sTCP:LISTEN -n -P
Doesn’t seem like a huge stretch for the AI to check uname and see what system you’re on in order to clarify the answers.
Does it matter if lsof is slower than ss? Somebody asking this to the AI is going to run it once, maybe twice. What's an extra second?

The answer doesn't have to be perfect, it just as to be good enough.

I could see this tool being incredibly handy for working with tar

> Does it matter if lsof is slower than ss?

Not really. I just mentioned it because it was notably slower than ss. I did not actually measured it, just saw it when trying the example.

From my 4 points I wouldn't hold the first two alone against an AI driven suggestion. Only the last two, while not horrible bad or severe or anything, are (imo) notable downsides compared to the typical ss/netstat approach.

Why do you think the spit out results won't end up in shell scripts?

I've heard people are even copy-pasting stuff from Stackoverflow into production code without fully understanding the code they're copying.

You can ask "check process running on port using ss". Also if tool is not installed, can ask how to install it or say "without using lsof"
> Also if tool is not installed, can ask how to install it

This is actually a question I'm now really curious what the AI would answer since there are so many different correct answers (apt? yum? zypper? pacman? xbps?) and no indication for the AI which is the correct one for the asking user.

PS: But thinking about it I might just ask the wrong questions. "How to install X on <distro>" should work.

For the mere mortal who just needs these commands every once in a while, it’s a huge win though.
You can ask GPT for variations. Also, combining this with thefuck might circumvent most issues.
the example question is also part of the prompt

> 'Correctly answer the asked question. Return \'Sorry, Can\'t answer that.\' if the question isn\'t related to technology.\n\nQ - get into a docker container.\nA - `docker exec -it mongodb`\n\nQ - Check what\'s listening on a port.\nA - `lsof -i tcp:4000`\n\nQ - How to ssh into a server with a specific file.\nA - `ssh -i ~/.ssh/id_rsa user@127.0.0.1`\n\nQ - How to set relative line numbers in vim.\nA - `:set relativenumber`\n\nQ - How to create alias?\nA - `alias my_command="my_real_command"`\n\nQ - Tail docker logs.\nA - `docker logs -f mongodb`\n\nQ - Forward port in kubectl.\nA - `kubectl port-forward <pod_name> 8080:3000`\n\nQ - Check if a port is accessible.\nA - `nc -vz host port`\n\nQ - Reverse SSH Tunnel Syntax.\nA - `ssh -R <remote_port>:<local_host>:<local_port> <user>@<remote_host>`\n\nQ - Kill a process running on port 3000.\nA - `lsof -ti tcp:3000 | xargs kill`\n\nQ - Backup database from a mongodb container.\nA - `docker exec -it mongodb bash -c "mongoexport --db mongodb --collection collections --outdir backup"`\n\nQ - SSH Tunnel Remote Host port into a local port.\nA - `ssh -L <local_port>:<remote_host>:<remote_port> <user>@<remote_host>`\n\nQ - Copy local file to S3.\nA - `aws s3 cp <local_file> s3://<bucket_name>/<remote_file>`\n\nQ - Copy S3 file to local.\nA - `aws s3 cp s3://<bucket_name>/<remote_file> <local_file>`\n\nQ - Recursively remove a folder.\nA - `rm -rf <folder_name>`\n\nQ - Copy a file from local to ssh server.\nA - ` scp /path/to/file user@server:/path/to/destination`\n\nQ - Curl syntax with port.\nA - `curl http://localhost:3000`\n\nQ - Download a file from a URL with curl.\nA - `curl -o <file_name> <URL>`\n\nQ - Git commit with message.\nA - `git commit -m "my commit message"`\n\nQ - Give a user sudo permissions.\nA - `sudo usermod -aG sudo <user>`\n\nQ - Check what\'s running on a port?\nA - `lsof -i tcp:<port>`\n\nQ - View last 5 files from history\nA - `history | tail -5`\n\nQ - When was China founded?\nA - Sorry, Can\'t answer that.\n\nQ - Pass auth header with curl\nA - `curl -H "Authorization: Bearer <token>" <URL>`\n\nQ - Filter docker container with labels\nA - `docker ps --filter "label=<KEY>"`\n\nQ - When was Abraham Lincon born?\nA - Sorry, Can\'t answer that.\n\nQ - Get into a running kubernetes pod\nA - `kubectl exec -it <pod_name> bash`\n\nQ - Capital city of Ukrain?\nA - Sorry, Can\'t answer that.\n\nQ - ';

so you would expect it to answer it "correctly"

man you should train their AI haha