jq json parser hash the field value - parsing

I have a JSON file as the below:
[{
"macAddress": "ac:5f:3e:87:d7:1a",
"ip": "1.2.3.4"
},
{
"macAddress": "ac:5f:3e:87:d7:2a",
"ip": "1.2.3.4"
},
{
"macAddress": "ac:5f:3e:87:d7:3a",
"ip": "1.2.3.4"
}]
use jq to hash the macAddress field like so:
jq .[] | hash(.macAddress)
Can I define my own hash function and let jq to run the hash during the parsing process?
My expected hash function can be simple as using native linux command md5sum
echo -n "my_salt""42:12:20:2e:2b:ca" | md5sum
d973ea7c353e78ba1724efbc8054dfdc -
So the output json will be
[{
"macAddress": "d973ea7c353e78ba1724efbc8054dfdc",
"ip": "1.2.3.4"
},
{
"macAddress": "d973ea7c353e78ba1724efbc8054d2er",
"ip": "1.2.3.4"
},
{
"macAddress": "d973ea7c353e78ba2324efbc8054d123",
"ip": "1.2.3.4"
}]

My expected hash function can be simple as using native linux command md5sum
This stays native and might be suitable;
Invocation :
jq -c .[] "$jsonfile" |
while read -r jsonline ; do
hashmac="$(jq --arg mysalt "$mysalt" -s -j '
.[] | "\($mysalt)" + .macAddress' <<<"$jsonline" |
md5sum | cut -d ' ' -f1)"
jq --arg hashmac "$hashmac" -s -r '
.[] | .macAddress |= "\($hashmac)"' <<<"$jsonline"
done
Example file - /tmp/testfile:
[{
"macAddress": "ac:5f:3e:87:d7:1a",
"ip": "1.2.3.4"
},
{
"macAddress": "ac:5f:3e:87:d7:2a",
"ip": "1.2.3.4"
},
{
"macAddress": "ac:5f:3e:87:d7:3a",
"ip": "1.2.3.4"
},
{
"macAddress": "42:12:20:2e:2b:ca",
"ip": "1.2.3.4"
}]
Result Output:
{
"macAddress": "1f960fe4d24684ca44e5e67b6259362c",
"ip": "1.2.3.4"
}
{
"macAddress": "3527422754ecbfdd01d48b17fce87842",
"ip": "1.2.3.4"
}
{
"macAddress": "9bc8da72324448c3032a20fb67a31466",
"ip": "1.2.3.4"
}
{
"macAddress": "d973ea7c353e78ba1724efbc8054dfdc",
"ip": "1.2.3.4"
}
Comments:
-j causes jq to not output a newline, equivalent to your echo -n example
Variables in this example are sent to jq as strings using -arg, and referenced as "\($var)" as opposed to escaping the variable directly, for example:
"\($mysalt)" + .macAddress' (jq variable)
Instead of:
"'"$mysalt"'" + .macAddress' (direct shell substitution)
This example uses cut -d ' ' -f1 to trim off the -, but there's probably a better way
Alternate:
jq --arg hashmac "$hashmac" -s -r '.[] |= . + {"hashAddress":"\($hashmac)"}'
Would append the json
[
{
"macAddress": "ac:5f:3e:87:d7:1a",
"ip": "1.2.3.4",
"hashAddress": "1f960fe4d24684ca44e5e67b6259362c"
}
]
etc.

The accepted answer entails 2n + 1 invocations of jq, where n is the number invocations of the message digest function.
To reduce the number of invocations to just 2, see shell-out value to md5 (crypto) function

Related

Not able to access container by user-defined network name from another container

I created a new network.
Exposed port 8086 for one of my containers and also published this port. Run both containers with --network influx-net.
Check docker network inspect influx-net gives my
docker network inspect influx-net
[
{
"Name": "influx-net",
"Id": "76ad2f3ec76e15d88330739c2b3db866a15d5affb0912c64c7ec7615b14e8570",
"Created": "2021-11-09T17:32:42.479637982+09:00",
"Scope": "local",
"Driver": "bridge",
"EnableIPv6": false,
"IPAM": {
"Driver": "default",
"Options": {},
"Config": [
{
"Subnet": "172.18.0.0/16",
"Gateway": "172.18.0.1"
}
]
},
"Internal": false,
"Attachable": false,
"Ingress": false,
"ConfigFrom": {
"Network": ""
},
"ConfigOnly": false,
"Containers": {
"92c0f2c5dc5c75db15e5a5ea0bed6ec5047f0c57dba06283ef6e72c8adcc6a3a": {
"Name": "ultimate_hydroponics",
"EndpointID": "529c2c31baaec7685251a584e8f5643b4669966372b629d67f3a9910da5e809c",
"MacAddress": "02:42:ac:12:00:02",
"IPv4Address": "172.18.0.2/16",
"IPv6Address": ""
},
"a678409dbc43685fd089ed29f42601b67c46c20b66c277e461a8fe2adad31b5a": {
"Name": "influxdb",
"EndpointID": "73263b9969dc9c67760ec3aab4ebab75e16763c75cd498828a737d9e56ed34ef",
"MacAddress": "02:42:ac:12:00:03",
"IPv4Address": "172.18.0.3/16",
"IPv6Address": ""
}
},
"Options": {},
"Labels": {}
}
]
So my both containers are connected to the network influx-net.
But when I trying to ping or wget InfluxDB container (influx-net:8086) from another container by it's network name I'm obtaining nothing.
Also when I do the same with 192.168.111.11:8086 (my pc IP), I'm getting a response.
What is the problem?
Local PC firewall is off.
# create a network
$ docker network create influx-net
# start influx db attached to the network influx-net
$ docker run -d --name idb --net influx-net influxdb
Now, create a new container attached to same network and try connecting:
$ docker run -it --net influx-net ubuntu
root#fc26733c33da:/# telnet idb 8086
Trying 172.18.0.2...
Connected to idb.
Escape character is '^]'.
And it's working.
If you need to connect using IP, inspect the network to get container IP and then use the same to connect.
$ docker network inspect influx-net
[
{
"Name": "influx-net",
...
"Containers": {
"844311255fb9dd74fee1df2dc65023533ad961d1dd6345128cc2c92237ba35e0": {
"Name": "idb",
"EndpointID": "b294cb0661f9f1166833f381a02bcbfa531cdeba6657c51b9595814f5ee323be",
"MacAddress": "02:42:ac:12:00:02",
"IPv4Address": "172.18.0.2/16", 👈 # this one here
"IPv6Address": ""
}, ...
},
"Options": {},
"Labels": {}
}
]
$ docker run -it --net influx-net ubuntu
root#360bbca6534d:/# telnet 172.18.0.2 8086
Trying 172.18.0.2...
Connected to 172.18.0.2.
Escape character is '^]'.

Docker: cant access my local MongoDB container when running my application in a container

I have run a python application that uses a container MongoDB container as a database.
When running my controller application on local I have no issues to access the Mongo database.
This is my docker file for my application.
FROM python:alpine3.7
COPY . /app
WORKDIR /app
RUN pip install -r requirements.txt
EXPOSE 8000
CMD python ./controller.py
I run my Mongo database with the next command.
sudo docker run -p 27027:27017 -d --name my-local-db --network local_bridge mongodb
And my application
sudo docker run -p 8000:8000 -d --name scrapper-controller --network local_bridge scrapper-controller
I have inspected my local_bridge on docker and I can see that both containers are in the same network.
[
{
"Name": "local_bridge",
"Id": "fa2609918a702e51ecba93920434b992abf98249c90955d6ac700b59a5089166",
"Created": "2019-09-29T00:46:11.301246442+02:00",
"Scope": "local",
"Driver": "bridge",
"EnableIPv6": false,
"IPAM": {
"Driver": "default",
"Options": {},
"Config": [
{
"Subnet": "172.18.0.0/16",
"Gateway": "172.18.0.1"
}
]
},
"Internal": false,
"Attachable": false,
"Ingress": false,
"ConfigFrom": {
"Network": ""
},
"ConfigOnly": false,
"Containers": {
"bcf382d955e0492991e73a3659e729ea6ddd6b4514809f05bb11e43b92a9ecdb": {
"Name": "scrapper-controller",
"EndpointID": "f51340bf27e553b3936ff580ce396896f8d1c14e9d0b81ae3bc869be97106db6",
"MacAddress": "02:42:ac:12:00:02",
"IPv4Address": "172.18.0.2/16",
"IPv6Address": ""
},
"ceaea1d9f6317cd224c2fe96a0ce6fd6e6740661bba2f0bc014f5b55054eab99": {
"Name": "my-local-db",
"EndpointID": "95f3cd136d7810b8e168e0e01e0ab9d4667122f058d59ddd2319ada6888434cc",
"MacAddress": "02:42:ac:12:00:03",
"IPv4Address": "172.18.0.3/16",
"IPv6Address": ""
}
},
"Options": {},
"Labels": {}
}
]
Any idea what is failing here?

Docker Container not use host DNS

i'm used custom docker network named "backend-network"
[root#localhost docker]# docker inspect backend-network
{
"Name": "backend-network",
"Id": "18180c0c1ef14460a25b66b7fb971e090f7bb85f549921704d11937af70766c7",
"Created": "2018-08-07T12:36:02.4175991+09:00",
"Scope": "local",
"Driver": "bridge",
"EnableIPv6": false,
"IPAM": {
"Driver": "default",
"Options": {},
"Config": [
{
"Subnet": "172.18.0.0/16",
"Gateway": "172.18.0.1"
}
]
},
"Internal": false,
"Attachable": false,
"Containers": {
"531c1ecbe993ee13e632fbd9697b392ee989d756ff60c07eae96a700901aaa01": {
"Name": "splash",
"EndpointID": "c9e4e7ec319ecf9cdcbb9ca50170efb63c4fca33bcbbabb584c4a4e41576b15d",
"MacAddress": "02:42:ac:12:00:05",
"IPv4Address": "172.18.0.5/16",
"IPv6Address": ""
},
"c6a5aa827e901b6b6d7b35d4a8be5a5b2fc73f1a7a385416ce200e847d400b21": {
"Name": "flask",
"EndpointID": "5d5abb3bc964d251379a7f6a84cb5b5d9bddac9b778f2222d52aba657b28dd34",
"MacAddress": "02:42:ac:12:00:02",
"IPv4Address": "172.18.0.2/16",
"IPv6Address": ""
},
"da839143fb58d738e38922c669efa332c545fee4dd0a5b733583ed7b8df60875": {
"Name": "django",
"EndpointID": "f046e9cc93f895b12ce1c4de983fbe0e54a3904460c04db3ba238ba84ba82327",
"MacAddress": "02:42:ac:12:00:03",
"IPv4Address": "172.18.0.3/16",
"IPv6Address": ""
},
"fc9e6ef183c81a3fe7dd29ecb5c17c0dc27fb803ef8e214d4f344a2b3407ec54": {
"Name": "mongo",
"EndpointID": "ab94182f4b175f105ab01ccbbc43b7dad37cf5506eee831168fd5bd9094ccde8",
"MacAddress": "02:42:ac:12:00:04",
"IPv4Address": "172.18.0.4/16",
"IPv6Address": ""
}
},
"Options": {},
"Labels": {}
}
But each Container not used host DNS.
host DNS is.
[root#localhost docker]# cat /etc/resolv.conf
# Generated by NetworkManager
nameserver 192.168.88.1
and container DNS is
(django) root#da839143fb58:/opt/django_backend/scrapy_app# cat /etc/resolv.conf
nameserver 127.0.0.11
options ndots:0
i added
nameserver 192.168.88.1
container's resolv.conf, and it works but request too long.
I think first search in
nameserver 127.0.0.11
and search
nameserver 192.168.88.1
how can i set docker-network to host dns?
remove
nameserver 127.0.0.11
then don't communicate other container by name. like
(django) root#da839143fb58:/opt/django_backend/scrapy_app# ping splash
ping: splash: Name or service not known
Docker containers are resolving DNS requests through embedded DNS server (this is the IP you are seeing in the container's /etc/resolv.conf – see bottom note in documentation). Depending on your configuration the embedded DNS server forwards the query to your host (default) or another DNS-server. You can pass a custom DNS server with the --dns- flag.
Please find more information about that in the documentation.

Getting connection refused error from one docker container to another only for REST request

I've two docker containers apiserver and loginserver. Both of them are provide REST API and are built using spring boot. I've created a bridge network called my-network and both the containers are attached to the same bridge.
I pinged loginserver from apiserver via interactive shell and it is accessible. I make REST request from the host machine so I know the socket exposed. But, when I make the same REST request from apiserver to loginserver, I am getting error:
: HttpQueryService::uri=http://172.28.0.7:8090/users/login
2018-06-19 19:08:24.196 ERROR 7 --- [nio-9000-exec-3] o.a.c.c.C.[.[.[/].[dispatcherServlet] : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception
org.apache.http.conn.HttpHostConnectException: Connect to 172.28.0.7:8090 [/172.28.0.7] failed: Connection refused (Connection refused)
Here are the details from my-network:
docker network inspect my-network
[
{
"Name": "my-network",
"Id": "ef610688b58b6757cba57caf6261f7a1eaeb083798098214c4848cbb152cae26",
"Created": "2018-04-21T00:19:46.918124848Z",
"Scope": "local",
"Driver": "bridge",
"EnableIPv6": false,
"IPAM": {
"Driver": "default",
"Options": {},
"Config": [
{
"Subnet": "172.28.0.0/16",
"Gateway": "172.28.0.1"
}
]
},
"Internal": false,
"Attachable": true,
"Ingress": false,
"ConfigFrom": {
"Network": ""
},
"ConfigOnly": false,
"Containers": {
"71863d2f61789d4350fcabb1330b757500d5734a85c68b60eb1ac8f6f1e8344e": {
"Name": "mymongo",
"EndpointID": "717c8dbdc8993a70f9d3e97e549cb9784020b8e68e7a557c30b0818b4c9acb90",
"MacAddress": "02:42:ac:1c:00:02",
"IPv4Address": "172.28.0.2/16",
"IPv6Address": ""
},
"936447ce8325f3a7273a7fb462d75e55841a9ff37ccf27647831b3db1b8a1371": {
"Name": "mypg",
"EndpointID": "6a1a1b2f7852b89a9d2cb9b9abecdabd134849cd789c31613c7ddb91a4bc43d1",
"MacAddress": "02:42:ac:1c:00:06",
"IPv4Address": "172.28.0.6/16",
"IPv6Address": ""
},
"ad03348dffaef3edd916d349c88e8adf6cf7d88dbc40f82dc2384dee826cfa83": {
"Name": "myloginserver",
"EndpointID": "fe22c2b5f57b7fe4776087972ffa5f7f089ca6a59fde2fa677848b3f238ea026",
"MacAddress": "02:42:ac:1c:00:07",
"IPv4Address": "172.28.0.7/16",
"IPv6Address": ""
},
"c69bfbf9ccdc9e29e87d2847b5c2a51e9c232fb8d06635bcef0cdd1f7c66e051": {
"Name": "apiserver",
"EndpointID": "46e94a52d34670eb00448b1d39a0cc365b882ece790c9d868dcee04ad141d1ca",
"MacAddress": "02:42:ac:1c:00:0b",
"IPv4Address": "172.28.0.11/16",
"IPv6Address": ""
}
},
"Options": {},
"Labels": {}
}
]
Is port 8090 exposed by your loginserver image? For checking type in command
docker images
and then find the ImageID of your loginserver image. Then enter command
docker inspect image {Login server image id}
In the output check ExposedPorts if 8090 is exposed or not
Late to the party but I just fixed this on my system by setting the address to get the REST request from as the public IP address:
eg: http://217.114.203.196/myrequest

Internet unreachable on the container when a VPN is ON on the host

When I do
docker run -i -t --privileged busybox ping google.com
It works when the VPN is OFF on my host machine but when I start it, it fails.
I found my DNS Server address by using
sudo systemd-resolve --status | grep "DNS Server"
And I tried to use it in the docker container, it gives :
docker run -i -t --privileged --dns=192.168.1.254 busybox nslookup google.com
Server: 192.168.1.254
Address 1: 192.168.1.254
Name: google.com
Address 1: 2607:f8b0:4020:804::200e yul02s04-in-x0e.1e100.net
Address 2: 172.217.13.206 yul03s05-in-f14.1e100.net
So it can find the IP address of the domain but I'm still not able to ping the it.
When I use the --network host option, it works but I have trouble with other containers.
Here is my /etc/resolv.conf file when the VPN is OFF :
# Dynamic resolv.conf(5) file for glibc resolver(3) generated by resolvconf(8)
# DO NOT EDIT THIS FILE BY HAND -- YOUR CHANGES WILL BE OVERWRITTEN
# 127.0.0.53 is the systemd-resolved stub resolver.
# run "systemd-resolve --status" to see details about the actual nameservers.
nameserver 8.8.8.8
nameserver 127.0.0.53
search telus
And when it's ON :
nameserver 8.8.8.8
nameserver 8.8.4.4
# Dynamic resolv.conf(5) file for glibc resolver(3) generated by resolvconf(8)
# DO NOT EDIT THIS FILE BY HAND -- YOUR CHANGES WILL BE OVERWRITTEN
# 127.0.0.53 is the systemd-resolved stub resolver.
# run "systemd-resolve --status" to see details about the actual nameservers.
search telus
And here is information about the network used by the container :
[
{
"Name": "project_default",
"Id": "e5b5cdaf12ea277f28b5e5a050041a55fe33d279bcd6b2c737a3a6cdfb039ea2",
"Created": "2017-10-05T21:24:16.200249606+02:00",
"Scope": "local",
"Driver": "bridge",
"EnableIPv6": false,
"IPAM": {
"Driver": "default",
"Options": null,
"Config": [
{
"Subnet": "172.19.0.0/16",
"Gateway": "172.19.0.1"
}
]
},
"Internal": false,
"Attachable": true,
"Ingress": false,
"ConfigFrom": {
"Network": ""
},
"ConfigOnly": false,
"Containers": {
"03af7eb1bcfb394c436784974603ad72667610a8a2bba6f8ec3ca87a3eecc733": {
"Name": "project_mongo_1",
"EndpointID": "6567c3ad72fba3d2d4519d6aa47ac9fc7d65d2b6884f91aa70f4041ba2fc98cc",
"MacAddress": "02:42:ac:13:00:02",
"IPv4Address": "172.19.0.2/16",
"IPv6Address": ""
},
"13dce3db104e2dafb2ebbdceeefe1a5ca8559808c050a9e95d4abae5b2203b54": {
"Name": "project_redis_1",
"EndpointID": "fb5abe7b896fb9d5f635cd30cbc57ad721e184c36a754bffce696b7f7fc1fbfc",
"MacAddress": "02:42:ac:13:00:05",
"IPv4Address": "172.19.0.5/16",
"IPv6Address": ""
},
"3f301bd0973637ac4440f9f9bfc8f6d2fdf4d3bf048ab95c9db91ef03dc4cde1": {
"Name": "dimelo_faye_server",
"EndpointID": "90eda817636ce87123882187cea629c63419712816fbf952364959fd6d41f25b",
"MacAddress": "02:42:ac:13:00:06",
"IPv4Address": "172.19.0.6/16",
"IPv6Address": ""
},
"530c61bfde2a21812ae3b677f0a019805a1dcea21722d181648defd0037de7f3": {
"Name": "project_elasticsearch_1",
"EndpointID": "a8cd9df941907c4abfb5762fe6af5069cfa6e97259d2d5c1c9992f269f6b444a",
"MacAddress": "02:42:ac:13:00:03",
"IPv4Address": "172.19.0.3/16",
"IPv6Address": ""
},
"5e615627d664bf5cad7b31f5594bc64730dca46ae8c80bb12b0ee881dac8bfb1": {
"Name": "project_memcache_1",
"EndpointID": "b7388f26c1aaedb98ebc6d3eac018d713e0c914bc4d6a84501358cc126be1105",
"MacAddress": "02:42:ac:13:00:04",
"IPv4Address": "172.19.0.4/16",
"IPv6Address": ""
},
"6b143aa84d6fd55245b462220a4dab4f54c9d365ab4a1e7c7823265b53432bbb": {
"Name": "project_web_run_53",
"EndpointID": "964311691aefff57c9ad5898b1153c8a166575193c25babad575e9990092e911",
"MacAddress": "02:42:ac:13:00:08",
"IPv4Address": "172.19.0.8/16",
"IPv6Address": ""
}
},
"Options": {},
"Labels": {
"com.docker.compose.network": "default",
"com.docker.compose.project": "project"
}
}
]
What can I do to be able to have access to Internet on the container when the VPN is ON?

Resources