0% found this document useful (0 votes)
12 views5 pages

Network Configuration Script

teste

Uploaded by

valdir2009mimi
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views5 pages

Network Configuration Script

teste

Uploaded by

valdir2009mimi
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd

#!

/bin/bash

# FIND DETAILS
details=$(cat /etc/os-release)

# FIND DIST
dist=$(echo "$details" | grep ^ID= | grep -Eo [a-z]+)

# FIND VERSION
version=$(echo "$details" | grep ^VERSION= | grep -Eo [0-9]+ | head -n 1)

# FIND NAME
name=$(ls /sys/class/net | head -n 1)

# NETMASK SCRIPT
cat <<EOF > /etc/[Link]

import argparse

parser = [Link]()
parser.add_argument('netmask')
args = parser.parse_args()

cidr = 0

for bits in [Link]('.'):

bits = bin(int(bits)).count('1')

cidr = cidr + bits

print(cidr)

EOF

# UBUNTU
function ubuntuIP {

cat <<EOF > /etc/network/interfaces


auto lo
iface lo inet loopback
auto $name
iface $name inet static
address @address
netmask @netmask
gateway @gateway
dns-nameservers [Link] [Link]
EOF

# START
ifup $name

# UBUNTU
function ubuntuIPWithRoute {

cat <<EOF > /etc/network/interfaces


auto lo
iface lo inet loopback
auto $name
iface $name inet static
address @address
netmask @netmask
broadcast @address
post-up route add @gateway dev $name
post-up route add default gw @gateway
pre-down route del @gateway dev $name
pre-down route del default gw @gateway
dns-nameservers [Link] [Link]
EOF

# START
ifup $name

# DEBIAN
function debianIP {

cat <<EOF > /etc/network/interfaces


auto lo
iface lo inet loopback
auto $name
iface $name inet static
address @address
netmask @netmask
gateway @gateway
dns-nameservers [Link] [Link]
EOF

# START
ifup $name

# DEBIAN
function debianIPWithRoute {

cat <<EOF > /etc/network/interfaces


auto lo
iface lo inet loopback
auto $name
iface $name inet static
address @address
netmask @netmask
broadcast @address
post-up route add @gateway dev $name
post-up route add default gw @gateway
pre-down route del @gateway dev $name
pre-down route del default gw @gateway
dns-nameservers [Link] [Link]
EOF

# START
ifup $name

}
# CENTOS
function centosIP {

cat <<EOF > /etc/sysconfig/network-scripts/ifcfg-$name


DEVICE=$name
TYPE=Ethernet
IPADDR=@address
NETMASK=@netmask
GATEWAY=@gateway
ONBOOT=YES
DNS1=[Link]
DNS2=[Link]
EOF

# START
ifup $name

# NETMASK TO CIDR
if which python3; then

cidr=$(python3 /etc/[Link] @netmask)


else

cidr=$(python /etc/[Link] @netmask)


fi

# NETPLAN
function netplanIP {

cat <<EOF > /etc/netplan/[Link]


network:
version: 2
renderer: networkd
ethernets:
$name:
addresses: [@address/$cidr]
gateway4: @gateway
dhcp4: no
nameservers:
addresses: [[Link], [Link]]
EOF

# START
netplan apply

# NETPLAN
function netplanIPWithRoute {

cat <<EOF > /etc/netplan/[Link]


network:
version: 2
renderer: networkd
ethernets:
$name:
addresses: [@address/$cidr]
gateway4: @gateway
dhcp4: no
nameservers:
addresses: [[Link], [Link]]
routes:
- to: @gateway/$cidr
via: [Link]
scope: link
EOF

# START
netplan apply

# NETPLAN
if [ -d /etc/netplan ]; then

if [ $cidr == 32 ]; then
netplanIPWithRoute
else
netplanIP
fi
else

# UBUNTU
if [ "$dist" == ubuntu ]; then

if [ $cidr == 32 ]; then
ubuntuIPWithRoute
else
ubuntuIP
fi
fi

# DEBIAN
if [ "$dist" == debian ]; then

if [ $version == 8 ]; then

if [ $cidr == 32 ]; then
debianIPWithRoute
else
debianIP
fi
else
debianIP
fi
fi

# CENTOS
if [ "$dist" == centos ]; then

centosIP
fi

# ALMA
if [ "$dist" == almalinux ]; then
centosIP
fi

# ROCKY
if [ "$dist" == rocky ]; then

centosIP
fi
fi

# RESIZE PARTITION
(echo d; echo n; echo p; echo ; echo ; echo ; echo w; echo n; echo w) | fdisk
/dev/sda

# RESIZE FILESYSTEM
partprobe && resize2fs /dev/sda1

# LOGIN BY PASSWORD
(echo @password; echo @password) | passwd @username

# LOGIN BY RSA
if [ ! -z "@publicKey" ]; then

# RSA PUBLIC KEY


mkdir -p $HOME/.ssh && echo "@publicKey" > $HOME/.ssh/authorized_keys
fi

# DELETE FILE
rm /home/[Link]

You might also like