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

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

Related

wondering if i could apply loading while it performs, is this possible?

I am new to bash, aware that only one line is executed at a time. have a script i feel comfortable with. works fine, but i would like to add a loading sign at the top of the script based on the amount of the script that has run. Now this seems much more complex like some python scripting. but is it possible to do this in a bash script?
This would be extremely helpful as i tend to repeat usages of code through out other scripts. Im slow and i need reminders so i make scripts to automate all the tasks i need to do that involve remembering lines of commands.
Also i would be happy to take any suggestions or ideas to add into the code.
#!/bin/bash
BOLDY='\033[1;1;33m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
NC='\033[0m'
printf " ${GREEN} by MONEY ${CN}"
echo ''
printf " ${BOLDY} T H E M A C F L I P P E R${CN}"
echo ''
printf " ${YELLOW}starting mac address change for WLAN0 and ETH0${NC}\n"
echo ' ........................................................'
sleep 3
printf ${GREEN}
service NetworkManager stop
ifconfig wlan0 down
ifconfig wlan0 up
service NetworkManager start
printf " ${YELLOW} STARTING MAC CHANGER ${CN}\n"
printf " ${GREEN} -- ${CN}\n"
sleep 1
printf " ${GREEN} -- \n"
macchanger -s eth0
sleep 1
printf " ${GREEN} -- \n"
ifconfig eth0 down
macchanger -r eth0
sleep 1
printf " ${GREEN} -- \n"
ifconfig eth0 down
macchanger -s eth0
ifconfig eth0 up
ip link set eth0 up
printf " ${GREEN} -- \n"
sleep 1
printf "${YELLOW}ethernet MAC changed${NC}\n"
echo ''
printf ${GREEN}
macchanger -s eth0
sleep 6
printf "${YELLOW}starting wlan0 MAC change${NC}\n"
printf ${GREEN}
ifconfig wlan0 down
macchanger -s wlan0
sleep 1
macchanger -r wlan0
sleep 1
macchanger -s wlan0
ifconfig wlan0 up
ip link set wlan0 up
printf "${YELLOW}Wireless lan MAC changed${NC}\n"
echo ''
printf ${GREEN}
macchanger -s wlan0
sleep 6
clear
printf "${YELLOW}Wlan0 and Eth0 MAC addresses have been changed\n"
printf "${YELLOW}exiting program${NC}"
sleep 4
clear
exit
want to add some loading script near the top obviously this is related to time and not a specific instance in the script occurring, but for now it serves as an example.
echo LOADING
{
for pc in $(seq 1 100); do
echo -ne "$pc%\033[0k\r"
sleep 1
done
echo
}
exit
You could have this progress bar function:
#!/bin/sh
progbar()
{
command "$#" > /dev/null 2>&1 &
PID=$!
printf "["
# While process is running...
while kill -0 $PID 2> /dev/null; do
printf "▓"
sleep 1
done
printf "]"
}
Then call it with progbar some_long_running_command.
This will increase the size of a great progress bar like this:
[▓▓▓▓▓▓▓▓▓
Also, for this to work, all the running period of your process must be in one single call as an argument to progbar.
So, you should enclose your whole process to a function, namely long_function and then run progbar long_function.

How to find if IP is being used?

I have following IP in a server
192.168.1.1
192.168.1.2
192.168.1.3
192.168.1.4
I need to know if those IP are being used or not?
I could find using lsof but it's too slow
for ip in ${server_ip_list[#]}; do
lsof -i #$ip &>/dev/null && echo "$ip is used" || echo "$ip is unused"
done
Below command is faster but I need to specify port and protocol
true &>/dev/null </dev/tcp/192.168.1.1/80 && echo used || echo unused
I'm looking for a faster command using which I can determine if the IP is used regardless of protocol or port.
I'd appreciate if anyone could help with a solution or links where I can read about it.
Maybe you could use command ping to check if specific ip is being used (not disable ICMP in firewall). If you think for or while loop operation is too slow, you could try to use command parallel to execute command simultaneously.
Code sample like
ip_check(){
ip="$1"
ping -c 1 -W 1 "$ip" &> /dev/null
if [[ $? -eq 0 ]]; then
echo 'used'
else
echo 'unused'
fi
}
export -f ip_check
cat ip_file.txt | parallel -k -j 0 ip_check 2> /dev/null

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

Linux bash script that pings multiple IP addresses from a file

I have a file containing multiple hosts and IPs in the format above:
alpha, 192.168.1.1
beta, 192.168.1.2
gamma, 192.168.1.3
I am trying to create a script that says something like:
"Pinging hostname alpha"
ping 192.168.1.1
and jump to the next ip in the list.
I don't want the entire script, just some suggestions.
Thanks,
Alex
If you add a comma to the input field separator, it'll help parse the lines:
IFS=$IFS,
while read name ip; do
echo -n "Pinging hostname $name..."
ping -c2 "$ip" &>/dev/null && echo success || echo fail
done < /tmp/hosts
I'd read in the lines with read. You'll probably also want to give ping an option telling it how many times to ping. The default on most Linux systems for example is to ping forever, which doesn't seem like it would work well in your situation.
You could use AWK:
$ awk '{print "Pinging hostname "$1; system("ping -c 3 "$2) }' ips
Pinging hostname alpha,
PING 192.168.1.1 (192.168.1.1) 56(84) bytes of data.
You can also remove that comma if is it important to you:
$ awk '{sub(/,/,"");print "Pinging hostname "$1; system("ping -c 3 "$2) }' ips
Pinging hostname alpha
PING 192.168.1.1 (192.168.1.1) 56(84) bytes of data.
I might be a bit late to the party, but how about fping? Use -f to read from a file (requires sudo), or pipe the file with < (as suggested on the man page). It won't tell you "pinging alpha", but it will quickly tell you whether or not you can get in touch with the hosts.
Script for hosting 100+ hosts in same scheme like 192.168.xx.xxx
#!/bin/bash
for i in `seq ${2} ${3}`
do
ping -c 1 ${1}.${i} > /dev/null 2>&1
if [ $? -eq 0 ]; then
echo "${1}.${i} responded."
else
echo "${1}.${i} did not respond."
fi
done
command to ping the host
bash test.sh 192.168.1 0 100
Try this
#!/bin/bash
IPLIST="path_to_the_Ip_list_file"
for ip in $(cat $IPLIST)
do
ping $ip -c 1 -t 1 &> /dev/null
if [ $? -ne 0 ]; then
echo $ip ping faild;
else
echo $ip ping passed;
fi
done

busybox network configuration script error

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}"

Resources