admin 发表于 2016-10-20 16:21:49

linux开设无线热点的shell脚本

#!/bin/bash

#####################################
#Author: 定位                  #
#email: cnblackhat@qq.com       #
#QQ: 1074189538                     #
#Version: 1.0                       #
#Note: If you have some GOOD ideas  #
#      or advice, please mail me^^  #
#Modified: cnblackhat@ 2016-10-20 #
#          (cnblackhat@qq.com)    #
#####################################

#####################################
ShareNet=eth0
Wmode=ad-hoc
Wchannel=auto
Wessid=WiFi-Network
WInterface=$1
# the length of encrypted key is determined by device,
# running `sudo iwlist interface keys` to check
# the supported length
WPasskey=
Wkey=off
Igateway=192.168.2.254
Inetmask=255.255.255.0
Inetwork=192.168.2.0
DhcpRangeMin=192.168.2.10
DhcpRangeMax=192.168.2.105
#####################################

adhocFolder=/etc/adhoc
dnsmasqFile=$adhocFolder/dnsmasq.conf
resolvFile=/etc/resolv.conf
dnsmasqPid=/var/run/dnsmasq.pid
dnsmasqLeases=/var/run/dnsmasq.leases

# run command and print error message
function run_command
{
  local command="$1"
  local msg
  msg=$(${command} 2>&1)
  sleep 0.5
  if [ $? -ne 0 ]; then
    echo -e "    [ \033"
    echo    "    ${msg}"
    exit 1
  else
    echo -e "    [ GOOD ]"
  fi
}

# start Ad-hoc
function adhoc_start
{
  echo "Starting Ad-hoc..."
  check_dnsmasq
  sleep 0.5

  echo -n "Setting $WInterface gateway[$Igateway] and netmask[$Inetmask]..."
  run_command "ifconfig $WInterface $Igateway netmask $Inetmask"

  echo -n "Setting down $WInterface..."
  run_command "ifconfig $WInterface down"
  # only is wlan0 shut down, you can set mode
  echo -n "Setting $WInterface mode[$Wmode]..."
  run_command "iwconfig $WInterface mode $Wmode"

  echo -n "Setting $WInterface essid[$Wessid]..."
  run_command "iwconfig $WInterface essid $Wessid"

  echo -n "Setting $WInterface channel[$Wchannel]..."
  run_command "iwconfig $WInterface channel $Wchannel"

  if [ -n "$WPasskey" ]; then
    Wkey="restricted"
    echo -n "Setting $WInterface password[$WPasskey]..."
    run_command "iwconfig $WInterface key sWPasskey"
  else
    Wkey="off"
  fi

  echo -n "Setting $WInterface key type[$Wkey]..."
  run_command "iwconfig $WInterface key $Wkey"
  if [ "$Wkey" = "off" ]; then
    echo -e "    [ \033"
  fi

  echo -n "Setting up $WInterface..."
  run_command "ifconfig $WInterface up"

  echo "Setting iptable:"
  #remove the old rules
  echo -n "    remove old rules..."
  iptables -N wireless-adhoc
  iptables -F wireless-adhoc
  iptables -t nat -F PREROUTING
  iptables -t nat -F POSTROUTING
  iptables -t nat -F
  echo "    [ GOOD ]"
  #bring up the NAT rules
  echo -n "    bring up NAT rules..."
  iptables -A wireless-adhoc -m state --state ESTABLISHED,RELATED -j ACCEPT
  iptables -A wireless-adhoc -s $Inetwork/24 -j ACCEPT
  iptables -A wireless-adhoc -p 47 -j ACCEPT
  iptables -A wireless-adhoc -j DROP
  iptables -A FORWARD -m state --state INVALID -j DROP
  iptables -A FORWARD -j wireless-adhoc
  iptables -t nat -I POSTROUTING -s $Inetwork/24 -j MASQUERADE
  echo "    [ GOOD ]"

  sh -c "echo 1 > /proc/sys/net/ipv4/ip_forward"

  echo -n "Starting DNS and DHCP server..."
  run_command "dnsmasq -i $WInterface \
                        --resolv-file=$resolvFile \
                        --conf-file=$dnsmasqFile"
  echo "Completed!^^"
}

function wait_a_moment
{
  sleep 0.5
  echo -n "."
}

# stop Ad-hoc
function adhoc_stop
{
  echo "Stopping adhoc ..."
  sh -c "echo 0 > /proc/sys/net/ipv4/ip_forward"
  echo -n "."
  # only is wlan0 shut down, you can set mode
  ifconfig $WInterface down
  wait_a_moment

  # stop adhoc mode
  iwconfig $WInterface mode managed
  wait_a_moment

  iwconfig $WInterface key off
  wait_a_moment

  iwconfig $WInterface essid any
  wait_a_moment

  # remove iptabled rules
  iptables -D FORWARD -j wireless-adhoc
  iptables -D FORWARD -m state --state INVALID -j DROP
  iptables -F wireless-adhoc
  iptables -X wireless-adhoc
  iptables -t nat -F PREROUTING
  iptables -t nat -F POSTROUTING
  iptables -t nat -F
  wait_a_moment

  if [ -f $dnsmasqPid ]; then
    dnsmasqID=`cat $dnsmasqPid`
    kill $dnsmasqID
  fi
  if [ -f $dnsmasqLeases ]; then
    rm $dnsmasqLeases
  fi
  echo -e "\nWifi ad-hoc now stopped"
}

function adhoc_restart
{
  echo "Now, resart Ad-hoc ..."
  adhoc_stop
  sleep 1
  adhoc_start
}
# check dnsmasq.conf
function check_dnsmasq
{

  if [ -f $dnsmasqPid ]; then
    echo "DHCP server is running!"
    echo "Now, restart Ad-hoc"
    adhoc_stop
  fi
  if [ ! -d $adhocFolder ]; then
    mkdir $adhocFolder
  fi

  if [ ! -f $dnsmasqFile ]; then
    echo "$dnsmasqFile is not exist, now building."

    echo "dhcp-authoritative" > $dnsmasqFile
    echo "dhcp-range=$DhcpRangeMin,$DhcpRangeMax,12h" >> $dnsmasqFile
    echo "dhcp-leasefile=$dnsmasqLeases" >> $dnsmasqFile
    echo "pid-file=$dnsmasqPid" >> $dnsmasqFile
    echo "user=root" >> $dnsmasqFile
    echo "no-negcache" >> $dnsmasqFile
  fi
}

# is super user?
function super_user
{
  if [ "$UID" = "0" ]; then
    return 0
  else
    return 1
  fi
}

# check whether encrypted key has supported length or not
function check_key_size
{
  local key=$1
  if [[ -n $key ]]; then
    local key_bit_sizes=$(iwlist ${WInterface} keys \
                        | grep "sizes" \
                        | sed "s/.*: *\(.*\)bits/\1/; s/,/ /")
    local key_byte_sizes
    local wrong_key=1
    local size
    for size in ${key_bit_sizes}; do
      key_byte_sizes="${key_byte_sizes},$((size/8))"
      if ((${#key} == ${size}/8)); then
        wrong_key=0
      fi
    done
    if ((${wrong_key})); then
      echo "Encrypted key must have ${key_byte_sizes#,} characters."
      exit 1
    fi
  fi
}

# check whether the specified wifi interface is valid or not
function check_wifi_interface
{
  local wifi="$1"
  local interfaces=$(iwconfig 2> /dev/null \
                          | grep "ESSID" \
                          | sed "s/^\([^ ]\{1,\}\).*/\1/")
  if [ -z "${interfaces}" ]; then
    echo "It seems that you haven't any WiFi device!"
    exit 1
  fi
  local inf
  for inf in ${interfaces}; do
    if [ "${wifi}" = "${inf}" ]; then
      return 0
    fi
  done
  echo "Maybe \"${wifi}\" is not a WiFi device which you have."
  echo "Detected WiFi devices following:"
  for inf in ${interfaces}; do
    echo -n "    ${inf}"
  done
  echo
  exit 1
}

function usage
{
  local program=$(echo $0 | sed "s/.*\/\([^\/]*\)$/\1/")
  echo "             Wifi Ad-hoc Control"
  echo
  echo "Usage:"
  echo "    ${program} interface (start|stop|restart)"
  echo
  echo "Description:"
  echo "    essid    The name of your network, default \"$Wessid\"."
  echo "    key      The encryption key of your network,"
  echo "             you can use any ASCII string,"
  echo "             if not set, your network will be non-encrypted."
  echo "    start    Start creating Wifi Ad-hoc Network."
  echo "    stop     Stop Wifi Ad-hoc Network."
  echo "    restart  Restart creating Wifi Ad-hoc Network."
}

if [ $# -lt 1 ]; then
  usage
  exit 1
fi
if ! super_user ; then
  echo "Need super user permission!"
  exit 1
fi

check_wifi_interface "$WInterface"

shift
while [[ -n $1 ]]; do
  case $1 in
    essid)  shift
            Wessid=$1
            ;;
    key)    shift
            WPasskey=$1
            check_key_size "${WPasskey}"
            ;;
    stop)   Action="stop"
            ;;
    restart) Action="restart"
            ;;
    start) Action="start"
            ;;
    *)      usage
            exit 1
            ;;
  esac
  shift
done
case $Action in
  stop)     adhoc_stop
            ;;
  restart)  adhoc_restart
            ;;
  start)    adhoc_start
            ;;
  *)        usage
esac


页: [1]
查看完整版本: linux开设无线热点的shell脚本