busybox network configuration script error - linux

Hello I have the script in the start up but I don't get why it is showing error on execution
#!/bin/sh
# Starting the network interface
PATH="/sbin:/bin:/usr/bin:/usr/sbin"
FILENAME="/etc/ipconf"
count=0
while read LINE
do
ipValues[count]=$(echo $LINE | awk -F'=' '{print $2}')
count=`expr $count + 1`
done < $FILENAME
echo "Setting up IP Address"
ifconfig eth0 up
ifconfig eth0 ${ipValues[0]} netmask ${ipValues[1]}
echo "IP :: ${ipValues[0]} SUBNET MASK :: ${ipValues[1]}"
route add default gw ${ipValues[2]}
echo "Default Gateway :: ${ipValues[2]}"
echo "Network configured properly"
exit 0
Here is my ipconf file
IPADDRESS=192.168.1.13
SUBNETMASK=255.255.255.0
GATEWAY=192.168.1.220
And here is my scripts error
ipValues[count]=192.168.1.13 Not found
ipValues[count]=255.255.255.0 Not found
ipValues[count]=192.168.1.220 Not found
Setting up IP Address
Line 20 syntax error: Bad substitution
My script is braking in line ifconfig eth0 ${ipValues[0]} netmask ${ipValues[1]}. Is this array assignment is correct or busybox scripts needs different approach?

You're right, busybox doesn't support the array syntax in your script.
In order to set the values, you might use
eval ipValues$count=$(echo $LINE | awk -F'=' '{print $2}')
and to read the variables
ifconfig eth0 ${ipValues0} netmask ${ipValues1}
echo "IP :: ${ipValues0} SUBNET MASK :: ${ipValues1}"
route add default gw ${ipValues2}
echo "Default Gateway :: ${ipValues2}"

Related

Printing IP address of Nic card to bash using script

currently I am working on a school assignment on Linux using Vim where I have to write a script that displays the user that is currently logged in, the time and date, and list only the IP address of the Nic card. I have everything working except for the IP address part. If anyone could help I would appreciate it greatly.
Edit to include the code I have at the moment.
#!/bin/bash
Time=$(date)
IP=$(ifconfig ens33)
echo "The following user is currently logged in $USER"
echo ""
echo "The current time is $Time"
echo ""
echo "The IP information is $IP"
You can filter the result of ifconfig using awk like this (IPv4):
$ ifconfig ens33 | awk '/inet addr/{print substr($2, 6)}'
Result:
10.10.xx.xx
inet addr: represents IPv4 address.
inet6 addr: represents IPv6 address.
this line
IP=$(ifconfig ens33| grep inet | sed 's/ */ /' | cut -d" " -f3)

Shell script to find the IP address of an Virtual machine created by using KVM/virsh command

I am looking for the shell script to find the IP address of a virtual machine created by using KVM/VIRSH.
I used the following steps to get it so, but couldn´t able to find it.
Ping the IP addresses in range
2.Use Virsh list command to list all the active VM
3.use Virsh dumpxml domainname and project the xml of vm
use grep command and fetch the Hardware address of vm
5.Display the hardware address of each vm
Now I would like to add one more step like fetching the IP address for that particular Hardware address using ¨arp -ne¨
I couldn´t able to figure out how to add the part.
could any one help me on this.
for i in {1..150}
do
ping -c 1 -n -q -r -t 1 -s 1 -W 1 192.168.1.$i > /dev/null &
done
for name in `virsh list | grep running | awk '{ print $2 }'`
do
# printf "\n$name\n "
arp -e | grep "`virsh dumpxml $name | grep "mac address"|sed "s/.*'\(.*\)'.*/\1/g"`" |
awk '{ printf $1 ; printf " " ; printf $3 "\n" }'
done
current output:
$ ./virshshell.sh
vm2 52:54:00:4b:7f:41
vm3 52:54:00:0e:4c:42
The output I am expecting is
$ ./virshshell.sh
vm2 52:54:00:4b:7f:41 192.*.*.*
vm3 52:54:00:0e:4c:42 192.*.*.*
Use nmap to do network discovery instead of ping. It can do what ping does but also much more, plus it runs way faster and takes care of the network-scope scanning that you're doing in your for loop.
$ nmap -T5 -n -PE 192.168.4.0/24 > /dev/null
$ ip neigh show | grep 192.168.4 | grep -v FAILED
192.168.4.92 dev eth0 lladdr 54:52:00:90:90:92 REACHABLE
192.168.4.11 dev eth0 lladdr fa:16:3e:fa:ac:07 REACHABLE
192.168.4.91 dev eth0 lladdr 54:52:00:90:90:91 REACHABLE
192.168.4.90 dev eth0 lladdr 54:52:00:90:90:90 REACHABLE

Linux/Unix check if VPN connection is Active/Up

I have a code which detects if OpenVPN connection is up or down:
if echo 'ifconfig tun0' | grep -q "00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00"
then
echo "VPN up"
else
echo "VPN down"
fi
exit 0
now I'm trying to re-write the code to work with PPTP or IPSEC connection. I've tried to do:
if echo 'ifconfig ppp0' | grep -q "00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00"
or the same with ipsec but does not work. Is there any other way to detect PPTP or IPSEC connection?
That echo statement is erroneous. As #unwind says, the single quotes (') should be backtics (`). Your current code is sending the literal value ifconfig ppp0 to grep, which doesn't do anything useful.
But you don't actually need the backtics, either. You can just send the output of ifconfig to grep directory; using echo doesn't get you anything:
if ifconfig ppp0 | grep -q "00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00"; then
echo ppp connection is up
fi
The following script will:
Run the ISPConnectivity.sh script every 5 minutes. This will mean that the VPN tunnel will not be down for more than 5 minutes.
Check if the tun interface is down, and start the vpn script if it is.
Check connectivity if the tun0 interface is up. It does ping tests on 2 Public IPs (if I get even a single response from 1 of the IPs tested, I consider this a success ), and all have to fail to run the vpn script. I ran ping tests on multiple hosts to prevent the vpn script from starting in case the ping test failed on 1 IP.
Send all failure output to a file in my home directory. I do not need to see if any test succeeded.
Contents of sudo crontab:
*/5 * * * * /home/userXXX/ISPConnectivity.sh >> /home/userXXX/ISPConnectivity.log 2>&1
Contents of ISPConnectivity.sh script:
#!/bin/bash
# add ip / hostname separated by white space
#HOSTS="1.2.3.4"
HOSTS="8.8.8.8 4.2.2.4"
# no ping request
totalcount=0
COUNT=4
DATE=`date +%Y-%m-%d:%H:%M:%S`
if ! /sbin/ifconfig tun0 | grep -q "00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00"
then
echo $DATE tun0 down
sudo /home/userXXX/startVPN.sh start
else
for myHost in $HOSTS;
do
count=`ping -c $COUNT $myHost | grep 'received' | awk -F',' '{ print $2 }' | awk '{ print $1 }'`
totalcount=$(($totalcount + $count))
done
if [ $totalcount -eq 0 ]
then
echo $DATE $totalcount "fail"
sudo /home/userXXX/startVPN.sh start
#else
# echo $DATE $totalcount "pass"
fi
fi
You can also check with the nmcli command, to check if VPN is running or not.
nmcli c show --active | grep vpn
I'm actually looking into more flexible solution eg:
MyIP=$(curl http://api.ipify.org/?format=text)
if [ "$MyIP" != "MYORYGINALIP" ]
then
echo "IPSEC VPN is Running - " $MyIP
else
echo "IPSEC VPN is Not Running - " $MyIP
fi
exit 0
what about that? can I improve it any way?
ip route list table 220 if Ip address shown -> VPN connection established, none -> no VPN
or
if [ "0" == ifconfig | grep wlan0 | wc -l ]; then echo "NO wlan0 has no VPN"; else echo "YES wlan0 has VPN"; fi

Bash script to bring up and down a wireless interface on loop

If I have found the following code from another post regarding an Ethernet connection. I want to do the same thing with the wireless network card which is wlan0 on my machine. I figured that I would try swapping the iface to wlan0 and then put the ping as my router however it hasn't worked.
#!/bin/bash
timeout=5 # delay between checks
pingip='8.8.8.8' # what to ping
iface="eth0"
LOG_FILE="/var/log/syslog"
isdown=0 # indicate whether the interface is up or down
# start assuming interface is up
while true; do
LOG_TIME=`date +%b' '%d' '%T`
if ping -q -c 2 "$pingip" >> /dev/null ; then # ping is good - bring iface up
if [ "$isdown" -ne 0 ] ; then
ifup $iface && isdown=0
printf "$LOG_TIME $0: Interface brought up: %s\n" "$iface" | tee -a $LOG_FILE
fi
else # ping is bad - bring iface down
beep -f 4000
if [ "$isdown" -ne 1 ] ; then
ifdown $iface && isdown=1
printf "$LOG_TIME $0: Interface brought down: %s\n" "$iface" | tee -a $LOG_FILE
fi
fi
sleep "$timeout"
done
Am I barking up the wrong tree here, or do I just need to edit this a bit further. My bash skills are subpar I'm afraid.
I'm pretty sure that ifconfig presents on your system
determine the name of your interface ls /sys/class/net, likely it's wlan0 or wlp2s0
change ifup $iface and ifdown $iface to ifconfig $iface up and ifconfig $iface down respectively

Bash Loops: How to execute once, then move to the next iteration

I've never actually had an instance where this was required, but here I am...
What I've (unfortunately) got is an output of netstat -nr (almost 6000 individual static routes).
I need to take that file, and eventually translate each line into a new 'ip route add' command once I've got this working.
Here's my failcode (echos at the bottom are for testing -> I'd want to substitute them for ip route add once this works):
num=`cat $logfile | wc -l`
echo $num
echo " "
for ((i=0; i<=$num; i++))
do
dst=$(awk '{print$1}' $logfile)
gw=$(awk '{print$2}' $logfile)
mask=$(awk '{print$3}' $logfile)
echo $dst
echo $gw
echo $mask
echo " "
done
The output, instead of looking like:
Destination Gateway Netmask
looks like:
Destination Destination Destination Destination
Gateway Gateway Gateway Gateway
Netmask Netmask Netmask Netmask
How do I make it so that each time it runs, each output is a single DST/GW/netmask that I can feed into a command?
Thanks!
Your loops are inside out. Your awk commands process the whole file each time and they're done.
If you need the values in variables replace almost all of your script with this:
#!/bin/bash
while read -r dst gw mask _
do
echo "$dst $gw $mask" # substitute your command here
done < "$logfile"
The answer lies in AWK:
awk '{print "echo",$1,$2,$3}' $logfile | sh -x
Now replace echo with whatever command you need to run

Resources