Redirecting linux terminal file data - linux

I have two files. One is v3in.txt file in which I have the commands to be executed list. In the second file v3out.txt I wish to store the output of each of these commands.
Can some one help me how to use the standard input output stream to achieve this.
My v3in.txt file contents:
sudo snmpget -v 3 -l authPriv -a SHA -A "NetMan2019" -x DES -X "ITP201820" -u John 198.51.100.5 .1.3.6.1.2.1.1.4.0
sudo snmpget -v 3 -l authPriv -a SHA -A "Net20192020" -x DES -X "TCP201820" -u John 198.51.100.5 .1.3.6.1.2.1.1.5.0

Related

How to edit the mosquitto.conf in a mosquitto Docker container?

I have a linux system running with several Docker containers. One of them is mosquitto container which runs from mosquitto 1.6.7 docker image.
I do not have control how the Mosquitto container is created as it is given by default from a supplier/client.
I need to make changes in the mosquitto/config/mosquitto.conf file. This is the output when I run ls -l
/mosquitto/config # ls -l
total 4
-rwxrwxr-x 1 nobody nobody 210 May 24 05:35 mosquitto.conf
I tried the codes below to add a comment in the mosquitto.conf, but I am not successful.
/mosquitto/config # echo '#test' | su nobody -c 'tee -a mosquitto.conf'
nologin: this account is not available
/mosquitto/config # echo '#test' | su nobody -s sh -c 'tee -a mosquitto.conf'
su: can't execute 'sh': No such file or directory
/mosquitto/config # echo '#test' | su nobody -s bin/sh -c 'tee -a mosquitto.conf'
su: can't execute 'bin/sh': No such file or directory
/mosquitto/config # echo '#test' | su nobody -s /bin/sh -c 'tee -a mosquitto.conf'
tee: mosquitto.conf: Permission denied
#test
Is it possible to change the mosquitto.conf?
If yes, how? Thanks.
You don't.
You make a copy of it on the host machine, edit there and then mount that edited copy into the container when you start it.
e.g.
docker run -d -v /path/to/local/mosquitto.conf:/mosquitto/config/mosquitto.conf mosquitto

Pass arrays to SSH connections

I want to pass an array to a script that is on a remote computer. I'm using SSH for this. I tried the below code and I'm getting an error saying that the parameter is not available.
ssh -i location/to/keyfile -o StrictHostKeyChecking=no -T ubuntu#18.220.20.50 ./script.sh -m 1G -s 0 -d 120 -w 60 -j 512M -k 512M -l 515M -b "${array_1[*]}" -u "${array_2[*]}"
Here ${array_1} and ${array_2} are indexed arrays.
If I understand the situation correctly, you have two arrays containing numbers, something like:
array_1=(1 2 3)
array_2=(21 22 23)
...and want to pass those lists of numbers to the script as space-separated lists, something like running this on the remote computer:
./script.sh -m 1G -s 0 -d 120 -w 60 -j 512M -k 512M -l 515M -b "1 2 3" -u "21 22 23"
If this is correct, try the following command:
ssh -i location/to/keyfile -o StrictHostKeyChecking=no -T ubuntu#18.220.20.50 ./script.sh -m 1G -s 0 -d 120 -w 60 -j 512M -k 512M -l 515M -b "'${array_1[*]}'" -u "'${array_2[*]}'"
Explanation: commands passed via ssh get parsed twice; first by the local shell, and then the result of that gets parsed again by the remote shell. In each of these parsing phases, quotes (and escapes) get applied and removed. Your original command had only one level of quotes, so the local shell parses, applies, and removes it, so the remote shell doesn't see any quotes, so it treats each of the numbers as a separate thing.
In more detail: the original command:
ssh -i location/to/keyfile -o StrictHostKeyChecking=no -T ubuntu#18.220.20.50 ./script.sh -m 1G -s 0 -d 120 -w 60 -j 512M -k 512M -l 515M -b "${array_1[*]}" -u "${array_2[*]}"
has the array references expanded, giving the equivalent of (assuming the array contents I listed above):
ssh -i location/to/keyfile -o StrictHostKeyChecking=no -T ubuntu#18.220.20.50 ./script.sh -m 1G -s 0 -d 120 -w 60 -j 512M -k 512M -l 515M -b "1 2 3" -u "21 22 23"
The local shell parses and removes the quotes, but they have the effect of passing 1 2 3 and 21 22 23 to the ssh programs as single arguments. But then ssh just pastes the list of command arguments it got back together with spaces in between, so this is what it sends to the remote shell:
./script.sh -m 1G -s 0 -d 120 -w 60 -j 512M -k 512M -l 515M -b 1 2 3 -u 21 22 23
...which confuses the script.
My solution, adding single-quotes around the array references, doesn't change the local parsing (the single-quotes are inside the double-quotes, so they have no special effect); they just get passed through, resulting in this command being sent to the remote shell:
./script.sh -m 1G -s 0 -d 120 -w 60 -j 512M -k 512M -l 515M -b '1 2 3' -u '21 22 23'
The single-quotes here have the same effect that double-quotes would (since there are no other quotes, escapes, dollar signs, or other special characters inside them), so this should give the result you want.
another solution slightly different from the answers in the question nominated by #jww
idea is pass array definition as text;
and than eval them through stdin device
sample code piece below
you need to replace echo part with your own array definition script,
and put the source /dev/stdin inside script.sh
echo 'array_1[id]=3.14'|ssh ubuntu#18.220.20.50 'source /dev/stdin; echo ${array_1[id]}'

Bash Script Command Not Executing

I need help with the following Bash v4.1.2 script.
#!/bin/bash
IP=$1
IPTABLES=/sbin/iptables
$IPTABLES -I INPUT -s $IP -j DROP
echo $IPTABLES -I INPUT -s $IP -j DROP |wall
The variables, IP and IPTABLES, get populated in the echo but the line above is not executed. The echo outputs...
/sbin/iptables -I INPUT -s 1.2.3.4 -j DROP
...which is syntactically correct and works if executed manually.
I don't know Bash so I'm struggling to debug this elementary script. I see some scenarios where commands are left bare as I have mine and some that are wrapped in $() (with and without quotes). I've also tried using backticks and quoting various parts of the command. The echo piped through wall only exists for debugging.
I found a basically identical post at Bash script commands not working in cron. My script is not running from cron though.
=== EDIT ===
Added for #Barmar
[root#server tmp]# bash -x /bin/netfilter-drop.sh
+ IP=1.2.3.4
+ IPTABLES=/sbin/iptables
+ /sbin/iptables -I INPUT -s 1.2.3.4 -j DROP
+ wall
+ echo /sbin/iptables -I INPUT -s 1.2.3.4 -j DROP
[root#server tmp]#
Broadcast message from root#server (Thu Dec 29 12:46:44 2016):
/sbin/iptables -I INPUT -s 1.2.3.4 -j DROP
^C
[root#server tmp]#
I had initially only given sudo access to run the posted Bash script. The problem was not the script, rather it was permissions. I needed to give additional sudo access to run iptables in my sudoers. Fixed.

how to recursively fetch some data with pattern using wget

I am trying to download some specific files from this website (http://nomads.ncep.noaa.gov/pub/data/nccf/com/hourly/prod/), they keep 10 days data. I want to download all the files starting with "ST4" from all the directories starting with "nam_pcpn_anal". I could download all the files staring with "ST4" from one folder like :
wget -r -nd -N --no-parent -nH --cut-dirs=100 -P ~/test/ -A ST4* 'http://nomads.ncep.noaa.gov/pub/data/nccf/com/hourly/prod/nam_pcpn_anal.20160625/'
but I do not know how to search ST4 recursively. I thought the following should work but nope!
wget -r -nd -N --no-parent -nH --cut-dirs=100 -P ~/test/ -A ST4* --accept nam_pcpn_anal*/ST4* 'http://nomads.ncep.noaa.gov/pub/data/nccf/com/hourly/prod/'
Any idea!
The wget manual shows:
-I list
--include-directories=list
Specify a comma-separated list of directories you wish to follow
when downloading. Elements of list may contain wildcards.
So, you could try:
wget -r -nd -N --no-parent -nH --cut-dirs=100 -P ~/test/ \
-A 'ST4*' -I '*/nam_pcpn_anal.*' \
'http://nomads.ncep.noaa.gov/pub/data/nccf/com/hourly/prod/'

Using wget on Linux to scan sub-folders for specific files

Using wget -r -P Home -A jpg http://example.com will result me a list of files from that website directory, what i'm searching for is how do i query a search like: wget -r -P home -A jpg http://example.com/from 65121 to 75121/ file_ 100 to 200.jpg
Example(s):
wget -r -P home -A jpg http://example.com/65122/file_102.jpg
wget -r -P home -A jpg http://example.com/65123/file_103.jpg
wget -r -P home -A jpg http://example.com/65124/file_104.jpg
Is it possible to achieve that on a Linux distro?
I'm fairly new to Linux OS, any tips are welcome.
Use a nested for loop and some bash scripting:
for i in {65121..75121}; do for j in {100..200}; do wget -r -P home -A jpg "http://example.com/${i}/file_${j}.jpg"; done; done
Wget has loop
wget -nd -H -p -A file_{100..200}.jpg -e robots=off http://example.com/{65121..75121}/
If there are only file_{100..200}.jpg It's simpler
wget -nd -H -p -A jpg -e robots=off http://example.com/{65121..75121}/

Categories

Resources