关于路由表

  • route
    查看或修改路由表
route  [-v] [-A family] add [-net|-host] target [netmask Nm] [gw Gw] [metric N] [mss M] [window W] [irtt I] [reject] [mod] [dyn] [reinstate] [[dev] If]

常用参数:

        -n                                 //查看时不使用域名解析
        add                                //添加路由条目
        del                                //删除路由条目
        -net                               //添加网段
        gw                                 //gateway(网关)
        default                            //设定默认

例如:

[root@kyle ~]# route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
2.2.2.0         0.0.0.0         255.255.255.0   U     0      0        0 eth0
192.168.204.0   0.0.0.0         255.255.255.0   U     0      0        0 vmnet8
172.16.104.0    0.0.0.0         255.255.255.0   U     0      0        0 vmnet1
169.254.0.0     0.0.0.0         255.255.0.0     U     0      0        0 eth0
0.0.0.0         2.2.2.1         0.0.0.0         UG    0      0        0 eth0
[root@kyle ~]# route add default gw 2.2.2.254                          //添加默认网关
[root@kyle ~]# route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
2.2.2.0         0.0.0.0         255.255.255.0   U     0      0        0 eth0
192.168.204.0   0.0.0.0         255.255.255.0   U     0      0        0 vmnet8
172.16.104.0    0.0.0.0         255.255.255.0   U     0      0        0 vmnet1
169.254.0.0     0.0.0.0         255.255.0.0     U     0      0        0 eth0
0.0.0.0         2.2.2.254       0.0.0.0         UG    0      0        0 eth0
0.0.0.0         2.2.2.1         0.0.0.0         UG    0      0        0 eth0
[root@kyle ~]# route del default gw 2.2.2.254                          //删除默认网关
[root@kyle ~]# route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
2.2.2.0         0.0.0.0         255.255.255.0   U     0      0        0 eth0
192.168.204.0   0.0.0.0         255.255.255.0   U     0      0        0 vmnet8
172.16.104.0    0.0.0.0         255.255.255.0   U     0      0        0 vmnet1
169.254.0.0     0.0.0.0         255.255.0.0     U     0      0        0 eth0
0.0.0.0         2.2.2.1         0.0.0.0         UG    0      0        0 eth0
[root@kyle ~]# route add -net 192.168.2.0/24 eth0                          //添加网段
[root@kyle ~]# route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
192.168.2.0     0.0.0.0         255.255.255.0   U     0      0        0 eth0
2.2.2.0         0.0.0.0         255.255.255.0   U     0      0        0 eth0
192.168.204.0   0.0.0.0         255.255.255.0   U     0      0        0 vmnet8
172.16.104.0    0.0.0.0         255.255.255.0   U     0      0        0 vmnet1
169.254.0.0     0.0.0.0         255.255.0.0     U     0      0        0 eth0
0.0.0.0         2.2.2.1         0.0.0.0         UG    0      0        0 eth0
[root@kyle ~]# route del -net 192.168.2.0/24 eth0                          //删除网段
[root@kyle ~]# route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
2.2.2.0         0.0.0.0         255.255.255.0   U     0      0        0 eth0
192.168.204.0   0.0.0.0         255.255.255.0   U     0      0        0 vmnet8
172.16.104.0    0.0.0.0         255.255.255.0   U     0      0        0 vmnet1
169.254.0.0     0.0.0.0         255.255.0.0     U     0      0        0 eth0
0.0.0.0         2.2.2.1         0.0.0.0         UG    0      0        0 eth0
  • /proc/net/dev
    查看网络状况
[root@kyle ~]# cat /proc/net/dev
Inter-|   Receive                                                |  Transmit
face |bytes    packets errs drop fifo frame compressed multicast|bytes    packets errs drop fifo colls carrier compressed
    lo: 2474304    2458    0    0    0     0          0         0  2474304    2458    0    0    0     0       0          0
  eth0:332565751  977628    0    0    0     0          0         0 73458156  967301    0    0    0     0       0          0
  sit0:       0       0    0    0    0     0          0         0        0       0    0    0    0     0       0          0
vmnet1:       0       0    0    0    0     0          0         0        0      46    0    0    0     0       0          0
vmnet8:       0       0    0    0    0     0          0         0        0      46    0    0    0     0       0          0
  • netstat
    查看网络信息
netstat [address_family_options] [--tcp|-t] [--udp|-u] [--raw|-w] [--listening|-l] [--all|-a] [--numeric|-n] [--numeric-hosts][--numeric-ports][--numeric-
       ports] [--symbolic|-N] [--extend|-e[--extend|-e]] [--timers|-o] [--program|-p] [--verbose|-v] [--continuous|-c] [delay]

常用选项:

        -r                                        //查看路由
        -n                                        //不做域名解析
        -i                                        //查看接口
        -e                                        //查看详细
        -a                                        //查看所有端口状态
        -t                                        //查看tcp端口的信息
        -u                                        //查看utp端口的信息
        -l                                        //查看正在监听的端口
        -p                                        //查看正在使用此端口的进程

例如:

[root@kyle ~]# netstat -luntp
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address               Foreign Address             State       PID/Program name   
tcp        0      0 127.0.0.1:2208              0.0.0.0:*                   LISTEN      2538/hpiod         
tcp        0      0 0.0.0.0:609                 0.0.0.0:*                   LISTEN      2123/rpc.statd      
tcp        0      0 0.0.0.0:3306                0.0.0.0:*                   LISTEN      2662/mysqld         
tcp        0      0 0.0.0.0:111                 0.0.0.0:*                   LISTEN      2088/portmap        
tcp        0      0 127.0.0.1:631               0.0.0.0:*                   LISTEN      2565/cupsd         
tcp        0      0 127.0.0.1:25                0.0.0.0:*                   LISTEN      2695/sendmail: acce
tcp        0      0 127.0.0.1:2207              0.0.0.0:*                   LISTEN      2543/python         
tcp        0      0 :::80                       :::*                        LISTEN      2723/httpd         
tcp        0      0 :::22                       :::*                        LISTEN      2556/sshd           
udp        0      0 0.0.0.0:48958               0.0.0.0:*                               2820/avahi-daemon:  
udp        0      0 0.0.0.0:603                 0.0.0.0:*                               2123/rpc.statd      
udp        0      0 0.0.0.0:606                 0.0.0.0:*                               2123/rpc.statd      
udp        0      0 0.0.0.0:5353                0.0.0.0:*                               2820/avahi-daemon:  
udp        0      0 0.0.0.0:111                 0.0.0.0:*                               2088/portmap        
udp        0      0 0.0.0.0:631                 0.0.0.0:*                               2565/cupsd         
udp        0      0 :::59753                    :::*                                    2820/avahi-daemon:  
udp        0      0 :::5353                     :::*                                    2820/avahi-daemon:
  • tcpdump
    抓包工具
tcpdump [ -AdDeflLnNOpqRStuUvxX ] [ -c count ]
               [ -C file_size ] [ -F file ]
               [ -i interface ] [ -m module ] [ -M secret ]
               [ -r file ] [ -s snaplen ] [ -T type ] [ -w file ]
               [ -W filecount ]
               [ -E spi@ipaddr algo:secret,...  ]
               [ -y datalinktype ] [ -Z user ]
               [ expression ]

常用选项:

        -A                           //以ASCII形式显示所有数据包信息
        udp                          //查看udp数据包信息
        tcp                          //查看tcp数据包信息
        arp                          //查看arp数据包信息
        icmp                         //查看icmp数据包信息
        port                         //指定查看端口
        -w                           //将输出保存到文件
        -vv                          //查看详细信息

例如:

tcpdump dst port 80                  //查看目标为dst80端口的所有数据包信息
tcpdump dst 2.2.2.50                 //查看来自2.2.2.50目标为dst的所有数据包的信息

将VIM变成开发PHP的IDE

  • NERDTree

目录管理,树状导航

[root@kyle download]# wget -c http://www.vim.org/scripts/download_script.php?src_id=11500
[root@kyle download]# mkdir NERD_tree/
[root@kyle download]# mv NERD_tree.zip NERD_tree/
[root@kyle download]# cd NERD_tree/
[root@kyle NERD_tree]# unzip NERD_tree.zip
[root@kyle NERD_tree]# cp -rvf plugin/* ~/.vim/plugin/
[root@kyle NERD_tree]# cp -rvf doc/* ~/.vim/doc/
[root@kyle NERD_tree]# cp -rvf nerdtree_plugin/ ~/.vim/
[root@kyle NERD_tree]# vim /etc/vimrc
在最后添加:
nmap <F2> :NERDTree  <CR>
  • taglist

函数和变量标签

[root@kyle download]# wet -c http://www.vim.org/scripts/download_script.php?src_id=7701
[root@kyle download]# mkdir taglist/
[root@kyle download]# mv taglist_45.zip taglist/
[root@kyle download]# cd taglist/
[root@kyle taglist]# unzip taglist_45.zip
[root@kyle taglist]# cp -rvf plugin/* ~/.vim/plugin/
[root@kyle taglist]# cp -rvf doc/* ~/.vim/doc/
[root@kyle taglist]# vim /etc/vimrc
在最后添加:
nmap <F3> :Tlist  <CR>
  • php-doc

生成php注释文档

[root@kyle download]# wget -c http://www.vim.org/scripts/download_script.php?src_id=4666
[root@kyle download]# cp -rvf php-doc.vim ~/.vim/plugin/
[root@kyle download]# vim /etc/vimrc
在最后添加:
inoremap <C-P> <ESC>:call PhpDocSingle()<CR>i
nnoremap <C-P> :call PhpDocSingle()<CR>
vnoremap <C-P> :call PhpDocRange()<CR>
  • ACP

自动补齐

[root@kyle download]# wget -c http://www.vim.org/scripts/download_script.php?src_id=11894
[root@kyle download]# mkdir ACP
[root@kyle download]# cp -rvf vim-autocomplpop.zip ACP/
[root@kyle ACP]# unzip vim-autocomplpop.zip
[root@kyle ACP]# cp -rvf autoload/ ~/.vim/
[root@kyle ACP]# cp -rvf doc/* ~/.vim/doc/
[root@kyle ACP]# cp -rvf plugin/* ~/.vim/doc/
[root@kyle ACP]# cd /root/.vim/

上网去下载php_funclist.txt
本人是去phpdoc-doc-base上面复制粘贴到到~/.vim/php_funclist.txt

[root@kyle ACP]# vim /etc/vimrc
在最后添加:
set dictionary-=~/.vim/php_funclist.txt dictionary =~/.vim/php_funclist.txt
set complete-=k complete =k

Network Service

  • 检测网线是否连接
mii-tool  [-v, --verbose] [-V, --version] [-R, --reset] [-r, --restart] [-w, --watch] [-l, --log] [-A, --advertise=media,...]  [-F, --force=media] [inter-
       face ...]

例如:

[root@kyle /]# mii-tool
eth0: negotiated 100baseTx-FD, link ok
  • 查看或者临时配置网络
ifconfig interface [aftype] options | address ... 

例如:
将IP绑定到2.2.2.254,netmask设置为255.255.255.0(临时)

[root@kyle /]# ifconfig eth0 2.2.2.254 netmask 255.255.255.0
  • 启动网卡
ifup <device name>
  • 关闭网卡
ifdown <设备名>
  • 查看或修改主机名(临时)
hostname [-v] [-a] [--alias] [-d] [--domain] [-f] [--fqdn] [-i] [--ip-address] [--long] [-s] [--short] [-y] [--yp] [--nis] [-n] [--node]

例如:

[root@kyle /]# hostname kyle.redhat.com
[root@kyle /]# hostname
kyle.redhat.com
  • 修改主机名
    修改主机名步骤:
    1)hostname
    2)修改/etc/sysconfig/network中的HOSTNAME

例如:

[root@kyle /]# hostname kyle.redhat.com
[root@kyle /]# vim /etc/sysconfig/network                                //修改HOSTNAME=kyle.redhat.com
[root@kyle /]# cat /etc/sysconfig/network
NETWORKING=yes
NETWORKING_IPV6=no
HOSTNAME=kyle.redhat.com
  • 修改IP地址(永久)
/etc/sysconfig/network-scripts/ifcfg-ethX

例如:

[root@kyle /]# cat /etc/sysconfig/network-scripts/ifcfg-eth0
# Realtek Semiconductor Co., Ltd. RTL-8139/8139C/8139C 
DEVICE=eth0
BOOTPROTO=static              //static为静态IP,dhcp为自动获取
NETMASK=255.255.255.0         //子网掩码
GATEWAY=2.2.2.1               //网关
IPADDR=2.2.2.254              //绑定的IP地址
HWADDR=00:27:19:96:B0:CD      //MAC地址
ONBOOT=yes                    //重启网络服务时自动启动

注意:修改完配置文件要重启网络服务(service network restart)

  • 本地DNS解析文件
/etc/hosts

格式:

IP                                        servername

例如:

[root@kyle /]# cat /etc/hosts
# Do not remove the following line, or various programs
# that require network functionality will fail.
127.0.0.1               localhost.localdomain localhost
::1             localhost6.localdomain6 localhost6

2.2.2.254       kyle.redhat.com kyle
2.2.2.192       teacher.redhat.com      teacher
  • 配置DNS服务器地址
/etc/resolv.conf

格式:

nameserver                        IP

例如:

[root@kyle /]# cat /etc/resolv.conf

search localdomain
nameserver 8.8.8.8
  • 设定DNS解析的优先级(本地或查找nameserver)
/etc/nsswitch.conf

hosts: files dns //先查找本地文件,再查找DNS服务器

基于AliyunDNS的DDNS实现

  • 前段时间在琢磨着怎么从Oray的付费用户中脱离出来, 在网上徘徊了许久, 最后选择了直接从AliyunDNS的接口去自己实现一个DDNS服务, 其中有些代码来自网络, 对其修改并做了一部分优化, 目前还比较稳定的跑在内网服务器上, github地址https://github.com/kylechenoO/aliddns, 附上代码与注释:
# -*- coding: UTF-8 -*-
# 请使用Python2.X来执行此脚本
import json
import os
import re
import sys
import requests
from datetime import datetime
from aliyunsdkalidns.request.v20150109 import UpdateDomainRecordRequest, DescribeDomainRecordsRequest, \
    DescribeDomainRecordInfoRequest
from aliyunsdkcore import client

#请填写你的Access Key ID
access_key_id = 'XXX'

#请填写你的Access Key Secret
access_Key_secret = 'XXX'

#请填写你的账号ID
account_id = 'XXX'

#请填写你的一级域名
rc_domain = 'XXX.com'

#请填写你的解析记录
rc_rr = 'www'

#请填写你的记录类型,DDNS请填写A,表示A记录
rc_type = 'A'

#请填写解析记录ID(可以先check_records()获取ID再填写这里)
rc_record_id = 'XXX'

#请填写解析有效生存时间TTL,单位:秒
rc_ttl = '600'

#请填写返还内容格式,json,xml
rc_format = 'json'

# 获取当前IP地址, 有多种方式实现, 我这边最快的是seip.cc就用它了
def my_ip():
    get_ip_method = os.popen('curl -s seip.cc')
    get_ip_responses = get_ip_method.readlines()[0]
    get_ip_pattern = re.compile(r'\d \.\d \.\d \.\d ')
    get_ip_value = get_ip_pattern.findall(get_ip_responses)[0]
    return(get_ip_value)

# 获取所有记录与相关信息, 包含rc_record_id
def check_records(dns_domain):
    clt = client.AcsClient(access_key_id, access_Key_secret, 'cn-hangzhou')
    request = DescribeDomainRecordsRequest.DescribeDomainRecordsRequest()
    request.set_DomainName(dns_domain)
    request.set_accept_format(rc_format)
    result = clt.do_action_with_exception(request)
    return(result)

# 获取原来的DNS记录值
def old_ip():
    clt = client.AcsClient(access_key_id, access_Key_secret, 'cn-hangzhou')
    request = DescribeDomainRecordInfoRequest.DescribeDomainRecordInfoRequest()
    request.set_RecordId(rc_record_id)
    request.set_accept_format(rc_format)
    result = clt.do_action_with_exception(request)
    result = json.JSONDecoder().decode(result)
    result = result['Value']
    return(result)

# 更新DNS记录信息
def update_dns(dns_rr, dns_type, dns_value, dns_record_id, dns_ttl, dns_format):
    clt = client.AcsClient(access_key_id, access_Key_secret, 'cn-hangzhou')
    request = UpdateDomainRecordRequest.UpdateDomainRecordRequest()
    request.set_RR(dns_rr)
    request.set_Type(dns_type)
    request.set_Value(dns_value)
    request.set_RecordId(dns_record_id)
    request.set_TTL(dns_ttl)
    request.set_accept_format(dns_format)
    result = clt.do_action_with_exception(request)
    return(result)

# 将每次IP变动的过程写入日志文件, 以便后续分析
def write_to_file():
    time_now = datetime.now().strftime('%Y-%m-%d %H:%M:%S')
    current_script_path = sys.path[7]
    print(current_script_path)
    log_file = current_script_path   '/'   'aliyun_ddns_log.txt'
    write = open(log_file, 'a')
    write.write(time_now   ' '   str(rc_value)   '\n')
    write.close()
    return(True)

# mail run parts
if __name__ == '__main__':
    # 如果你是第一次运行, 请先执行check_records, 获取相关rc_record_id, 完善文件头中的变量信息后再执行后续操作
    # print check_records(rc_domain)
    # rc_value = my_ip()

    # 获取当前IP
    rc_value = my_ip()
    # 获取当前DNS记录值
    rc_value_old = old_ip()
    # 如果当前IP与DNS记录值一致, 则不更新记录
    if rc_value_old == rc_value:
        print('The specified value of parameter Value is the same as old')

    # 否则更新记录, 写入日志文件
    else:
        print(update_dns(rc_rr, rc_type, rc_value, rc_record_id, rc_ttl, rc_format))
        write_to_file()