java实现配置有线和无线网络笔记(linux(debian))欢迎大家拍砖

java实现网络配置,主要是往shell传递命令,例如:
Runtime.getRuntime().exec("dhclient ....");


有线wlan,这里有线设备名称为eth0(有关有线和无线设备可以在目录/sys/class/net/目录下看到)
主要配置文件:
/etc/network/interfaces 这个文件是保存ip,netmask,gateway信息的(包括静态和动态),实时生效的
/etc/resolv.conf 这个文件是保存DNS服务器信息的,实时生效
/etc/hosts 、/etc/hostname 在基于debian的linux系统中,修改主机名称,需要修改这两个文件,必须重启才能生效


设置自动获取IP
sudo ifconfig eth0 down
sudo ifconfig eth0 up
sudo dhclient eth0

执行完上述命令以后,查看/etc/network/interfaces :
# Used by ifup(8) and ifdown(8). See the interfaces(5) manpage or
# /usr/share/doc/ifupdown/examples for more information.

# The loopback network interface
auto lo
iface lo inet loopback

# The primary network interface

auto eth0
iface eth0 inet dhcp








#iface eth0 inet static


查看/etc/resolv.conf :
nameserver 192.168.200.200
nameserver 192.168.200.240

修改主机名:
同时修改/etc/hosts 、/etc/hostname这两个文件即可


设置自定义IP
sudo ifconfig eth0 down
sudo ifconfig eth0 up
sudo ifconfig eth0 192.168.10.222 netmask 255.255.255.0
sudo route add default gw 192.168.10.1;

执行完上述命令以后,查看/etc/network/interfaces :
# Used by ifup(8) and ifdown(8). See the interfaces(5) manpage or
# /usr/share/doc/ifupdown/examples for more information.

# The loopback network interface
auto lo
iface lo inet loopback"

# The primary network interface

#auto eth0
#iface eth0 inet dhcp








iface eth0 inet static

address 192.168.10.222
netmask 255.255.255.0
gateway 192.168.10.1

auto eth0


关于无线WIFI,设备名称ra0:

其实配置IP,DNS,流程和有线是一样的,主要是和无线路由器连接

用命令sudo iwlist ra0 scan 即可搜索出范围内的所有无线网络名称
我们可以使用命令 sudo iwlist ra0 scan >/tmp/wireless.txt
通过查看wireless.txt我们需要用到几个关键值:
Address , ssid ,Encryption


如果没有加密的无线网络(名称为test1)

sudo eth0 down
sudo ra0 down
sudo killall wpa_supplicant
sudo ra0 up
sudo iwconfig ra0 essid test1
sudo dhclient ra0

即可

假设:如果WEP 64/128-bit Hex 加密的无线网络(名称为test2),密码为1234567890 ,ADDRESS为 E0:15:22:42:28

sudo eth0 down
sudo ra0 down
sudo killall wpa_supplicant
sudo ra0 up
sudo iwconfig ra0 essid test2

1:如果Encryption 职位on
sudo iwconfig ra0 key open
sudo iwconfig ra0 key 1234567890
sudo iwconfig ra0 ap E0:15:22:42:28
sudo iwconfig ra0 rate auto
sudo dhclient ra0

2:如果Encryption 职位off
sudo iwconfig ra0 key off
sudo iwconfig ra0 ap E0:15:22:42:28
sudo iwconfig ra0 rate auto
sudo dhclient ra0

=========================================================================================
假设:如果WEP 64/128-bit Ascii 加密的无线网络(名称为test2),密码为1234567890 ,ADDRESS为 E0:15:22:42:28

sudo eth0 down
sudo ra0 down
sudo killall wpa_supplicant
sudo ra0 up
sudo iwconfig ra0 essid test2

1:如果Encryption 职位on
sudo iwconfig ra0 key open
sudo iwconfig ra0 key s:1234567890
sudo iwconfig ra0 ap E0:15:22:42:28
sudo iwconfig ra0 rate auto
sudo dhclient ra0

2:如果Encryption 职位off
sudo iwconfig ra0 key off
sudo iwconfig ra0 ap E0:15:22:42:28
sudo iwconfig ra0 rate auto
sudo dhclient ra0

=========================================================================================
假设:如果WPA Personal 加密的无线网络(名称为test3),密码为1234567890 ,ADDRESS
需要将:
network={
ssid=test3
proto=WPA
key_mgmt=WPA-PSK
pairwise=CCMP TKIP
group=CCMP TKIP WEP104 WEP40
psk=1234567890
}


写入/ect/wpa_supplicant.conf文件中,这个文件原来在/etc下是不存在的

sudo wpa_supplicant -Bw -D wext -i ra0 -c /etc/wpa_supplicant.conf
或者低版本的wpasupplicant工具使用这个命令:
sudo wpa_supplicant -B -D wext -i ra0 -c /etc/wpa_supplicant.conf
sudo dhclient ra0


*********************************************


由于需求原因,需要在matchbox里安装resolvconf:
这就产生一个问题,如果指定静态IP,/etc/resolv.conf的内容会变成
# Dynamic resolv.conf(5) file for glibc resolver(3) generated by resolvconf(8)
# DO NOT EDIT THIS FILE BY HAND -- YOUR CHANGES WILL BE OVERWRITTEN
然后修改/etc/resolv.conf:
nameserver 192.168.200.240
nameserver 192.168.200.234

重启电脑以后还是恢复成:
[color=red]# Dynamic resolv.conf(5) file for glibc resolver(3) generated by resolvconf(8)
# DO NOT EDIT THIS FILE BY HAND -- YOUR CHANGES WILL BE OVERWRITTEN [/color]


下边的方法是参照:http://ubuntuforums.org/archive/index.php/t-6704.html
原文给出的:
I have a nice bodgy solution which seems to stick :)
The file /etc/resolvconf/resolv.conf.d/base contains entries which will
always be added, regardless of what resolv.conf also adds. It's empty
by default. If you edit it:
[color=red]sudo gedit /etc/resolvconf/resolv.conf.d/base[/color]
And add lines for your nameserver(s):
nameserver xxx.xxx.xxx.xxx
nameserver xxx.xxx.xxx.xxx
Then when resolvconf runs on startup, it will produce a resolv.conf file something like this:
# Dynamic resolv.conf(5) file for glibc resolver(3) generated by resolvconf(8)
# DO NOT EDIT THIS FILE BY HAND -- YOUR CHANGES WILL BE OVERWRITTEN
nameserver xxx.xxx.xxx.xxx
nameserver xxx.xxx.xxx.xxx
nameserver 127.0.0.1
nameserver yyy.yyy.yyy.yyy
...and that works.

[color=red]如果没有装resolvconf工具的需求的话,直接把resolvconf工具给卸载了,修改/etc/resolv.conf即可
[/color]


附件包含相关命令手册
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值