Skip to main content
added 305 characters in body
Source Link
Bogdan
  • 976
  • 1
  • 6
  • 12

We also need to determine the OLDIP because that's what we replace:

OLDIP=`grep -w away -A 1 /etc/ssh/ssh_config | grep Hostname | awk '/Hostname/ {print $2}'`

It is necessary that here the Hostname line is exactly below the Host away line otherwise you would have to adjust -A 1 to -A 2.


-w away matched the line where you have the word away

-A 1 shows one line after the line matched perviously

awk '/Hostname/ {print $2}' from those few lines previously matched we only keep the Hostname line and from that we only keep the second column.


Then we just do a sed to replace the OLDIP with IP.

sed -i "s/$OLDIP/$IP/g" /etc/ssh/ssh_config

The hole thing would look something like:

#!/bin/sh
IP=$(wget http://ipecho.net/plain -qO-)
OLDIP=`grep -w away -A 1 /etc/ssh/ssh_config | awk '/Hostname/ {print $2}'`
sed -i "s/$OLDIP/$IP/g" /etc/ssh/ssh_config

We also need to determine the OLDIP because that's what we replace:

OLDIP=`grep -w away -A 1 /etc/ssh/ssh_config | grep Hostname | awk '{print $2}'`

It is necessary that here the Hostname line is exactly below the Host away line otherwise you would have to adjust -A 1 to -A 2.

Then we just do a sed to replace the OLDIP with IP.

sed -i "s/$OLDIP/$IP/g" /etc/ssh/ssh_config

The hole thing would look something like:

#!/bin/sh
IP=$(wget http://ipecho.net/plain -qO-)
OLDIP=`grep -w away -A 1 /etc/ssh/ssh_config | awk '/Hostname/ {print $2}'`
sed -i "s/$OLDIP/$IP/g" /etc/ssh/ssh_config

We also need to determine the OLDIP because that's what we replace:

OLDIP=`grep -w away -A 1 /etc/ssh/ssh_config | awk '/Hostname/ {print $2}'`

It is necessary that here the Hostname line is exactly below the Host away line otherwise you would have to adjust -A 1 to -A 2.


-w away matched the line where you have the word away

-A 1 shows one line after the line matched perviously

awk '/Hostname/ {print $2}' from those few lines previously matched we only keep the Hostname line and from that we only keep the second column.


Then we just do a sed to replace the OLDIP with IP.

sed -i "s/$OLDIP/$IP/g" /etc/ssh/ssh_config

The hole thing would look something like:

#!/bin/sh
IP=$(wget http://ipecho.net/plain -qO-)
OLDIP=`grep -w away -A 1 /etc/ssh/ssh_config | awk '/Hostname/ {print $2}'`
sed -i "s/$OLDIP/$IP/g" /etc/ssh/ssh_config
Source Link
Bogdan
  • 976
  • 1
  • 6
  • 12

We also need to determine the OLDIP because that's what we replace:

OLDIP=`grep -w away -A 1 /etc/ssh/ssh_config | grep Hostname | awk '{print $2}'`

It is necessary that here the Hostname line is exactly below the Host away line otherwise you would have to adjust -A 1 to -A 2.

Then we just do a sed to replace the OLDIP with IP.

sed -i "s/$OLDIP/$IP/g" /etc/ssh/ssh_config

The hole thing would look something like:

#!/bin/sh
IP=$(wget http://ipecho.net/plain -qO-)
OLDIP=`grep -w away -A 1 /etc/ssh/ssh_config | awk '/Hostname/ {print $2}'`
sed -i "s/$OLDIP/$IP/g" /etc/ssh/ssh_config