Command run via xargs fails but runs manually - bash

I'm trying to create a command that will automatically attach to my existing python docker container, and trying to chain a bunch of commands together.
docker ps | grep "mypythoncontainer" | awk '{print $1}' | xargs docker attach
If I run
docker ps | grep "mypythoncontainer" | awk '{print $1}' | xargs echo
I get back a docker id string, as expected. And if I do docker attach {id string} (copied from the return of the statement right above this), it works. But when I run the full command at top, I get an error (the input device is not a TTY).
So docker ps | grep "mypythoncontainer" | awk '{print $1}' | xargs echo would echo out abc, but docker ps | grep "mypythoncontainer" | awk '{print $1}' | xargs docker attach would fail, while docker attach abc works. Not sure what about xargs I don't understand.

Try:
docker attach $(docker ps | grep "mypythoncontainer" | awk '{print $1}')
or simplier:
docker attach $(docker ps | awk '/mypythoncontainer/{print $1}')
Not sure what about xargs I don't understand.
Running: ...| ... docker ... will redirect docker's standard input to ... the ouput of awk, wich was already read by xargs. So docker abc will r
un with a broken (already closed) STDIN, then fail.

Related

How do i redirect a list of IP addresses to a command line function?

I want to see what countries are trying to access my VPS. I have installed a tool called "goiplookup", which was forked from another effort called "geoiplookup". If I type this at the command line:
goiplookup 8.8.8.8
It returns this:
US, United States
So I figured out how to get a list of IPs that are trying to access my server by using this:
sudo grep "disconnect" /var/log/auth.log | grep -v COMMAND | awk '{print $9}'
Which gives a long list of IPs like this:
1.1.1.1
2.2.2.2
3.3.3.3
I cannot figure out how to get this list of IPs to be processed by the "goiplookup" tool. I tried this:
sudo grep "disconnect" /var/log/auth.log | grep -v COMMAND | awk '{print $9}' | goiplookup
but that did not work. I also tried with no luck:
sudo grep "disconnect" /var/log/auth.log | grep -v COMMAND | awk '{print $9}' | xargs -0 goiplookup
Try this:
sudo grep "disconnect" /var/log/auth.log | grep -v COMMAND | awk '{print $9}' | sort | uniq | xargs -n 1 goiplookup
I added | sort | uniq to ensure each IP only appears once
and xargs -n 1 so that each found IP is processes by goiplookup
I would put it into a file and make a small utility to parse it:
sudo grep "disconnect" /var/log/auth.log | grep -v COMMAND | awk '{print $9}' | sort -u > ./file.txt
cat ./file.txt | while read -r line; do
temp$(echo $line)
goiplookup $temp
done
This will read through the file one line at a time and execute the goiplookup with each IP.
sudo grep disconnect /var/log/auth.log | awk '!/COMMAND/ && !seen[$0]++ {system("geoiplookup \""$9"\""}
Note that geoiplookup only allows one IP per invocation.
The whole thing can be done in awk, but using grep allows the rest to be run unprivileged.
Consider whether grep -w (match whole word) is appropriate, and in awk you can do a similar thing with !/(^|[^[:alnum:]_])COMMAND($|[^[:alnum:]_])/.
I just made a shell script, which works.
#!/bin/bash
readarray -t array < <(sudo grep "disconnect" /var/log/auth.log | grep -v COMMAND | awk '{print $9}' | sort | uniq)
for ip in "${array[#]}"
do
:
country=$(/usr/local/bin/goiplookup -c $ip)
echo "$ip $country"
done

bash script (or something else) to automate docker tag docker push

Looking for help to write a bash script to automate my docker workflow, or open to suggestions what to do instead
Current workflow is:
1.
me$ docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
abc.amazonaws.com/XYZ/XYZ-server 0.0.3-7-g45b4b4232e cf324458299c 8 minutes ago 936MB
2.
me$ docker tag cf324458299c abc.amazonaws.com/XYZ/XYZ-server:0.0.3-7-g45b4b4232e
me$ docker tag <last_image_id> <last_repo_id>:<last_tag>
3.
me$ docker push abc.amazonaws.com/XYZ/XYZ-server:0.0.3-7-g45b4b4232e
how could I automate this in a bash script so I can put it as an alias?
thank you very much
You would just extract the needed data and use them.
# 1.
if ! tmp=$(docker images --format '{{.Repository}}\t{{.Tag}}\t{{.ID}}' | grep 'abc.amazonaws.com/XYZ/XYZ-server'); then
: #hadnle error
fi
IFS=$'\t' read -r last_image_id last_repo_id last_tag <<<"$tmp"
# 2.
docker tag "$last_image_id" "$last_repo_id:$last_tag"
# 3.
docker push "$last_repo_id:$last_tag"
Steps:
create a bash script sample.sh and add these lines in it
var="amazonaws"
echo docker tag $(docker images | grep $var | awk '{print $3}') $(docker images | grep $var | awk '{print $1}'):$(docker images | grep $var | awk '{print $2}')
docker tag $(docker images | grep $var | awk '{print $3}') $(docker images | grep $var | awk '{print $1}'):$(docker images | grep $var | awk '{print $2}')
echo docker push $(docker images | grep $var | awk '{print $1}'):$(docker images | grep $var | awk '{print $2}')
docker push $(docker images | grep $var | awk '{print $1}'):$(docker images | grep $var | awk '{print $2}')
chmod +x sample.sh
execute as: sample.sh

Not able to run piped shell scripts in jenkins

I am trying to kill all the existing processes before checkout the code and build it, in order to do, I am using the below command.
sudo ps -ef | grep 'dotnet' | grep -v grep | awk '{print $2}' | xargs -r kill -9;
This is working fine when I run it in the server manually. whereas, using in the jenkins pipeline as Execute Shell script, its not working.
Here is the jenkin's output
**-----------
[CICD] $ /bin/sh -xe /tmp/jenkins6283168714008394634.sh
+ ps -ef
+ grep dotnet
+ grep -v grep
+ + awk {print $2}xargs
-r kill -9
Failed build for hudson.tasks.Shell#178f47d3
**------------
Can someone please help?

Grep on docker image names in package.json

I want to set a script in the package.json that starts the container based on image-name that I grep after piping the docker images command, something like this:
"docker:run": "docker run -it -v /$(pwd):/app $(docker images | grep 'online-check-in-frontend' | awk '{ print $1 }') sh",
also tried :
"docker:run": "docker run -it -v /$(pwd):/app $(docker images | grep \"online-check-in-frontend\" | awk \"{ print $1 }\") sh",
The commands above is not working, i get the following error:
Im using windows, and bash. :S
Br

Ubuntu grep option error while executing a batch script

#!/bin/bash
vm1_MAC=`virsh -c qemu:///system domiflist instance-00000009 -e | grep virbr0 | awk '{print $5}'`
vm2_MAC=`virsh -c qemu:///system domiflist instance-0000000d -e | grep -i virbr0 | awk -e '{print $5}'`
vm1_IP=`arp -e | grep $vm1_MAC | awk '{print $1}'`
vm2_IP=`arp -e | grep $vm2_MAC | awk '{print $1}'`
echo "VM1 IP Address: $vm1_IP"
echo "VM2 IP Address: $vm2_IP"
The shell script was meant to display the IP addresses of my two openstack instances but I am receiving grep command option error:
Usage: grep [OPTION]... PATTERN [FILE]...
Try 'grep --help' for more information.
Usage: grep [OPTION]... PATTERN [FILE]...
Try 'grep --help' for more information.
VM1 IP Address:
VM2 IP Address:
Is there anyone here who can assist me as I am not a bash script expert, just need to do this to get some tasks done. Thank you
This message typically happens when you try to grep for a string which starts with a dash.
The immediate workaround is to use grep -e "$variable" but really, you want to avoid the useless use of grep here.
#!/bin/bash
vm1_MAC=$(virsh -c qemu:///system domiflist instance-00000009 -e | awk "/virbr0/"'{print $5}')
vm2_MAC=$(virsh -c qemu:///system domiflist instance-0000000d -e | awk -e 'tolower($0) ~ /vibr0/ {print $5}')
vm1_IP=$(arp -e | awk -v mac="$vm1_MAC" '$0 ~ mac {print $1}')
vm2_IP=$(arp -e | awk -v mac="$vm2_MAC" '$0 ~ mac {print $1}')
Incidentally, this also demonstrates three different ways to pass a regex to Awk. Notice as well how we prefer the modern $(command) substitution over the dinosaur `backtick` syntax.

Resources