help with sed

General support questions
Post Reply
Spork Schivago
Posts: 37
Joined: 2017/08/14 04:21:54

help with sed

Post by Spork Schivago » 2018/10/20 06:07:00

Hi!

Not sure if this is the right place to ask but I couldn't really think of any others.

I have a script that searches for a string in a file. If it finds the string, it replaces the string with a variable in the script. The variable in the script can (but doesn't mean it will) contain multiple words separated by a space. If there is more than one word in the variable, I would like to add each word to the file, after the first one is added. I will show code and give an example of my expected output:

Code: Select all

IP_ADDR=$( grep -o -E '192\.168\.2\.([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-4])\b' fake_ips | tr '\n' ' '|sed -e 's/[[:space:]]*$//' )
SSHD_CONFIG="sshd_config.test"
X=19

function update_sshd_config() {
      EXPANDED_IP_ADDR="$(echo ${IP_ADDR// /$'\n'})"

      for IP_ADDRESSES in ${EXPANDED_IP_ADDR}
      do
          grep -q -F "ListenAddress ${IP_ADDRESSES}" ${SSHD_CONFIG} || sed -i "0,/#ListenAddress .*/s//ListenAddress ${IP_ADDRESSES}/" ${SSHD_CONFIG}
          X=$((${X}+1))
      done
}

update_sshd_config
So first I check sshd_config.test to see if the IP address is already added. If it's not, then I'm attempting to use sed to replace #ListenAddress <whatever> with ListenAddress <IP Address>

This script of mine does not work properly. It replaces #ListenAddress 0.0.0.0 with the first IP address, but then it replaces #ListenAddress :: with the second and stops. This is not what I want. I want to replace #ListenAddress 0.0.0.0 with the first IP address, and then append ListenAddress <second IP> etc on new lines afterwards.

Any ideas how to accomplish this? If there is a better place to ask, I am okay with suggestions.
-- Niklaus Wirth's Law: software is getting slower more rapidly than hardware becomes faster.

Spork Schivago
Posts: 37
Joined: 2017/08/14 04:21:54

Re: help with sed

Post by Spork Schivago » 2018/10/20 06:23:27

It might not be the best solution, but I believe I have it now.

This is my new script:

Code: Select all

IP_ADDR=$( grep -o -E '192\.168\.2\.([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-4])\b' fake_ips | tr '\n' ' '|sed -e 's/[[:space:]]*$//' )
SSHD_CONFIG="sshd_config.test"

function update_sshd_config() {
      EXPANDED_IP_ADDR="$(echo ${IP_ADDR// /$'\n'})"
      for IP_ADDRESSES in ${EXPANDED_IP_ADDR}
      do
          grep -q -F "ListenAddress ${IP_ADDRESSES}" ${SSHD_CONFIG} || sed -i "0,/#ListenAddress .*/s//ListenAddress ${IP_ADDRESSES}\n#ListenAddress 0.0.0.0/" ${SSHD_CONFIG}
      done

      grep -q -F "#ListenAddress 0.0.0.0" ${SSHD_CONFIG} && sed -i "/#ListenAddress 0.0.0.0/d" ${SSHD_CONFIG}
}

update_sshd_config
I first search to see if the IP address is in the config, if it's not, I remove #ListenAddress <any IP address> and replace it with ListenAddress <IP address N>. I insert a new line, then add #ListenAddress 0.0.0.0 and repeat the process until no more IP addresses are left to parse.

After the for loop runs, I go back and delete the #ListenAddress 0.0.0.0 from the config file. Not the way I really wanted to do it but it works.
-- Niklaus Wirth's Law: software is getting slower more rapidly than hardware becomes faster.

Post Reply