Because of [Archive] PragmaticProgrammers on Twitter: “Helpful Unix trick: use script
to log your session. …” / Twitter:
- Linux:
- Windows
–jeroen
Posted by jpluimers on 2023/01/26
Because of [Archive] PragmaticProgrammers on Twitter: “Helpful Unix trick: use script
to log your session. …” / Twitter:
–jeroen
Posted in *nix, *nix-tools, ash/dash, bash, bash, Batch-Files, Development, Power User, Scripting, Software Development | Leave a Comment »
Posted by jpluimers on 2022/12/28
Years ago, I wrote Getting your public IP address from the command-line. All methods were http
based, so were very easy to execute using cURL
.
But then in autumn 2021, Chris Bensen wrote this cool little blog-post [Wayback/Archive] Chris Bensen: How do I find my router’s public IP Address from the command line?:
dig -4 TXT +short o-o.myaddr.l.google.com @ns1.google.com
At first sight, I thought it was uncool, as the command was quite long and there was no explanation of the dig
command trick.
But then, knowing that dig
is a DNS
client, it occurred to me: this perfectly works when http
and https
are disabled by your firewall, but the DNS
protocol works and gives the correct result:
# dig -4 TXT +short o-o.myaddr.l.google.com @ns1.google.com "80.100.143.119"
This added the below commands and aliases to my tool chest for *nix based environments like Linux and MacOS (not sure yet about Windows yet :), but that still doesn’t explain why it worked. So I did some digging…
dig -4 TXT +short o-o.myaddr.l.google.com @ns1.google.com
dig -4 TXT +short o-o.myaddr.l.google.com @ns1.google.com | xargs
alias "whatismyipv4_dns=dig -4 TXT +short o-o.myaddr.l.google.com @ns1.google.com | xargs"
dig -6 TXT +short o-o.myaddr.l.google.com @ns1.google.com
dig -6 TXT +short o-o.myaddr.l.google.com @ns1.google.com | xargs
alias "whatismyipv6_dns=dig -6 TXT +short o-o.myaddr.l.google.com @ns1.google.com | xargs"
Let’s stick to dig and IPv4 as that not having IPv6 (regrettably still) is the most common situation today:
# dig -4 TXT +short o-o.myaddr.l.google.com @ns1.google.com "80.100.143.119"
What it does is request the DNS
TXT
record of o-o.myaddr.l.google.com
from the Google DNS server ns1.google.com
and returns the WAN IPv4 address used in the DNS request, which is for instance explained in [Wayback/Archive] What is the mechanics behind “dig TXT o-o.myaddr.l.google.com @ns1.google.com” : linuxadmin.
Since these are TXT records, dig will automatically double quote them, which xargs
can remove (see below how and why):
# dig -4 TXT +short o-o.myaddr.l.google.com @ns1.google.com | xargs 80.100.143.119
The DNS query will fail when requesting the Google Public DNS servers 8.8.8.8 or 8.8.4.4:
# dig -4 TXT +short o-o.myaddr.l.google.com @8.8.8.8 "2a00:1450:4013:c1a::103" "edns0-client-subnet 80.101.239.0/24"
Or, with quotes removed (the -L 1
ensures that xargs
performs the quote-pair removal action on each line):
# dig -4 TXT +short o-o.myaddr.l.google.com @8.8.8.8 | xargs -L 1 2a00:1450:4013:c1a::103 edns0-client-subnet 80.101.239.0/24
This request is both slower than requesting the ns1.google.com
server and wrong.
The reason is that only ns1.google.com
understands the special o-o.myaddr.l.google.com
hostname which instructs it to return the IP address of the requesting dig DNS client.
That 8.8.8.8
returns a different IP address and an additional edns0-client-subnet
with less accurate information is explained in an answer to [Wayback/Archive] linux – Getting the WAN IP: difference between HTTP and DNS – Stack Overflow by [Wayback/Archive] argaz referring to this cool post: [Wayback/Archive] Which CDNs support edns-client-subnet? – CDN Planet.
ns1.google.com
: any DNS server serving the google.com domainSince o-o.myaddr.l.google.com
is part of the google.com
domain, the above works for any DNS server serving the google.com
domain (more on that domain: [Wayback/Archive] General DNS overview | Google Cloud).
Getting the list of DNS servers is similar to getting the list of MX servers which I explained in Getting the IP addresses of gmail MX servers, replacing MX
record type (main exchange) with the NS
record type (name server) and the gmail.com
domain with the google.com
domain:
# dig @8.8.8.8 +short NS google.com ns3.google.com. ns1.google.com. ns2.google.com. ns4.google.com.
The ns1.google.com
DNS server is a special one of the NS servers: it is the start of authority server, which you can query using the SOA
record type that also gives slightly more details for this server:
# dig @8.8.8.8 +short SOA google.com ns1.google.com. dns-admin.google.com. 410477869 900 900 1800 60
The difference between using NS
and SOA
records with dig
are explained in the [Wayback] dns – How do I find the authoritative name-server for a domain name? – Stack Overflow answer by [Wayback/Archive] bortzmeyer who also explains how to help figuring out SOA
and NS
discrepancies (note to self: check out the check_soa
tool originally by Michael Fuhr (I could not find recent content of him, so he might have passed away) of which source code is now at [Wayback/Archive] Net-DNS/check_soa at master · NLnetLabs/Net-DNS).
So this works splendid as well using ns4.google.com
on my test system:
# dig -4 TXT +short o-o.myaddr.l.google.com @ns4.google.com | xargs 80.100.143.119
xargs
removes outer quotes removal trick[Wayback/Archive] string – Shell script – remove first and last quote (“) from a variable – Stack Overflow (thanks quite anonymous [Wayback/Archive] user1587520):
> echo '"quoted"' | xargs quoted
xargs
usesecho
as the default command if no command is provided and strips quotes from the input.
Some notes are in [Wayback/Archive] How to get public IP address from Linux shell, but note the telnet trick now fails as myip.gelma.net is gone (latest live version was archived in the Wayback Machine in august 2019).
whatismyipv4='curl ipv4.whatismyip.akamai.com && echo'
It’s a quite a bit shorter than the dig construct in your post (;”–jeroen
Posted in *nix, *nix-tools, Apple, bash, bash, Batch-Files, Communications Development, Development, DNS, Internet protocol suite, Linux, Mac, Mac OS X / OS X / MacOS, Power User, Scripting, Software Development, TCP | Leave a Comment »
Posted by jpluimers on 2022/06/30
Even with a batch file saved as UTF-8 (with or without BOM), by default it does not show most non-ASCII Unicode characters.
The reason is that the default codepage usually is an ANSI one like codepage 437.
Thanks [Wayback] niutech for answering [Wayback/Archive.is] Unicode symbols in a batch file – Stack Overflow:
You can manually set the codepage to UTF-8 by typing
chcp 65001
at the top of your batch file.
Codepage 65001 is Windows speak for the UTF-8 code page. I have some more blog entries mentioning codepage 65001.
An example where I needed this was to show how to address the localghost from a batch file (see The spookback localghost address to resolve 👻). This was the resulting UTF-8 saved batch file:
chcp 65001 ping 👻 ping xn--9q8h
For single-byte non-ASCII characters, you can usually get away with setting the encoding of your batch file to your default code page as mentioned in [Wayback/Archive.is] cmd – Using box-drawing Unicode characters in batch files – Stack Overflow.
–jeroen
Posted in Batch-Files, Development, Encoding, Scripting, Software Development, Unicode, UTF-8, Windows Development | Leave a Comment »
Posted by jpluimers on 2022/06/16
Adapted from [Archive.is] How can you export the Visual Studio Code extension list? – Stack Overflow, presuming that code
is on the PATH
:
git
installed:code --list-extensions | xargs -L 1 echo code --install-extension
git
installed:code --list-extensions | % { "code --install-extension $_" }
or, as I think, more clearly (see also [WayBack] syntax – What does “%” (percent) do in PowerShell? – Stack Overflow):
code --list-extensions | foreach { "code --install-extension $_" }
or even more explanatory:
code --list-extensions | ForEach-Object { "code --install-extension $_" }
cmd.exe
command:@for /f %l in ('code --list-extensions') do @echo code --install-extension %l
cmd.exe
batch file (in a .bat
/.cmd
script):@for /f %%l in ('code --list-extensions') do @echo code --install-extension %%l
PowerShell -Command "code --list-extensions | % { """""code --install-extension $_""""" }"
Note that here too, the %
can be expanded into foreach
or ForEach-Object
for clarity.
All of the above prepend “code --install-extension
” (note the trailing space) before each installed Visual Studio Code extension.
They all give you a list like this which you can execute on any machine having Visual Studio Code installed and its code
on the PATH
, and a working internet connection:
code --install-extension DavidAnson.vscode-markdownlint code --install-extension ms-vscode.powershell code --install-extension yzhang.markdown-all-in-onex
(This is about the minimum install for me to edit markdown documents and do useful things with PowerShell).
Of course you can pipe these to a text-file script to execute them later on.
The double-quote escaping is based on [Wayback/Archive.is] How to escape PowerShell double quotes from a .bat file – Stack Overflow:
you need to escape the
"
on the command line, inside a double quoted string. From my testing, the only thing that seems to work is quadruple double quotes""""
inside the quoted parameter:
powershell.exe -command "echo '""""X""""'"
Via: [Archive.is] how to save your visual studio code extension list – Google Search
–jeroen
Posted in *nix, *nix-tools, .NET, bash, Batch-Files, CommandLine, Console (command prompt window), Development, Mac OS X / OS X / MacOS, Power User, PowerShell, PowerShell, Software Development, Visual Studio and tools, vscode Visual Studio Code, Windows, Windows 10, Windows 7, Windows 8, Windows 8.1, Windows Development, Windows Server 2008, Windows Server 2008 R2, Windows Server 2012, Windows Server 2012 R2, Windows Server 2016, WSL Windows Subsystem for Linux, xargs | Leave a Comment »
Posted by jpluimers on 2022/03/24
Last week finally there was the stable [Wayback/Archive] Release version 1.0.0 · chocolatey/choco · GitHub.
So I fixed the Wikipedia page
It was a few days after the 11th birthday “Celebration”: [Wayback/Archive] Chocolatey Software Blog | This One Goes To 11! Celebrating 11 Years Of Chocolatey. Not a really festive post, though it does have a really nice overview of 11 years of Chocolatey history and clearly showing the momentum of it has been a few years behind us.
The thing is: hardly anybody noticed the celebration nor the 1.0.0 release. Being at various 0.* versions for like a decade makes people not follow sudden version bumps closely. I only noticed when updating a bunch of testing VMs of which one had a problem, so I inspected the logs and saw the 1.0.0 version.
So these recent tweets did not gain much attention:
Anyway: the release notes indicate a few things scheduled for 2.0.0. Given the sudden 0.12.0 -> 1.0.0 bump, I have no clue far (or near!) in the future that will be.
It is kind of both a saddening and relieved feeling: like for instance Stack Overflow/Stack Exchange (both in the same age cohort as Chocolatey), Chocolatey is just there and mostly works.
–jeroen
Posted in .NET, Batch-Files, C#, Chocolatey, CommandLine, Development, Power User, PowerShell, PowerShell, Scripting, Software Development, Windows | Leave a Comment »