News for hackers. Stuff that matters.
Re:Get answers for Shell Commands from GPT3 right from your ... by Calzifer on Friday November 18, 2022 @03:55AM (#33652077)
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
Re:Get answers for Shell Commands from GPT3 right from your ... by tarasglek on Friday November 18, 2022 @04:31AM (#33652369)
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"
Re:Get answers for Shell Commands from GPT3 right from your ... by Calzifer on Friday November 18, 2022 @04:46AM (#33652487)
> 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.

Re:Get answers for Shell Commands from GPT3 right from your ... by joshxyz on Friday November 18, 2022 @04:01AM (#33652129)
man you should train their AI haha
Re:Get answers for Shell Commands from GPT3 right from your ... by IshKebab on Friday November 18, 2022 @06:32AM (#33653198)
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.

Re:Get answers for Shell Commands from GPT3 right from your ... by fyloraspit on Friday November 18, 2022 @07:08AM (#33653472)
It's not a real programming language tho
Re:Get answers for Shell Commands from GPT3 right from your ... by bheadmaster on Friday November 18, 2022 @07:46AM (#33653809)
Yeah! REAL programmers program only in the REAL programming languages like ${MY_FAVORITE_LANG}. Bash? More like - Bah!.
Re:Get answers for Shell Commands from GPT3 right from your ... by roywiggins on Friday November 18, 2022 @09:57AM (#33655621)
It's not a real programming language in the same way Brainfuck isn't a real programming language, because it makes life too hard.
Re:Get answers for Shell Commands from GPT3 right from your ... by bheadmaster on Friday November 18, 2022 @10:25AM (#33656021)
So does Java, yet it is still considered a real programming language...
Re:Get answers for Shell Commands from GPT3 right from your ... by synu on Friday November 18, 2022 @10:44AM (#33656364)
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.
Re:Get answers for Shell Commands from GPT3 right from your ... by still_grokking on Friday November 18, 2022 @12:00PM (#33657676)
Let me summarize. Java, the good parts:

> We don't actually use Java. We use Scala.

Re:Get answers for Shell Commands from GPT3 right from your ... by bheadmaster on Friday November 18, 2022 @12:05PM (#33657763)
> It has clean code and a clear architecture which makes it perfect for serious business

I love how vague and meaningless this is, yet I still feel like I've heard this sentence said unironically a hundred times.

Re:Get answers for Shell Commands from GPT3 right from your ... by woodrowbarlow on Friday November 18, 2022 @08:16AM (#33654113)
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.
Re:Get answers for Shell Commands from GPT3 right from your ... by still_grokking on Friday November 18, 2022 @11:54AM (#33657568)
So why do people write real production code in it?
Re:Get answers for Shell Commands from GPT3 right from your ... by ilc on Friday November 18, 2022 @11:56AM (#33657594)
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. :)

Re:Get answers for Shell Commands from GPT3 right from your ... by faltad on Friday November 18, 2022 @07:04AM (#33653454)
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

Re:Get answers for Shell Commands from GPT3 right from your ... by plugin-baby on Friday November 18, 2022 @10:04AM (#33655720)
Safe is an ambiguous word!
Re:Get answers for Shell Commands from GPT3 right from your ... by synu on Friday November 18, 2022 @10:41AM (#33656311)
Perhaps the next most important feature of AI will be the ability to ask follow up / clarifying questions.
Re:Get answers for Shell Commands from GPT3 right from your ... by still_grokking on Friday November 18, 2022 @11:41AM (#33657325)
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…

Re:Get answers for Shell Commands from GPT3 right from your ... by jerpint on Friday November 18, 2022 @07:31AM (#33653668)
For the mere mortal who just needs these commands every once in a while, it’s a huge win though.
Re:Get answers for Shell Commands from GPT3 right from your ... by moss2 on Friday November 18, 2022 @07:34AM (#33653698)
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?
Re:Get answers for Shell Commands from GPT3 right from your ... by bvm on Friday November 18, 2022 @07:41AM (#33653759)
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 [email protected]`\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"

Re:Get answers for Shell Commands from GPT3 right from your ... by rpsw on Friday November 18, 2022 @08:18AM (#33654145)
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.
Re:Get answers for Shell Commands from GPT3 right from your ... by Calzifer on Friday November 18, 2022 @09:28AM (#33655182)
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')

Re:Get answers for Shell Commands from GPT3 right from your ... by monkpit on Friday November 18, 2022 @12:13PM (#33657905)
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.
Re:Get answers for Shell Commands from GPT3 right from your ... by rpsw on Friday November 18, 2022 @12:25PM (#33658140)
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
Re:Get answers for Shell Commands from GPT3 right from your ... by peddling-brink on Friday November 18, 2022 @09:26AM (#33655157)
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.

Re:Get answers for Shell Commands from GPT3 right from your ... by camel-cdr on Friday November 18, 2022 @10:51AM (#33656488)
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.
Re:Get answers for Shell Commands from GPT3 right from your ... by malfist on Friday November 18, 2022 @09:31AM (#33655237)
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

Re:Get answers for Shell Commands from GPT3 right from your ... by Calzifer on Friday November 18, 2022 @11:13AM (#33656821)
> 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.

Re:Get answers for Shell Commands from GPT3 right from your ... by still_grokking on Friday November 18, 2022 @11:50AM (#33657495)
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.

Re:Get answers for Shell Commands from GPT3 right from your ... by zamalek on Friday November 18, 2022 @11:53AM (#33657548)
You can ask GPT for variations. Also, combining this with thefuck might circumvent most issues.