shell script + constant spaces between the IP to the aliases name - ksh

from my ksh script
.
echo $IP1 $ALIAS1 >> /etc/hosts
echo $IP2 $ALAIS2 >> /etc/hosts
echo $IP3 $ALIAS3 >> /etc/hosts
I get the hosts file as the following
10.10.10.10 node1_star
10.10.10.100 node_from_some_where
10.10.1.1 Node_HP_MACHINE
what the simple way to create the following hosts file view
in order to get constant spaces between the IP to the aliases name
as the follwoing:
(it could be by printf or by echo manipulation)
10.10.10.10 node1_star
10.10.10.100 node_from_some_where
10.10.1.1 Node_HP_MACHINE

printf is a powerful function that can do exactely what you want.
printf "%-20s %s\n" "$IP1" "$ALIAS1" >> /etc/hosts

Related

sed to replace the ip addresses in /opt/data/hosts in bash script

I have a bash script where I want that script to replace the "IP addresses" in the hosts file by deleting the existing IP addresses.
My hosts file contains the below entry..
[buildservers]
10.10.10.01
10.10.10.02
10.10.10.03
[buildservers:vars]
ansible_connection=ssh
ansible_user=root
My expected result after running the script
[buildservers]
10.10.10.11
10.10.10.12
10.10.10.13
[buildservers:vars]
ansible_connection=ssh
ansible_user=root
and below is my bash script
#!/bin/bash
hosts=/opt/data/hosts
if [ -f $hosts ] ; then
echo "Hosts file exists"
update_host
else
echo "Hosts: No such file"
exit 0;
fi
update_host() {
sed -i '/^\[buildservers\].*/!b;n;c$1' $hosts
}
but it is replacing only the first IP address and not deleting the existing IP addresses.
Please provide the correct "sed" to do this..
Here is an alternative using GNU awk (for inplace editing)
#!/bin/bash
hosts=/opt/data/hosts
update_host(){
awk -i inplace '
BEGIN { FS=OFS="."}
f {
if (/\[buildservers:vars\]/)
f=0
else $4 = $4+10
}
/\[buildservers\]/{ f=1 }
{ print }
' "$hosts"
}
if [ -f $hosts ] ; then
echo "Hosts file exists"
update_host
else
echo "Hosts: No such file"
exit 0;
fi
You can try this one. ${REPLACE} should be all IP addresses to be replaced and separated by a newline
sed "/^\[buildservers\]/bx :x; /[0-9]*\.[0-9]*\.[0-9]*\.[0-9]*/d;by :y; 1a${REPLACE}"

Is there a way to display function output in bash command

Have a simple bash script I am working on with Mac, but I am having one issue.
#!/bin/bash
# Pull users IP from getifaddr on specfic port
myip ( ) {
#ipconfig getifaddr ppp0
local _ip _myip _line _nl=$'\n'
while IFS=$': \t' read -a _line ;do
[ -z "${_line%inet}" ] &&
_ip=${_line[${#_line[1]}>4?1:2]} &&
[ "${_ip#127.0.0.1}" ] && _myip=$_ip
done< <(LANG=C /sbin/ifconfig)
printf ${1+-v} $1 "%s${_nl:0:$[${#1}>0?0:1]}" $_myip
}
printf "My ip: %"
myip
# Static server IP stored
serverip ( ) {
echo 192.168.12.110
}
serverip
## Command to add them together
sudo route add $myip $severip
So far the functions work, but the sudo route command does not since I am guessing there is no return value.
What I need it to do it output the myip IP address and the serverip IP address onto the line so the command can run in a single command.
For example: sudo route add 192.168.0.1 192.168.0.2
Right now, it tells me "myip is not a valid routing address" as I believe it is taking the literal 4 character string "myip" and not the IP address.
Bash's command substitution might help:
sudo route add $(myip) $(serverip)

a part string is missing when echo in bash

The bash script is:
echo Updating hostname and IP address ...
echo ${config_template_ip}
echo "ssh-keygen -R ${config_template_ip} -f /home/testusr/.ssh/known_hosts"
echo ${config_template_ip}
The output is
Updating hostname and IP address ...
10.100.224.250
-f /home/testusr/.ssh/known_hosts
10.100.224.250
why didn't "ssh-keygen -R ${config_template_ip} " on STDOUT, and how to solve that?

How can I line break in ecal echo? in bash script

I need a help, i need line brake in my "eval echo".
It is a code for adding multiple additional ip in my server.
code:
#!/bin/bash
echo enter all ip
vi textfile.txt
echo gateway
read g
echo netmask
read n
echo how many ip do you need
read t
for ((u=1;; u++));
do
read "d$u" || break;
done < textfile.txt
for i in `eval echo {1..$t}`
do
eval "echo DEVICE=eth0:$i\nIPADDR=\$d$i\nNETMASK=$n\nGATEWAY=$g\n" # > "ifcfg-eth0:$i";
done
output:
DEVICE=eth0:1nIPADDR=192.168.0.2nNETMASK=255.255.255.0nGATEWAY=192.168.0.1n
required output:
DEVICE=eth0:1
IPADDR=192.168.0.2
NETMASK=255.255.255.0
GATEWAY=192.168.0.1

get ip address from a file and output result in a table

Im trying to get all ip address from file called ipserver.txt and check if the ip address is down or up.
In the file ipserver.txt I have ip address that looks like this:
# My text file ipserver.txt
host1 10.0.0.1
host2 192.168.10.23
host3 192.168.0.1
host4 192.168.23.10
# My script
date
cat ipserver.txt | while read output
do
ping -c 1 "$output" > /dev/null
if [ $? -eq 0 ]
then
# echo -e "\n\033[4;31mHOSTNAME\033[0m \t\t\033[1;4;31mIP\033[0m"
echo "$output is up"
# printf $output >> resultat
else
echo "$output is down"
fi
done
# cat resultat
What Im trying to output is a table of all ouputs like this:
HOSTNAME IP UP/DOWN
--------------------------------------------
host1 10.0.0.1 UP
host2 192.168.10.23 DOWN
host3 192.168.0.1 UP
host4 192.168.23.10 DOWN
--------------------------------------------
In my script Im trying to redirect the $output to resultat, but dont now if Im thinking right.
In text file ipserver.txt I can check the ip address from the file if I dont write host1, host2, host3 and host4.
Can somebody give me a tip on how to resolve my script, so I can get the output that i want
This will do what you want:
#!/bin/bash
printf "HOSTNAME\tIP\t\t\tUP/DOWN\n"
echo "----------------------------------------------------------"
while read -r host ip
do ping -c 1 "$ip" > /dev/null 2>&1 && printf "$host\t\t$ip\t\tUP\n" || printf "$host\t\t$ip\t\tDOWN\n"
done < ipserver.txt
echo "----------------------------------------------------------"
Probably best to put that in a script. If you want to send the results to a file, redirect the entire output. i.e. if you called it test-servers and made it executable:
./test-servers >> resultat
I think you're looking for printf, does the following work for you?
date
i=1
cat ipserver.txt | while read output
do
extr=${output#"host$i"}
ping -c 1 "$extr" > /dev/null
if [ $? -eq 0 ]
then
printf "%-16s %-16s %s\n" "host$i" $extr "UP"
else
printf "%-16s %-16s %s\n" "host$i" $extr "DOWN"
fi
((i++))
done

Resources