标签存档: Linux

CentOS Ubuntu 等linux 修改IP地址, DNS, 网关

由于修改网关总是忘记在哪个地方,每次都要去查询,太麻烦,今天在此备份一下:

一、CentOS 修改IP地址

修改对应网卡的IP地址的配置文件

# vi /etc/sysconfig/network-scripts/ifcfg-eth0
修改以下内容
DEVICE=eth0 #描述网卡对应的设备别名,例如ifcfg-eth0的文件中它为eth0
BOOTPROTO=static #设置网卡获得ip地址的方式,可能的选项为static,dhcp或bootp,分别对应静态指定的 ip地址,通过dhcp协议获得的ip地址,通过bootp协议获得的ip地址
BROADCAST=192.168.0.255 #对应的子网广播地址
HWADDR=00:07:E9:05:E8:B4 #对应的网卡物理地址
IPADDR=192.168.1.2 #如果设置网卡获得 ip地址的方式为静态指定,此字段就指定了网卡对应的ip地址
IPV6INIT=no
IPV6_AUTOCONF=no
NETMASK=255.255.255.0 #网卡对应的网络掩码
#NETWORK=192.168.1.1 #网卡对应的网络地址
GATEWAY=192.168.1.1(设置本机连接的网关的IP地址。例如,网关为10.0.0.2)
ONBOOT=yes #系统启动时是否设置此网络接口,设置为yes时,系统启动时激活此设备
TYPE=Ethernet
PEERDNS=yes
USERCTL=no
~

二、CentOS 修改网关
修改对应网卡的网关的配置文件
[root@centos]# vi /etc/sysconfig/network

修改以下内容
NETWORKING=yes(表示系统是否使用网络,一般设置为yes。如果设为no,则不能使用网络,而且很多系统服务程序将无法启动)

NETWORKING_IPV6=no

HOSTNAME=localhost.localdomain(设置本机的主机名,这里设置的主机名要和/etc/hosts中设置的主机名对应)

三、CentOS 修改DNS

修改对应网卡的DNS的配置文件
# vi /etc/resolv.conf
修改以下内容

nameserver 192.168.1.1 #域名服务器
nameserver 192.168.1.2

四、重新启动网络配置
# service network restart

# /etc/init.d/network restart

修改 IP 地址
即时生效:
# ifconfig eth0 192.168.0.2 netmask 255.255.255.0
启动生效:
修改 /etc/sysconfig/network-scripts/ifcfg-eth0

修改网关 Default Gateway
即时生效:
# route add default gw 192.168.0.1 dev eth0
启动生效:
修改 /etc/sysconfig/network

修改 DNS
修改/etc/resolv.conf
修改后可即时生效,启动同样有效

修改 host name
即时生效:
# hostname centos1
启动生效:
修改/etc/sysconfig/network

+++++++++++++++++++++++++++++++++++++++++

Ubuntu 下修改:

xinze@xinze-desktop:$ vi /etc/network/interfaces

auto lo

iface lo inet loopback

auto eth0

iface eth0 inet static

address 192.168.1.11

netmask 255.255.255.0

gateway  192.168.1.1

xinze@xinze-desktop:$ /etc/init.d/network restart

Linux中C/C++头文件说明

1、 Linux中一些头文件的作用:
<assert.h>:ANSI C。提供断言,assert(表达式)
<glib.h>:GCC。GTK,GNOME的基础库,提供很多有用的函数,如有数据结构操作函数。使用glib只需要包 含<glib.h>
<dirent.h>:GCC。文件夹操作函数。struct dirent,struct DIR,opendir(),closedir(),readdir(),readdir64()等<ctype.h>:ANSI C。字符测试函数。isdigit(),islower()等
<errno.h>:ANSI C。查看错误代码errno是调试程序的一个重要方法。当linuc C api函数发生异常时,一般会将errno变量(需include errno.h)赋一个整数值,不同的值表示不同的含义,可以通过查看该值推测出错的原因。在实际编程中用这一招解决了不少原本看来莫名其妙的问题。比较 麻烦的是每次都要去linux源代码里面查找错误代码的含义,现在把它贴出来,以后需要查时就来这里看了。来自linux 2.4.20-18的内核代码中的/usr/include/asm/errno.h
<getopt.h>:处理命令行参数。 getopt_r()

2、linux常用头文件如下:
POSIX标准定义的头文件
<dirent.h>        目录项
<fcntl.h>         文件控制
<fnmatch.h>    文件名匹配类型
<glob.h>    路径名模式匹配类型
<grp.h>        组文件
<netdb.h>    网络数据库操作
<pwd.h>        口令文件
<regex.h>    正则表达式
<tar.h>        TAR归档值
<termios.h>    终端I/O
<unistd.h>    符号常量
<utime.h>    文件时间
<wordexp.h>    字符扩展类型
————————-
<arpa/inet.h>    INTERNET定义
<net/if.h>    套接字本地接口
<netinet/in.h>    INTERNET地址族
<netinet/tcp.h>    传输控制协议定义
————————-
<sys/mman.h>    内存管理声明
<sys/select.h>    Select函数
<sys/socket.h>    套接字借口
<sys/stat.h>    文件状态
<sys/times.h>    进程时间
<sys/types.h>    基本系统数据类型
<sys/un.h>    UNIX域套接字定义
<sys/utsname.h>    系统名
<sys/wait.h>    进程控制
——————————
POSIX定义的XSI扩展头文件
<cpio.h>    cpio归档值
<dlfcn.h>    动态链接
<fmtmsg.h>    消息显示结构
<ftw.h>        文件树漫游
<iconv.h>    代码集转换使用程序
<langinfo.h>    语言信息常量
<libgen.h>    模式匹配函数定义
<monetary.h>    货币类型
<ndbm.h>    数据库操作
<nl_types.h>    消息类别
<poll.h>    轮询函数
<search.h>    搜索表
<strings.h>    字符串操作
<syslog.h>    系统出错日志记录
<ucontext.h>    用户上下文
<ulimit.h>    用户限制
<utmpx.h>    用户帐户数据库
—————————–
<sys/ipc.h>    IPC(命名管道)
<sys/msg.h>    消息队列
<sys/resource.h>资源操作
<sys/sem.h>    信号量
<sys/shm.h>    共享存储
<sys/statvfs.h>    文件系统信息
<sys/time.h>    时间类型
<sys/timeb.h>    附加的日期和时间定义
<sys/uio.h>    矢量I/O操作
——————————
POSIX定义的可选头文件
<aio.h>        异步I/O
<mqueue.h>    消息队列
<pthread.h>    线程
<sched.h>    执行调度
<semaphore.h>    信号量
<spawn.h>     实时spawn接口
<stropts.h>    XSI STREAMS接口
<trace.h>     事件跟踪

3、 C/C++头文件一览
C
#include <assert.h>    //设定插入点
#include <ctype.h>     //字符处理
#include <errno.h>     //定义错误码
#include <float.h>     //浮点数处理
#include <iso646.h>        //对应各种运算符的宏
#include <limits.h>    //定义各种数据类型最值的常量
#include <locale.h>    //定义本地化C函数
#include <math.h>     //定义数学函数
#include <setjmp.h>        //异常处理支持
#include <signal.h>        //信号机制支持
#include <stdarg.h>        //不定参数列表支持
#include <stddef.h>        //常用常量
#include <stdio.h>     //定义输入/输出函数
#include <stdlib.h>    //定义杂项函数及内存分配函数
#include <string.h>    //字符串处理
#include <time.h>     //定义关于时间的函数
#include <wchar.h>     //宽字符处理及输入/输出
#include <wctype.h>    //宽字符分类

传统C++
#include <fstream.h>    //改用<fstream>
#include <iomanip.h>    //改用<iomainip>
#include <iostream.h>   //改用<iostream>
#include <strstrea.h>   //该类不再支持,改用<sstream>中的stringstream
————————————————————————————————

标准C++
#include <algorithm>    //STL 通用算法
#include <bitset>     //STL 位集容器
#include <cctype>          //字符处理
#include <cerrno>      //定义错误码
#include <cfloat>     //浮点数处理
#include <ciso646>         //对应各种运算符的宏
#include <climits>     //定义各种数据类型最值的常量
#include <clocale>     //定义本地化函数
#include <cmath>      //定义数学函数
#include <complex>     //复数类
#include <csignal>         //信号机制支持
#include <csetjmp>         //异常处理支持
#include <cstdarg>         //不定参数列表支持
#include <cstddef>         //常用常量
#include <cstdio>      //定义输入/输出函数
#include <cstdlib>     //定义杂项函数及内存分配函数
#include <cstring>     //字符串处理
#include <ctime>      //定义关于时间的函数
#include <cwchar>      //宽字符处理及输入/输出
#include <cwctype>     //宽字符分类
#include <deque>      //STL 双端队列容器
#include <exception>    //异常处理类
#include <fstream>     //文件输入/输出
#include <functional>   //STL 定义运算函数(代替运算符)
#include <limits>      //定义各种数据类型最值常量
#include <list>      //STL 线性列表容器
#include <locale>          //本地化特定信息
#include <map>       //STL 映射容器
#include <memory>          //STL通过分配器进行的内存分配
#include <new>             //动态内存分配
#include <numeric>         //STL常用的数字操作
#include <iomanip>     //参数化输入/输出
#include <ios>       //基本输入/输出支持
#include <iosfwd>     //输入/输出系统使用的前置声明
#include <iostream>     //数据流输入/输出
#include <istream>     //基本输入流
#include <iterator>        //STL迭代器
#include <ostream>     //基本输出流
#include <queue>      //STL 队列容器
#include <set>       //STL 集合容器
#include <sstream>     //基于字符串的流
#include <stack>      //STL 堆栈容器
#include <stdexcept>    //标准异常类
#include <streambuf>    //底层输入/输出支持
#include <string>     //字符串类
#include <typeinfo>        //运行期间类型信息
#include <utility>     //STL 通用模板类
#include <valarray>        //对包含值的数组的操作
#include <vector>     //STL 动态数组容器
————————————————————————————————

C99增加的部分
#include <complex.h>   //复数处理
#include <fenv.h>    //浮点环境
#include <inttypes.h>  //整数格式转换
#include <stdbool.h>   //布尔环境
#include <stdint.h>   //整型环境
#include <tgmath.h>   //通用类型数学宏

ffmpeg ffmpeg-php 安装及出错解决办法

在centos 5.4环境下安装 FFMPEG 和 FFMPEG-PHP 。

  1. 下载ffmepg及ffmpeg-php
    wget http://www.ffmpeg.org/releases/ffmpeg-export-snapshot.tar.bz2
    wget http://downloads.sourceforge.net/project/ffmpeg-php/ffmpeg-php/0.6.0/ffmpeg-php-0.6.0.tbz2?use_mirror=ncu
  2. 安装ffmpeg
    tar -xjf ffmpeg-export-snapshot.tar.bz2
    #进入解压后的源文件目录
    cd ffmpeg-export-2010-03-02/
    ./configure --prefix=/usr/local   --enable-shared
    make
    make install

    测试是否能正常使用:

    [root@mail ffmpeg-export-2010-03-02]# ffmpeg
    FFmpeg version SVN-r22144-snapshot, Copyright (c) 2000-2010 the FFmpeg developers
      built on Mar 30 2010 23:53:53 with gcc 4.1.2 20080704 (Red Hat 4.1.2-46)
      configuration: --prefix=/usr/local --enable-shared
      libavutil     50. 9. 0 / 50. 9. 0
      libavcodec    52.55. 0 / 52.55. 0
      libavformat   52.54. 0 / 52.54. 0
      libavdevice   52. 2. 0 / 52. 2. 0
      libswscale     0.10. 0 /  0.10. 0

    如果提示:“ffmpeg: error while loading shared libraries: libavdevice.so.52: cannot open shared object file: No such file or directory”

    vi /etc/ld.so.conf.d/ffmpeg.conf
    #在里面加入:usr/local/lib,这个目录,为ffmpeg安装后libavdevice.so.52等lib文件所在目录。
    ldconfig -v
  3. 安装ffmpeg-php
    tar -xjf ffmpeg-php-0.6.0.tbz2
    cd ffmpeg-php-0.6.0
    /usr/local/php/bin/phpize
    ./configure --with-php-config=/usr/local/php/bin/php-config  --with-ffmpeg=/usr/local
    make test
    make
    make install
    #在php.ini中加入extension=ffmpeg.so

    如果在make test中发现错误提示:make: *** [ffmpeg_frame.lo] Error 1
    则:

    mv ffmpeg_frame.loT ffmpeg_frame.lo

    测试是否安装成功:

    php -r 'phpinfo();' | grep ffmpeg
    ffmpeg
    ffmpeg-php version => 0.6.0-svn
    ffmpeg-php built on => Mar 31 2010 14:17:14
    ffmpeg-php gd support  => enabled
    ffmpeg libavcodec version => Lavc52.55.0
    ffmpeg libavformat version => Lavf52.54.0
    ffmpeg swscaler version => SwS0.10.0
    ffmpeg.allow_persistent => 0 => 0
    ffmpeg.show_warnings => 0 => 0

    如果有ffmepg-php模块错误提示:php: symbol lookup error: /usr/local/php/lib/php/extensions/no-debug-non-zts-20060613/ffmpeg.so: undefined symbol: register_ffmpeg_frame_class

    nm /usr/local/php/lib/php/extensions/no-debug-non-zts-20060613/ffmpeg.so|grep register_ffmpeg_frame_class
    #输出U register_ffmpeg_frame_class
    

    则修改ffmpeg_frame.c,将3处的PIX_FMT_RGBA32 更改为PIX_FMT_RGB32即可。

    vi ffmpeg_frame.c
    :%s/PIX_FMT_RGBA32/PIX_FMT_RGB32
    :w
    :q!
    ./configure --with-php-config=/usr/local/php/bin/php-config  --with-ffmpeg=/usr/local
    make test
    make
    make install

及时阻止SSH暴力破解入侵者方法

最近,老是发现有很多暴力破解SSH密码的入侵者,虽然服务器密码设置的很复杂,但是对于这类骚扰者,还是及时屏蔽的好。如下有三个工具,原理很简单,就是分析/var/log/secure日志,找出IP,将其屏蔽。

  1. DenyHosts
    DenyHosts是Python语言写的一个程序,它会分析sshd的日志文件(默认是/var/log/secure),当发现重复的攻击时就会记录IP到/etc/hosts.deny文件,启用tcp_wrappers,从而达到自动屏IP的功能。 通过http://denyhosts.sourceforge.net可以下载DenyHosts的程序,可以直接下载rpm包来安装,也可以通过src.rpm包重新编译并安装等,通过这种方式默认是安装在/usr/share/denyhosts目录下。
    我是通过yum直接安装的:yum install denyhosts.noarch, 可以通过修改/etc/denyhosts.conf来更改配置。启动服务:/etc/init.d/denyhosts start

    详细信息,查看源码。

  2. Perl脚本
    #!/usr/bin/perl 
    
    # 及时封锁使用"暴入法" 入侵的使用者(SSH)
    # 技术支援: http://www.vixual.net/ 
    
    #==相关参数== 
    
    #记录ssh 连线的LOG 档,预设: /var/log/secure
    $log_file = "/var/log/secure"; 
    
    #于多久的时间内尝试登入(秒),预设: 1 小时
    $time_range = 1 * 60 * 60; 
    
    #于$time_range 所设定的时间内,尝试登入失败多少次立即封锁IP,预设: 10 次
    $drop_count = 10; 
    
    #寄件通知,预设收件者: root@localhost
    $mail = 'root@localhost'; 
    
    #寄件程式的位置
    $sendmail = "/usr/sbin/sendmail"; 
    
    #==== 
    
    use Time::Local; 
    
    $ip = $ARGV[0];
    $daemon = $ARGV[1];
    $count = 0; 
    
    %month = (
    Jan => 0,
    Feb => 1,
    Mar => 2,
    Apr => 3,
    May => 4,
    Jun => 5,
    Jul => 6,
    Aug => 7,
    Sep => 8,
    Oct => 9,
    Nov => 10,
    Dec => 11
    ); 
    
    $time = time();
    ($second,$minute,$hour,$day,$month,$year) = localtime($time); 
    
    #取得登入失败的 log
    @list = `cat $log_file | grep "sshd.*Failed password.* $ip "`;
    for(my $i = $#list; $i >= 0; $i--){
    #取得 log 的时间
    my($log_month,$log_day,$log_time) = split(/ +/,$list[$i]);
    my($log_hour,$log_minute,$log_second) = split(/:/,$log_time);
    #前一年的记录
    if($log_month > $month){
    $log_year = $year - 1;
    }else{
    $log_year = $year;
    }
    #将时间转为秒数
    $log_time = timelocal($log_second,$log_minute,$log_hour,$log_day,$month{$log_month},$log_year);
    if($time < $log_time + $time_range ){
    $count++;
    }else{
    last;
    }
    } 
    
    if($count > $drop_count){
    #封锁 IP
    `iptables -A INPUT -p tcp -s $ip --dport 22 -j DROP`;
    if($mail){
    #寄件通知
    $hostname = `hostname`;
    $month++;
    $year += 1900;
    chomp($hostname);
    open(MAIL, "| $sendmail -t") || die "Can't open $sendmail !\n";
    print MAIL qq|To: $mail\n|;
    print MAIL qq|Subject: [$hostname]封锁$ip\n|;
    print MAIL qq|Content-Transfer-Encoding: 8bit\n|;
    print MAIL qq|Content-type: text/plain\; charset=Big5\n\n|;
    print MAIL "\n时间: $year-$month-$day $hour:$minute:$second\n----\n使用者\"$ip\" 尝试以SSH 登入伺服器,共失败$ count 次,已于防火墙封锁该IP。\n\n";
    print MAIL @list;
    close(MAIL);
    }
    } 
    
    exit; 
  3. Bash shell
    
    #! /bin/bash
    # 获取前 1 分钟内的 secure 记录,统计 ssh 认证失败的 IP 和其 失败次数
    SCANNER=`grep "\`date \"+ %e %H:%M\" -d \"-1min\"\`" /var/log/secure|awk '/Failed/{print $(NF-3)}'|sort|uniq -c|awk '{print $1"="$2;}'`
    for i in $SCANNER
    do
          # 取认证失败次数
          NUM=`echo $i|awk -F= '{print $1}'`
          # 取其 IP 地址
          IP=`echo $i|awk -F= '{print $2}'`
          # 若其在失败次数超过 5 次且之前没有被阻断过,那么添加一条策略将其阻断,并记录日志
          if [ $NUM -gt 5 ] && [ -z "`iptables -vnL INPUT|grep $IP`" ]
          then
                  iptables -I INPUT -s $IP -j DROP
                  echo "`date` $IP($NUM)" >> /var/log/scanner.log
          fi
    done
    

/var/log/secure 日志不记录问题

上天查看了服务器安全日志,防火墙屏蔽了处理了一些暴力破解ssh密码的ip(其中一个ip地址为北京一家有名的CDN服务提供商),然后删除了所有的/var/log/secure* 日志文件。

今天再来查看日志的时候,发现/var/log/secure竟然没有记录,才想到直接删除日志文件的时候,对应的服务需要重启。运行命令:service syslog  restart ;service sshd restart 后正常。

顺便复习下ssh在syslog中的设置的知识。

  1. /etc/ssh/sshd_config 中的设置:(即:SyslogFacility 设为AUTHPRIV)
    [root@mail ~]# more /etc/ssh/sshd_config
    #Port 22
    # Logging
    # obsoletes QuietMode and FascistLogging
    #SyslogFacility AUTH
    SyslogFacility AUTHPRIV
    #LogLevel INFO
    #就是把sshd的日志定义在authriv.info级别。
  2. 配合/etc/syslog.conf中的设置:
  3. [root@mail ~]# more /etc/syslog.conf
    # Log all kernel messages to the console.
    # Logging much else clutters up the screen.
    #kern.*                                                 /dev/console
    
    # Log anything (except mail) of level info or higher.
    # Don't log private authentication messages!
    *.info;mail.none;authpriv.none;cron.none                /var/log/messages
    
    # The authpriv file has restricted access.
    authpriv.*                                              /var/log/secure
重新启动sshd和syslog

python 调用so dll动态链接库

ctypes使得python能够直接调用c语言开发的动态链接库,非常强大。
为了使用CTypes,你必须依次完成以下步骤:
* 编写动态连接库程序
* 载入动态连接库
* 将Python的对象转换为ctypes所能识别的参数
* 使用ctypes的参数调用动态连接库中的函数

一、Windows下使用Python的ctypes调用Dll动态链接库

  1. 编写dll文件
  2. 打开VS2008,新建一个VC工程,选择Win32类型,Win32项目,用程序类型选择DLL………
    调用方式见Linux调用方式。

二、Linux下使用Python的ctypes调用so动态链接库

  1. 编写so文件
  2. //test.h
    #include   "stdio.h"
    
    void test();
    float add(float, float);
    //test.c
    #include "test.h"
    
    void test()
    {
    	printf("Hello Dll...\n");
    }
    
    float add(float a, float b)
    {
        return a + b;
    }
    gcc -fPIC -shared test.c -o libtest.so
    
    #-fPIC  编译成位置无关代码,必须  不然你的程序在别的地方肯可能运行不了
    #-shared  当然是说要编译成共享库了
  3. Python调用so动态链接库
  4. #!/usr/bin/env python
    # -*-coding:UTF-8-*-
    
    print "sss"
    
    from ctypes import *
    
    test = cdll.LoadLibrary("./libtest.so")
    
    test.test()
    
    add = test.add
    add.argtypes = [c_float, c_float] # 参数类型,两个float(c_float内ctypes类型)
    add.restype = c_float
    
    print add(1.2, 19.2)

发的