Welcome to HBH! If you had an account on hellboundhacker.org you will need to reset your password using the Lost Password system before you will be able to login.

Bash Script


chompish's Avatar
Member
0 0

Not sure if this should be in here, but i couldn't see anywhere else it could go.

I'm trying to write a script that would set the IP address of the network card. It works, however i'm wondering if there is a more efficient way to code this?

What i've got so far seems like a lot of code for what it is doing.

Code bellow:

STRING="Working"
RECEVIED="1 received"
PINGFILE="./ping.txt"
echo $STRING

#Try DHCP
dhclient

#ping external address and output to file
ping -c 1 www.google.com > /root/Desktop/ping.txt

#check for PING reply from file
if [[ -n $(grep "$RECEVIED" /root/Desktop/ping.txt) ]]; then ./Desktop/Start_Stuff.sh && exit
else echo "shit" && # Set IP address
ifconfig eth0 down
ifconfig eth0 192.168.1.252
ifconfig eth0 up
fi
#ping external address and output to file
ping -c 1 www.google.com > /root/Desktop/ping.txt

#check for PING reply from file
if [[ -n $(grep "$RECEVIED" /root/Desktop/ping.txt) ]]; then IPADDRESS="192.168.1.252" && ./Desktop/Start_Stuff.sh && exit
else echo "shit" && # Set IP address
ifconfig eth0 down
ifconfig eth0 192.168.0.252
ifconfig eth0 up
fi

#ping external address and output to file
ping -c 1 www.google.com > /root/Desktop/ping.txt

#check for PING reply from file
if [[ -n $(grep "$RECEVIED" /root/Desktop/ping.txt) ]]; then IPADDRESS="192.168.0.252" && ./Desktop/Start_Stuff.sh && exit
else echo "shit" && # Set IP address
ifconfig eth0 down
ifconfig eth0 10.0.0.252
ifconfig eth0 up
fi

#ping external address and output to file
ping -c 1 www.google.com > /root/Desktop/ping.txt

#check for PING reply from file
if [[ -n $(grep "$RECEVIED" /root/Desktop/ping.txt) ]]; then IPADDRESS="10.0.0.252" && ./Desktop/Start_Stuff.sh && exit
else echo "shit" && # Set IP address
ifconfig eth0 down
ifconfig eth0 10.0.1.252
ifconfig eth0 up
fi

ping -c 1 www.google.com > /root/Desktop/ping.txt

if [[ -n $(grep "$RECEVIED" /root/Desktop/ping.txt) ]]; then IPADDRESS="10.0.1.252" && ./Desktop//Start_Stuff.sh && exit
else echo "shite, its no working" &&

exit

Yes i know i have spelt received wrong :p

Any help would be appreciated


chompish's Avatar
Member
0 0

Have a really posted a topic that no-one is going to give abuse on? :D

Or give any input for that matter :right:


starofale's Avatar
Member
0 0

Use functions and loops to reduce repeated code.


Arabian's Avatar
Member
0 0

starofale wrote: Use functions and loops to reduce repeated code.

^perfect response.


Mb0742's Avatar
Ultimate Headshot
0 0

Oh wow bash has functions. Cheers to this thread.

Oh wait I'm retarded, I've used fork bombs and shit. :angry:


chompish's Avatar
Member
0 0

thanks kindly :)


Death_metal666's Avatar
Member
0 0

starofale wrote: Use functions and loops to reduce repeated code. :D