Sponsored Content
Full Discussion: Hostsfile generator
Top Forums Shell Programming and Scripting Hostsfile generator Post 303040879 by Marti95 on Friday 8th of November 2019 05:55:54 AM
Old 11-08-2019
I attachet my script.

Its written in german.
Quesiton for yes and no: j=yes n=no

Quote:
Ist es eine Bähler/Dropa? [j/n]
use "j" (yes)

It generates you a hosts file.

Code:
#!/bin/bash
# ==============================================================================================
# Script um Hostfiles zu erstellen
# ==============================================================================================
# @Since 07.03.2019
# @Author Roman Buehler
# ==============================================================================================
# CHANGELOG
# 07.03.2019 - Version 1.0.0 RBU
# ==============================================================================================


# =====================
# ====== DECLARE ======
# =====================
declare -A ENTRIES
declare -A ENTRY
declare -x kunnr
declare -x ip3
declare -i baehler


# =======================
# ====== FUNCTIONS ======
# =======================

# Funktion welche eine Ja/Nein frage stellt und @makeentry aufruft.
# Input: $1=HOST; $2=IP; $3=COMMENT; $4=LINE ABOVE (Comment line);
function askyn() {
while true; do
read -p "${3} (${1})? [j/n]: " yn
case $yn in
[Jj]* ) makeentry "$1" "$2" "$3" "$4" "$5"; return 1;;
[Nn]* ) return 0;;
* ) echo "Bitte mit Ja [j] oder Nein [n] beantworten.";;
esac
done
}

# Funktion welche eine nummerische abfrage stellt und @makeentry aufruft.
# Input: $1=HOST; $2=IP; $3=COMMENT; $4=LINE ABOVE (Comment line);
function asknum() {
while true; do
read -p "Anzahl ${3/\{n\}/}(${1})?: " num
re='^[0-9]+$'
if [[ $num =~ $re ]] ; then
# Es ist eine Zahl
if [[ "$num" -ge "1" ]]; then
# >= 1
ip=$2
comment=$3
# Comment Line
if [ ! -z "$4" ]
then
# Titel Linie Generieren
addcomment "$ip" "$4"
fi
# Entry
for (( i=1; i<=$num; i++ ))
do
makeentry "${1}${i}" "${ip}" "${comment/\{n\}/$i}" "" "$5" # Kommentar {n} wird durch aktuelle Zahl ersetzt.
((ip++))
done
break;
else
break;
fi
else
# Es ist keine Zahl
echo "Bitte eine Zahl angeben";
fi
done
return 1
}

# Funktion welche einen Host eintrag in ein Array schreibt.
# Input: $1=HOST; $2=IP; $3=COMMENT; $4=LINE ABOVE (Comment line); $5=Host1 & Host2 umkehren (1=umkehren)
# Array: INDEX, $ip, kunnmr-host, host, comment, line above (Title line)
function makeentry() {
# IP - KNNRHOST - HOST - COMMENT

host=${1,,}; # lowercase

# Comment Line
if [ ! -z "$4" ]
then
# Titel Linie Generieren
addcomment "$2" "$4"
fi

# IP Address
ENTRY["IP"]="$ip3.$2"
if [[ "$5" == "1" ]]; then
if [[ "$baehler" == "1" ]]
then
# Bähler Host
ENTRY["HOST2"]="$kunnum$host"
else
ENTRY["HOST2"]="$kunnum-$host"
fi
ENTRY["HOST1"]="$host"
else
if [[ "$baehler" == "1" ]]
then
# Bähler Host
ENTRY["HOST1"]="$kunnum$host"
else
ENTRY["HOST1"]="$kunnum-$host"
fi
ENTRY["HOST2"]="$host"
fi
ENTRY["COMMENT"]="# $3"
for key in "${!ENTRY[@]}"; do
ENTRIES[$2"5",$key]=${ENTRY[$key]} # INDEX={IP}5
done
#echo ${ENTRY["IP"]}" "${ENTRY["HOST1"]}" "${ENTRY["HOST2"]}" "${ENTRY["COMMENT"]};
return 1
}

# Funktion welche einen Kommentar als Host eintrag erstellt
# Input: $1=ip; $2=COMMENT;
function addcomment() {
# Titel Linie Generieren
ENTRY["IP"]="# $2"
ENTRY["HOST1"]=""
ENTRY["HOST2"]=""
ENTRY["COMMENT"]=""
for key in "${!ENTRY[@]}"; do
ENTRIES[$1"4",$key]=${ENTRY[$key]} # INDEX={IP}4
done
# Zeile darüber = leere Zeile
ENTRY["IP"]=" "
for key in "${!ENTRY[@]}"; do
ENTRIES[$1"0",$key]=${ENTRY[$key]} # INDEX={IP}0
done
}

# Funktion um IP-Adresse zu pruefen (Nur 3 Segmente)
function valid_ip()
{
local ip=$1
local stat=1

if [[ $ip =~ ^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$ ]]; then
OIFS=$IFS
IFS='.'
ip=($ip)
IFS=$OIFS
[[ ${ip[0]} -le 255 && ${ip[1]} -le 255 \
&& ${ip[2]} -le 255 ]]
stat=$?
fi
return $stat
}


# ==================
# ====== MAIN ======
# ==================

# Welcome Message
echo "===================================================="
echo " Willkommen beim Hostfile Generator (Hosti) "
echo "===================================================="


# Kndr NR oder Baehler ident
# Check if Baehler
while true; do
read -p "Ist es eine Bähler/Dropa? [j/n]: " yn
case $yn in
[Jj]* )
baehler="1"
while true; do
read -p "Bitte DBD Ident/Filalnummer angeben (z.B. 01, 123, 234, ...) Wiki #4428...: " input_baehler
re='^[0-9]+$'
if [[ "$input_baehler" =~ $re ]]
then
if [[ $input_baehler -le 9 ]]
then
kunnum="0$input_baehler"
else
kunnum=$input_baehler
fi
break;
else
echo "Bitte Zahlen angeben!"
fi
done
break;;
[Nn]* )
baehler="0"
while true; do
read -p "Bitte Kundennummer angeben (z.B. 15533)...: " input_knr
re='^[0-9]+$'
if [[ "$input_knr" =~ $re ]] || [[ "$input_knr" -eq 5 ]]
then
kunnum=$input_knr
break;
else
#Kundennummer nicht OK (5 Stellen & Zahl)
echo "Kundennummer ist nicht korrekt! (Muss 5 stellig sein)"
fi
done
break;;
* ) echo "Bitte mit Ja [j] oder Nein [n] beantworten.";;
esac
done

# Kundennr OK
# Kundennr Splitten & IP Bereich generieren
if [[ baehler -eq "1" ]]
then
seg12="192.168"
if [[ $input_baehler -le 9 ]]
then
seg3="10$input_baehler"
elif [[ $input_baehler -le 99 ]]
then
seg3="1$input_baehler"
else
seg3="$input_baehler"
fi
ip3="$seg12.$seg3"
else
seg1="10"
seg2=${input_knr:0:3}
seg3=${input_knr:3:2}
ip3="$seg1.$seg2.$seg3"
fi
echo "IP Vorschlag: $ip3.xxx"
while true; do
read -p "Vorschlag in ordnung? [j/n]: " yn
case $yn in
[Jj]* ) break;;
[Nn]* )
while true; do
read -p "Bitte IP Adresse angeben (3 Segmente xxx.xxx.xxx): " input_ip
if valid_ip $input_ip; then
ip3="$input_ip"
break
else
echo "IP Adresse ist nicht korrekt."
fi
done
break;;
* ) echo "Bitte mit Ja [j] oder Nein [n] beantworten.";;
esac
done



# Propharma Server abfrage
# 1. Proxmox
# 2. CentOS
# 3. Physisch mit IMM
# 4. Physisch ohne IMM
addcomment "41" "x.x.x.41 - x.x.x.50 Server" # Überschrift PP Server
makeentry "propharma" "50" "ProPharma Server";
while true; do
echo "=== PROPHARMA SERVER ==="
maxcpp=4 # Maximale anzahl auswahlmöglichkeiten für PP Server
echo -e "1. CentOS\n2. Proxmox\n3. Physisch mit IMM\n4. Physisch ohne IMM"
read -p "Bitte wählen [1-$maxcpp]: " input_pp
re='^[0-9]+$'
if [[ "$input_pp" =~ $re ]] && [[ "$input_pp" -le "$maxcpp" ]]
then
case $input_pp in
[1]* )
makeentry "imm1" "5" "IMM 1 (ProPharma)"
makeentry "virtsrv1" "41" "CentOS (ProPharma)"
break;;
[2]* )
makeentry "imm1" "5" "IMM 1 (ProPharma)"
makeentry "proxmox1" "41" "Proxmox 1 (ProPharma)"
break;;
[3]*)
makeentry "imm1" "5" "IMM 1 (ProPharma)"
break;;
[4]*) break;;
* )
echo "Fehler."
break;;
esac
break
else
echo "Bitte zwischen 1 und $maxcpp wählen!"
fi
done



# RDP Server abfrage
# 1. Proxmox
# 2. CentOS
# 3. Physisch mit IMM
# 4. Physisch ohne IMM
# 5. Kein RDP Server
while true; do
echo "=== RDP SERVER ==="
maxcrdp=5 # Maximale anzahl auswahlmöglichkeiten für RDP Server
echo -e "1. Proxmox\n2. CentOS\n3. Physisch mit IMM\n4. Physisch ohne IMM\n5. Kein RDP Server"
read -p "Bitte wählen [1-$maxcrdp]: " input_rdp
re='^[0-9]+$'
if [[ "$input_rdp" =~ $re ]] && [[ "$input_rdp" -le "$maxcrdp" ]]
then
case $input_rdp in
[1]* )
makeentry "imm2" "6" "IMM 2 (RDP-Server)"
makeentry "proxmox2" "42" "Proxmox 2 (RDP-Server)"
rdpserver=1
break;;
[2]* )
makeentry "imm2" "6" "IMM 2 (RDP-Server)"
makeentry "virtsrv2" "42" "CentOS (RDP-Server)"
rdpserver=1
break;;
[3]*)
makeentry "imm2" "6" "IMM 2 (RDP-Server)"
rdpserver=1
break;;
[4]*)
rdpserver=1
break;;
[5]*)
rdpserver=0
break;;
* )
echo "Fehler."
break;;
esac
break
else
echo "Bitte zwischen 1 und $maxcrdp wählen!"
fi
done

if [[ rdpserver -eq "1" ]]; then
makeentry "rdp" "100" "RDP-Server"
if [[ baehler -eq "1" ]]; then
addcomment "100" "x.x.x.100 - x.x.x.100 RDP Server" # Überschrift RDP Server
asknum "rdp" "131" "RDP-Terminal {n}" "x.x.x.131 - x.x.x.155 RDP-Terminal" "1" # RDP Platine
else
addcomment "100" "x.x.x.100 - x.x.x.120 RDP Server & Stationen" # Überschrift RDP Server
asknum "rdp" "101" "RDP-Terminal {n}" "" "1" # RDP Platine
fi
fi


# UNAS
# !!! Spezial Fall !!!
while true; do
read -p "UNAS? [j/n]" yn
case $yn in
[Jj]* )
ENTRY["IP"]="$ip3.20"
ENTRY["HOST1"]="unas$kunnum"
ENTRY["HOST2"]="unas"
ENTRY["COMMENT"]="# UNAS Sicherung"
for key in "${!ENTRY[@]}"; do
ENTRIES["205",$key]=${ENTRY[$key]} # INDEX=205
done
break;;
[Nn]* ) break;;
* ) echo "Bitte mit Ja [j] oder Nein [n] beantworten.";;
esac
done

if [[ baehler -eq "1" ]]; then
# Bähler
makeentry "wan" "1" "Opensystem Gateway" "x.x.x.001 - x.x.x.020 ----- Gateway, Switch, Picco und Sicherung" # Firewall
asknum "switch" "2" "Switch {n}" #Switch
askyn "picco" "11" "Picco-Link Basisstation" # Picco

asknum "bon" "21" "Bondrucker {n}" "x.x.x.021 - x.x.x.035 ----- Bondrucker" # Bondrucker

askyn "rez" "51" "Rezeptscanner-PC" "x.x.x.051 ----- Rezeptscanner-PC" # Rez PC

asknum "mps" "161" "Netzwerkdrucker {n}" "x.x.x.161 - x.x.x.190 ----- Netzwerkdrucker" #Drucker

asknum "toschiba" "191" "Toshiba WPS {n}" "#x.x.x.191 - x.x.x.199 ----- WPS Drucker und PreisChecker" # WPS Drucker
asknum "prschk" "195" "Preischecker {n}" # Preischecker

# Bähler Rowa (DMZ)
# !!! Spezial Fall !!!
while true; do
read -p "Rowa? [j/n]" yn
case $yn in
[Jj]* )
addcomment "999" "10.168.${seg3}.xxx ----- DMZ" # Überschrift DMZ
ENTRY["IP"]="10.168.${seg3}.2"
ENTRY["HOST1"]="${kunnum}rowa"
ENTRY["HOST2"]="rowa"
ENTRY["COMMENT"]="# Rowa (DMZ)"
for key in "${!ENTRY[@]}"; do
ENTRIES["9995",$key]=${ENTRY[$key]} # INDEX=9995 (zu letzt)
done
break;;
[Nn]* ) break;;
* ) echo "Bitte mit Ja [j] oder Nein [n] beantworten.";;
esac
done


else
# Normal
asknum "switch" "21" "Switch {n}" "X.X.X.21 - X.X.X.24 Switches" #Switch
addcomment "75" "x.x.x.75 - x.x.x.80 Firewall und WLAN"

# Firewall abfrage
# 1. USG
# 2. Watchguard
# 3. Fortigate
# 4. Zywall
while true; do
echo "=== FIREWALL ==="
maxcfw=4 # Maximale anzahl auswahl möglichkeiten für Firewall
echo -e "1. Fortigate\n2. USG\n3. Watchguard\n4. Zywall"
read -p "Bitte wählen [1-$maxcfw]: " input_fw
re='^[0-9]+$'
if [[ "$input_fw" =~ $re ]] && [[ "$input_fw" -le "$maxcfw" ]]
then
case $input_fw in
[1]* )
makeentry "forti" "75" "Fortigate E30"
break;;
[2]* )
makeentry "usg" "75" "Firewall USG20"
break;;
[3]*)
makeentry "adsl" "75" "Firewall Watchguard"
break;;
[4]*)
makeentry "adsl" "75" "Firewall Zywall"
break;;
* )
echo "Fehler."
break;;
esac
break
else
echo "Bitte zwischen 1 und $maxcfw wählen!"
fi
done

# Firewall
asknum "at" "81" "Axel Terminal {n}" "x.x.x.80 - x.x.x.99 AXEL-Ansi Platinen" "1" # Axel Platine
asknum "mps" "141" "Netzwerkdrucker {n}" "x.x.x.141 - x.x.x.160 Netzwerkdrucker" # Drucker
asknum "prschk" "25" "Preischecker {n}" "x.x.x.25 - x.x.x.29 Preischecker" # Preischecker
asknum "bon" "191" "Bondrucker {n}" "x.x.x.191 - x.x.x.199 Bondrucker" # Bondrucker
askyn "picco" "162" "Picco-Link Basisstation" #Picco
# Kein Unterschied ob DBD oder Normal
askyn "wlan" "77" "WLAN Access Point" # Wlan
askyn "rowa" "161" "Rowa" "x.x.x.161 - x.x.x.161 Rowa" # Rowa
asknum "pc" "51" "PC-Arbeitsplatz {n}" "x.x.x.51 - x.x.x.70 PC-Arbeitsplatz" # Anzahl PC's
asknum "vid" "181" "Raspberry Pi {n}" "x.x.x.181 - x.x.x.190 Raspberry Pi" # Raspberry Pi

fi


# Hosts File generieren
echo "===================================================="
d=`date +%Y%m%d`
touch "/tmp/hosts.${d}"
IFS=$'\n'
echo "${!ENTRIES[*]}" | sort -t, -k1,1n -k2,2.2r -k2.3 | while read IX; do printf "%-10s\n" ${ENTRIES[$IX]}; done | paste -sd"\t\t\t\n" >> "/tmp/hosts.${d}"
echo "Script finished"
echo -e "\n\nDie Hosts-Datei wurde geschrieben: \e[32m/tmp/hosts.${d}\e[0m\nBitte durch kopieren nach /etc/hosts aktivieren!\n\n"
echo "===================================================="
exit 0


Last edited by Marti95; 11-12-2019 at 11:06 AM..
This User Gave Thanks to Marti95 For This Post:
 

9 More Discussions You Might Find Interesting

1. Cybersecurity

Password Generator

I need a great Password Generator program. I looked at a few of them, but none of them seemed to be what I wanted. So I have decided to write my own. (That's the cool thing about being a programmer....I always get what I want in software :) ) Do you have any password generators that you... (13 Replies)
Discussion started by: Perderabo
13 Replies

2. UNIX for Dummies Questions & Answers

date generator

Is there a command to generate the unix date that is in theshadow file?>? (2 Replies)
Discussion started by: BG_JrAdmin
2 Replies

3. Shell Programming and Scripting

How to compare result lpstat with hostsfile

Hi there all, I got a long list of printers installed and a longer list of printers in my hosts file. In the hosts file I got a even longer list of printers in the hosts file I got the IP adress of all printers next to the printer name. How can I get a script working to get the printers... (0 Replies)
Discussion started by: draco
0 Replies

4. Shell Programming and Scripting

Range generator

Dear All, I have a sorted file like 1 2 3 8 9 10 45 46 47 78 The output will be range like 1 3 8 10 45 47 78 78 (9 Replies)
Discussion started by: saifurshaon
9 Replies

5. Shell Programming and Scripting

Random Sentence Generator

Hi, I need to create a table with random sentences. I need lines that are upto 1000 characters in lenght. I need a random sentence generator that will create sentences and output it to a text file. The sentences should be of lenght varying from 1 to 1000. Does anyone know how this can be... (7 Replies)
Discussion started by: kaushys
7 Replies

6. What is on Your Mind?

Barcode Generator

QR Code Generator (0 Replies)
Discussion started by: Neo
0 Replies

7. Shell Programming and Scripting

time generator

Hi experts, I'd like to generate the table/file containing: number of milliseconds elapsed since midnight till midnight. It should contain 5 columns (hours minutes seconds milliseconds): Table will have theoretically 86 400 000 rows. My question is , is there somewhere the file or source... (7 Replies)
Discussion started by: hernand
7 Replies

8. Shell Programming and Scripting

Sequence generator

Thanks Guys This really helped (5 Replies)
Discussion started by: robert89
5 Replies

9. Shell Programming and Scripting

Generator script

Hello again unix.com people, I need your help again. I'm currently need a script that will generate ip ranges... lets say from 64.1.1.1 to 74.255.255.255 and the output should be like this: 64.1.1.2 64.1.1.3 ............. ............. 74.255.255.254 74.255.255.255 (line-by-line) ... (7 Replies)
Discussion started by: galford
7 Replies
All times are GMT -4. The time now is 05:31 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy