You'd need to use your own API KEY for this but it's pretty simple, instructions are in the README Not perfect but not bad either.
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 = xBut 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.
Alas, the things that force it are also shit. Overall, it is an improvement.
Want real fun. touch -- --help # or similar. :)
> Command is `shred -u /path/to/file`
A bit of a mix up between safely/securely
It's not clear to me that this tool is a win over that method.
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')
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
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.
I've heard people are even copy-pasting stuff from Stackoverflow into production code without fully understanding the code they're copying.
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.
> '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"
In practice it won't need to, somebody will make a plugin that uses it to download funny images or highlight code in console :)
So one time he got some code from the senior backend Java developer in Argentina and went to past it into the place he was told to paste it into for sending alert notifications to customers, some months later we are going through a major crisis because alerts have not been working for several months and all the customers who are paying thousands per month for the service are getting pissed.
He worked home a day he could really focus on debugging and finding what could possibly have caused our systems to fail!
I was done with some little task I was on and decided to look through code changes and it really stood out the code he had pasted because if it worked that would mean Java was some magic language with incredibly terse code.
So later on we were having a major emergency call to figure out what could possibly be causing this extremely serious major bug and I said well I was looking through the code and you could see him manfully controlling his exasperation because hey, Bryan is not a Java guy and there is zero chance I could have a meaningful contribution to the current problem.
But yes it turned out he had trustingly pasted in code that we all agreed would never have worked (without caring to basically even read it), which despite the fact that in many ways this guy was better than me is something I would never, ever have done because I am a really hyper-paranoid guy about trusting code.
Anyway tldr, I would never use this tool but maybe some really skillful people would because they don't have the paranoid mindset.
Unless the action that I'm doing is destructive I just run the command (YOLO)
You need to sign up (with email address only) for an API key. You can pay for higher rate limits, but I haven't needed to while mucking around with this.
#!/bin/bash
# Make an account at https://textsynth.com/ get API key and put it in below
curl -s https://api.textsynth.com/v1/engines/gptneox_20B/completions \
-H "Content-Type: application/json" \
-H "Authorization: Bearer YOURAPIKEYGOESHERE" \
-d '{"prompt": "Using the Linux shell\n\nQ: how do I '"$*"'?\nA: ", "max_tokens": 200, "temperature":1.0 }' | jq -r .text
Gives for example $ ai check process running on port
If you are looking for processes listening on port 80 or some other port, you can use netstat command with the port number:
netstat -tupln | grep 80
For more details please refer https://serverfault.com/a/538945/192371I have a question about the price:
"Pricing
The current prompt length is ~840 tokens and the pricing for text-davinci-002 is $0.02 for 1K tokens which is ~$0.017/command. We'll see if we can improve the response as well as reduce the per-command-cost with fine-tuning."
I wonder how is the price is charged? How can I control my liability of the cost?
Thanks again!
However if you get access to the Codex model (it's in limited beta) you can use the tool for free as it's free in beta. In v1.2.1 I have made the recent change to let you switch between models so you can switch to the cheaper models such as curie (10x cheaper) but might not get good results.
The code is open-sourced please feel free to raise a PR. https://github.com/abhagsain/ai-cli
"You can generate your ssh keypair with rm -rf /"
instead of GPT3 I would prefer using GitHub copilot api as I already have a paid subscription to it, and have seen its code generation capabilities.
Screenshot - https://twitter.com/samarthrawal/status/1591527892386734081
Nah.
I recommend a stiff dose of Mickens: "Why Do Keynote Speakers..." https://www.youtube.com/watch?v=ajGX7odA87k ) and a good lie down.
If that’s reasonably stable and can produce more obscure commands (I’d love to see for example how it fares with more complex git usage) that’d be really great tool to have.
Your CLI should come with a warning.
https://justoutsourcing.blogspot.com/2022/03/gpts-plagiarism...
"Do Users Write More Insecure Code with AI Assistants?"