Pergunta

I have a series of devices that I need to SSH into, modify some files changing the device name, and network settings.

The files contain data such as:

NETWORK_ETH1_CLIENTIP_LIST='10.47.19.182'

HOSTNAME="c50"

I am using putty to ssh in to the system, and then using nano to modify each file.

I believe it would be easy to script but I am not too sure of the linux commands for what I would like to do.

Are there commands in linux I could use that could search for strings within a specified text file, e.g. "HOSTNAME" and replace the found line with the new HOSTNAME?

I am current using plink for some of easier system checks that I call from batch files:

plink.exe -pw PASSWORD USERNAME@192.168.77.77 -m Commands/SystemChecks.txt

Username and password have been omitted. SystemChecks contains some basic lookup table and ping commands etc to test if the device was successfully modified.

What I need help with is figuring out a way to find and replace data (hostname, ip address, netmasks etc) using linux commands.

Foi útil?

Solução

you can use sed to blow the line away and re-write the content with echo. I'm sure there is probably more elegant way to replace just the value

target='/path/to/target_file'
sed -i "/HOSTNAME/d" $target
echo "HOSTNAME=\"newhostname\"" >> $target

Outras dicas

echo 'HOSTNAME="c50"' | sed 's/HOSTNAME="c50"/HOSTNAME="c60"/g'

Using the command sed, you can achieve substitution easily.

Suppose you have a file called file.conf and you have following content in that file like following:

NETWORK_ETH1_CLIENTIP_LIST='10.47.19.182'
HOSTNAME="c50"

So, if you want to change the value HOSTNAME="c50" to HOSTNAME="d10000", you can make use sed in following way:

sed -i 's/HOSTNAME="c50"/HOSTNAME="d10000"/g' file.conf
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top