ubuntu 建立ad-hoc共享无线网络

包含网卡/无线网的网络问题和ADSL/校园网/宽带拨号支持及代理/共享等网络使用问题
回复
头像
速腾1994
论坛版主
帖子: 17379
注册时间: 2008-11-01 20:43
系统: Arch+gnome

ubuntu 建立ad-hoc共享无线网络

#1

帖子 速腾1994 »

原文链接 http://www.chnlanker.com/unix-linux/3818.htmlhttps://gist.github.com/1117004
NetworkManager的 :用作热点 功能薄弱,老是不能获取地址,在我的电脑上有点鸡肋。不知在别人的电脑上使用效果如何?
使用了这个脚本后,手机马上就可以连接上网了。
我的是archlinux,运行脚本过程中提示:找不到 iwconfig dnsmasq。要自行安装 wireless_tools iw ? dnsmasq
脚本:

代码: 全选

#!/bin/bash

#####################################
#Author: Leon Lee                   #
#email: [email protected]       #
#QQ: 730395591                      #
#Version: 1.0                       #
#Note: If you have some good ideas  #
#	or advice, please mail me^^ #
#####################################

#####################################
ShareNet=eth0
Wmode=ad-hoc
Wchannel=auto
Wessid=Leon_in_GuangZhou #ssid自己改
WInterface=wlan0
WPasskey=androidworkab   #密码自己改
Wkey=restricted
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

# start Ad-hoc
function adhoc_start
{
	echo "Starting Ad-hoc..."
	check_dnsmasq
	sleep 1
	sudo ifconfig $WInterface down
	echo -n "Setting $WInterface gateway and netmask."
	sudo ifconfig $WInterface $Igateway netmask $Inetmask
	sleep 1
	echo -n "    [ good ]"
	echo

	echo -n "Setting up $WInterface ..."
	sudo ifconfig $WInterface up
	sleep 1
	echo -n "    [ good ]"
	echo

	echo -n "Setting $WInterface mode..."
	sudo iwconfig $WInterface mode $Wmode
	sleep 1
	echo -n "    [ good ]"
	echo

	echo -n "Setting $WInterface essid..."
	sudo iwconfig $WInterface essid $Wessid
	sleep 1
	echo -n "    [ good ]"
	echo

	echo -n "Setting $WInterface cannel..."
	sudo iwconfig $WInterface channel $Wchannel
	sleep 1
	echo -n "    [ good ]"
	echo

	echo -n "Setting $WInterface password..."
	sudo iwconfig $WInterface key s:$WPasskey
	sleep 1
	echo -n "    [ good ]"
	echo

	echo -n "Setting $WInterface key type..."
	sudo iwconfig $WInterface key $Wkey
	sleep 1
	echo -n "    [ good ]"
	echo
	echo
	echo -n "Setting iptable..."
	sleep 1
	#remove the old rules
	sudo iptables -N wireless-adhoc
  	sudo iptables -F wireless-adhoc
  	sudo iptables -t nat -F PREROUTING
  	sudo iptables -t nat -F POSTROUTING
  	sudo iptables -t nat -F
	#bring up the NAT rules
	sudo iptables -A wireless-adhoc -m state --state ESTABLISHED,RELATED -j ACCEPT
	sudo iptables -A wireless-adhoc -s $Inetwork/24 -j ACCEPT
	sudo iptables -A wireless-adhoc -p 47 -j ACCEPT
	sudo iptables -A wireless-adhoc -j DROP
	sudo iptables -A FORWARD -m state --state INVALID -j DROP
	sudo iptables -A FORWARD -j wireless-adhoc
	sudo iptables -t nat -I POSTROUTING -s $Inetwork/24 -j MASQUERADE
	sleep 1
	sudo sh -c "echo 1 > /proc/sys/net/ipv4/ip_forward"
	echo -n "."
	sleep 1
	sudo dnsmasq -i $WInterface --resolv-file=$resolvFile --conf-file=$dnsmasqFile
	echo
	echo "Success!^^"
}

# stop Ad-hoc
function adhoc_stop
{
	echo "Stopping adhoc ..."
	sudo sh -c "echo 0 > /proc/sys/net/ipv4/ip_forward"
	echo -n "."
	# stop adhoc mode
	sudo ifconfig $WInterface mode managed
	sleep 1
	echo -n "."
	sudo ifconfig $WInterface key off
	sleep 1
	echo -n "."
	sudo ifconfig $WInterface essid any
	sleep 1
	echo -n "."
	sudo ifconfig $WInterface down
	sleep 1
	echo -n "."
	# remove iptabled rules
	sudo iptables -D FORWARD -j wireless-adhoc
	sudo iptables -D FORWARD -m state --state INVALID -j DROP
	sudo iptables -F wireless-adhoc
	sudo iptables -X wireless-adhoc
	sudo iptables -t nat -F PREROUTING
	sudo iptables -t nat -F POSTROUTING
	sudo iptables -t nat -F
	sleep 1
	if [ -f $dnsmasqPid ]; then
		dnsmasqID=`cat $dnsmasqPid`
		kill $dnsmasqID
		sleep 1
	fi
	if [ -f $dnsmasqLeases ]; then 
		rm $dnsmasqLeases
	fi
	echo
	echo "Wifi ad-hoc now stopped"
}

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

	if [ -f $dnsmasqPid ]; then
		echo "Dhcp is running!"
		echo "Now, restart Ad-hoc"
		adhoc_stop
		sleep 1
	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
}

function usage
{
	echo "Wifi Ad-hoc control"
	echo "$1 [-h][-s]"
	echo "Default is start ad-hoc"
	echo "-h show the usage"
	echo "-s stop ad-hoc"
	echo "-r restart ad-hoc"
}

if ! super_user ; then
echo "Need super user permission!"
exit 1
fi
if [ $# -lt 1 ]; then
adhoc_start
elif [ "$1" = "-h" ]; then
usage
elif [ "$1" = "-s" ]; then
adhoc_stop
elif [ "$1" = "-r" ]; then
adhoc_restart
else
echo "Input error!"
echo "$1 -h gives usage information."
fi 
flay
帖子: 211
注册时间: 2010-01-25 9:27

Re: ubuntu 建立ad-hoc共享无线网络

#2

帖子 flay »

试试,正需要这货。。
头像
枫叶饭团
帖子: 14683
注册时间: 2010-06-16 1:05
系统: Mac OS X
来自: Tencent
联系:

Re: ubuntu 建立ad-hoc共享无线网络

#3

帖子 枫叶饭团 »

好东西
头像
eexpress
帖子: 58428
注册时间: 2005-08-14 21:55
来自: 长沙

Re: ubuntu 建立ad-hoc共享无线网络

#4

帖子 eexpress »

这干嘛的。。。搞这么复杂,也不精简下。。
老是不能获取地址
直接建立不行,估计怪jpwt
● 鸣学
头像
Cherrot
帖子: 981
注册时间: 2011-04-03 10:01
系统: Ubuntu 12.XX
来自: 帝都
联系:

Re: ubuntu 建立ad-hoc共享无线网络

#5

帖子 Cherrot »

mark
Ubuntu技巧汇总http://wiki.ubuntu.org.cn/UbuntuSkills --你遇到的各种问题可能已经在里面了
我的技术博客http://www.cherrot.com
Code tells you how, comments should tell you why.
头像
速腾1994
论坛版主
帖子: 17379
注册时间: 2008-11-01 20:43
系统: Arch+gnome

Re: ubuntu 建立ad-hoc共享无线网络

#6

帖子 速腾1994 »

eexpress 写了:这干嘛的。。。搞这么复杂,也不精简下。。
老是不能获取地址
直接建立不行,估计怪jpwt
jpwt?极品问题?
直接建立真的不行,我还以为是我的网卡不支持,我换一个8187l的网卡也是这样,我几个朋友的笔记本还是这样。。。。。
头像
速腾1994
论坛版主
帖子: 17379
注册时间: 2008-11-01 20:43
系统: Arch+gnome

Re: ubuntu 建立ad-hoc共享无线网络

#7

帖子 速腾1994 »

还是脚本爽快 :em06
头像
jouyouwen
帖子: 96
注册时间: 2011-02-13 15:50
系统: Deepin
联系:

Re: ubuntu 建立ad-hoc共享无线网络

#8

帖子 jouyouwen »

dnsmasq 失败,如下:

代码: 全选

Setting iptable...iptables: Chain already exists.
.
dnsmasq: failed to create listening socket for port 53: 地址已在使用
回复