Find alive hosts

This command can find alive hosts on linux:
nmap -v -sn 192.168.1.1/24 | grep 'Nmap scan' | grep -v 'host down'

Reference:

##sample code

Find alive hosts with ARP protocol

// Find alive hosts with ARP protocol
// Win32 console project, compile with VS2005
// Create by Dennis
// 2013-04-09

// Reference:
// http://msdn.microsoft.com/en-us/library/aa366358(VS.85).aspx

#include <winsock2.h>
#include <iphlpapi.h>
#include <process.h>
#include <stdio.h>
#include <stdlib.h>

#pragma comment(lib, "iphlpapi.lib")
#pragma comment(lib, "ws2_32.lib")

CRITICAL_SECTION  cs;

unsigned long netIp = 0;

// Get host IP
// Reference: http://blog.csdn.net/happycock/article/details/491424
// If your machine have more than one network adapter, you should
// fix this function.
void GetHostIP(char* ip, int size1, char* mask, int size2)
{
    ULONG len = 0; 
    GetAdaptersInfo(NULL, &len);
    PIP_ADAPTER_INFO p = static_cast<PIP_ADAPTER_INFO>(malloc(len));
    GetAdaptersInfo(p, &len);
    for (PIP_ADAPTER_INFO q = p; q != NULL; q = q->Next)
    {
        if (ip) strncpy(ip, q->IpAddressList.IpAddress.String, size1);
        if (mask) strncpy(mask, q->IpAddressList.IpMask.String, size2);
        break;
    }

    free(p);
}

unsigned int __stdcall ArpThread(LPVOID lParam)
{
    ULONG MacAddr[2];       /* for 6-byte hardware addresses */
    ULONG PhysAddrLen = 6;  /* default to length of six bytes */

#if 1
    EnterCriticalSection(&cs);
    //converts to TCP/IP network byte order (which is big-endian) 
    unsigned long dstIP=htonl(++netIp);
    LeaveCriticalSection(&cs);
#else
    // Reference: http://blog.csdn.net/morewindows/article/details/7429155
    //netIp++;  
    InterlockedIncrement((LPLONG)&netIp);
    unsigned long dstIP=htonl(netIp);
#endif

    if (NO_ERROR == SendARP(dstIP, 0,&(MacAddr[0]),&PhysAddrLen)) {
        EnterCriticalSection(&cs);
        BYTE* bPhysAddr = (BYTE *) & MacAddr;
        if (PhysAddrLen) {
            struct in_addr addr1;
            memcpy(&addr1, &dstIP, 4);
            printf("%16s : ", inet_ntoa(addr1));
            for (int i = 0; i < (int) PhysAddrLen; i++) {
                if (i == (PhysAddrLen - 1))
                    printf("%.2X\n", (int) bPhysAddr[i]);
                else
                    printf("%.2X-", (int) bPhysAddr[i]);
            }
        } else
            printf("Warning: SendArp completed successfully, but returned length=0\n");
        LeaveCriticalSection(&cs);
    }

    return 0;
}

int main(int argc, char **argv)
{
    IPAddr SrcIp = 0;

    char strSrcIP[16] = {0};
    char strSrcMask[16] = {0};
    // Get Host IP and Mask
    GetHostIP(strSrcIP, sizeof(strSrcIP), strSrcMask, sizeof(strSrcMask));

    InitializeCriticalSection(&cs);

    SrcIp = inet_addr(strSrcIP);
    unsigned long findMask=inet_addr(strSrcMask);
    int netsize = ~ntohl(findMask);
    netIp = ntohl(SrcIp & findMask); 

#if 0
    unsigned long dstIp;
    struct in_addr addr1;
    HANDLE hThr[63] = {0};
    for (int i=1; i<64; i++)
    {
        //converts to TCP/IP network byte order (which is big-endian) 
         dstIp=htonl(netIp+i);

         hThr[i-1] = (HANDLE)_beginthreadex(NULL, 0, ArpThread, &dstIp, 0, NULL);
    }

    WaitForMultipleObjects(63,&(hThr[0]),TRUE,INFINITE);
#else
    HANDLE *phThread = (HANDLE*)malloc(netsize*sizeof(HANDLE));
    for (int i=1; i<netsize; i++)
    {
        // _beginthreadex if more effect than CreateThread
        // you can google it for more information
        phThread[i-1] = (HANDLE)_beginthreadex(NULL, 0, ArpThread, 0, 0, NULL);
    }

    // There is a limit count for function WaitForMultipleObjects
    int netcount = netsize-1;
    int waitcount = 0;
    for (int i=0; i<netcount;)
    {
        waitcount = MAXIMUM_WAIT_OBJECTS;
        if (netcount-i<MAXIMUM_WAIT_OBJECTS)
            waitcount = netcount-i;
        WaitForMultipleObjects(waitcount,&(phThread[i]),TRUE,INFINITE);
        i += waitcount;
    }

    free(phThread);

#endif

    DeleteCriticalSection(&cs);

    //system("pause");
    return 0;
}

output as below:(For security reasons, replace some of the characters as asterisks)

  192.168.50.1 : **-**-E2-**-C7-**
  192.168.50.2 : **-**-4C-**-00-**
 192.168.50.16 : **-**-19-**-46-**
 192.168.50.22 : **-**-09-**-3A-**
 192.168.50.28 : **-**-AE-**-E6-**
 ...
192.168.50.129 : **-**-9B-**-19-**
192.168.50.238 : **-**-6F-**-28-**
Press any key to continue. . .

Find alive hosts with icmp protocol

// Find alive hosts with icmp protocol
// Win32 console project, compile with VS2005
// Create by Dennis
// 2013-04-09

// Reference:
// http://msdn.microsoft.com/en-us/library/windows/desktop/aa366050%28v=vs.85%29.aspx

#include <winsock2.h>
#include <iphlpapi.h>
#include <icmpapi.h>
#include <process.h>
#include <stdio.h>

#pragma comment(lib, "iphlpapi.lib")
#pragma comment(lib, "ws2_32.lib")

#define IP_SECTION "192.168.110.%d"

CRITICAL_SECTION cs;
int g_ip_index = 0;

unsigned int __stdcall IcmpThread(LPVOID LPARAM)
{
    // Declare and initialize variables

    EnterCriticalSection(&cs);
    char ip[16] = {0};
    sprintf(ip, IP_SECTION, ++g_ip_index);
    HANDLE hIcmpFile;
    unsigned long ipaddr = INADDR_NONE;
    DWORD dwRetVal = 0;
    char SendData[32] = "Data Buffer";
    LPVOID ReplyBuffer = NULL;
    DWORD ReplySize = 0;

    ipaddr = inet_addr(ip);
    if (ipaddr == INADDR_NONE) {
        return 1;
    }

    hIcmpFile = IcmpCreateFile();
    if (hIcmpFile == INVALID_HANDLE_VALUE) {
        printf("\tUnable to open handle.\n");
        printf("IcmpCreatefile returned error: %ld\n", GetLastError() );
        return 1;
    }    

    ReplySize = sizeof(ICMP_ECHO_REPLY) + sizeof(SendData);
    ReplyBuffer = (VOID*) malloc(ReplySize);
    if (ReplyBuffer == NULL) {
        printf("\tUnable to allocate memory\n");
        return 1;
    }    
    LeaveCriticalSection(&cs);

    dwRetVal = IcmpSendEcho(hIcmpFile, ipaddr, SendData, sizeof(SendData), 
        NULL, ReplyBuffer, ReplySize, 10);
#if 0
    if (dwRetVal != 0) {
        PICMP_ECHO_REPLY pEchoReply = (PICMP_ECHO_REPLY)ReplyBuffer;
        struct in_addr ReplyAddr;
        ReplyAddr.S_un.S_addr = pEchoReply->Address;
        printf("\tSent icmp message to %s\n", ip);
        if (dwRetVal > 1) {
            printf("\tReceived %ld icmp message responses\n", dwRetVal);
            printf("\tInformation from the first response:\n"); 
        }    
        else {    
            printf("\tReceived %ld icmp message response\n", dwRetVal);
            printf("\tInformation from this response:\n"); 
        }    
        printf("\t  Received from %s\n", inet_ntoa( ReplyAddr ) );
        printf("\t  Status = %ld\n", 
            pEchoReply->Status);
        printf("\t  Roundtrip time = %ld milliseconds\n", 
            pEchoReply->RoundTripTime);
    }
    else {
        printf("\tCall to IcmpSendEcho failed.\n");
        printf("\tIcmpSendEcho returned error: %ld\n", GetLastError() );
        return 1;
    }
#else
    if (dwRetVal != 0)
        printf("%16s is alive\n", ip);
#endif
}

int __cdecl main(int argc, char **argv)  {

    InitializeCriticalSection(&cs);

    int netsize = 255;

    HANDLE *phThread = (HANDLE*)malloc(netsize*sizeof(HANDLE));
    for (int i=1; i<netsize; i++)
    {
        // _beginthreadex if more effect than CreateThread
        // you can google it for more information
        phThread[i-1] = (HANDLE)_beginthreadex(NULL, 0, IcmpThread, 0, 0, NULL);
    }

    // There is a limit count for function WaitForMultipleObjects
    int netcount = netsize-1;
    int waitcount = 0;
    for (int i=0; i<netcount;)
    {
        waitcount = MAXIMUM_WAIT_OBJECTS;
        if (netcount-i<MAXIMUM_WAIT_OBJECTS)
            waitcount = netcount;
        WaitForMultipleObjects(waitcount,&(phThread[i]),TRUE,INFINITE);
        i += waitcount;
    }

    free(phThread);

    DeleteCriticalSection(&cs);
    system("pause");
    return 0;
}    

Output as below:

  192.168.110.1 is alive
  192.168.110.5 is alive
  192.168.110.6 is alive
 192.168.110.30 is alive
 192.168.110.32 is alive
 192.168.110.35 is alive
 ...
192.168.110.248 is alive
192.168.110.249 is alive
Press any key to continue. . .

IPFind

I have write a windows application with MFC, you can find the source code and execute file here IPFind
IPFind

kvm

##KVM Install

Install Tools:

yum -y install kvm python-virtinst libvirt bridge-utils virt-manager qemu-kvm-tools virt-viewer virt-v2v

安装

virt-install --name=win2003test --ram 1024 --vcpus=1 --disk path=/mnt/win2003test.img,size=20 --accelerate --cdrom /mnt/WindowsServer2003SP2.iso --graphics vnc,password=123456,listen=0.0.0.0,port=5920 --force --autostart

删除

[root@localhost ~]# virsh -c qemu:///system
virsh # list --all
virsh # destroy win2003Test
virsh # undefine win2003Test

安装过程中,在系统重启后,设置CD

修改 /etc/libvirt/qemu/win2003test.xml 或执行命令 virsh edit win2003test
<source dev='/mnt/WindowsServer2003SP2.iso'/> 
加入到
<disk type='block' device='cdrom'>
 <driver name='qemu' type='raw'/>
 <source dev='/mnt/WindowsServer2003SP2.iso'/> 
 ...
</disk>

然后执行一下命令  

[root@localhost ~]# virsh -c qemu:///system
virsh # shutdown win2003test
virsh # destroy win2003test
virsh # define /etc/libvirt/qemu/win2003test.xml
virsh # start win2003test
virsh # quit
[root@localhost ~]# virt-viewer win2003test

解决出现两个鼠标光标问题

修改 /etc/libvirt/qemu/win2003test.xml 或执行命令 virsh edit win2003test 
</console>
<input type='tablet' bus='usb'/>
<input type='mouse' bus='ps2'/>

安装网卡驱动

下载virtio-win-0.1-52.iso
http://alt.fedoraproject.org/pub/alt/virtio-win/latest/images/bin/virtio-win-0.1-52.iso
修改
<source dev='/mnt/WindowsServer2003SP2.iso'/> 
为
<source dev='/mnt/virtio-win-0.1-52.iso'/> 
重启系统
[root@localhost ~]# virsh shutdown win2003test
[root@localhost ~]# virsh define /etc/libvirt/qemu/win2003test.xml
[root@localhost ~]# virsh start win2003test
进入系统后就可以从光驱安装网卡驱动了.

[root@localhost ~]# /etc/init.d/network stop
Shutting down interface br0:  [  OK  ]
Shutting down interface eth0:  [  OK  ]
Shutting down interface eth1:  [  OK  ]
Shutting down interface eth2:  [  OK  ]
Shutting down interface eth3:  [  OK  ]
Shutting down interface eth4:  [  OK  ]
Shutting down interface eth5:  [  OK  ]
Shutting down loopback interface:  [  OK  ]

参考文章:


##KVM Bridge

command list:

[root@localhost ~]#service network stop
[root@localhost ~]#cd /etc/sysconfig/network-scripts/
[root@localhost ~]#cat ifcfg-eth0
    DEVICE=eth0
    ONBOOT=yes
    BRIDGE=br0
    HWADDR=b3:1c:51:ca:a9:f0

[root@localhost ~]#cat ifcfg-br0
    DEVICE=br0   
    TYPE=Bridge 
    BOOTPROTO=static
    ONBOOT=yes 
    IPADDR=192.168.110.30
    NETMASK=255.255.255.192
    GATEWAY=192.168.110.1

[root@localhost ~]#service network start

[root@localhost ~]#route add default gw 192.168.110.1 dev br0

参考:


##KVM Bond

关掉NetworkManager服务

[root@localhost ~]# service NetworkManager stop

[root@localhost ~]# service NetworkManager status
NetworkManager is stopped

列出当前的网卡的个数

查询eth*,其中[[:digit:]]匹配数字,+匹配用来一个或多个数字
[root@localhost ~]# ip a | grep -E "eth[[:digit:]]+:"
2: eth4: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
3: eth2: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
4: eth5: <BROADCAST,MULTICAST,SLAVE,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast master bond0 state UP qlen 1000
5: eth3: <BROADCAST,MULTICAST,SLAVE,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast master bond0 state UP qlen 1000
6: eth6: <BROADCAST,MULTICAST> mtu 1500 qdisc noop state DOWN qlen 1000
7: eth7: <BROADCAST,MULTICAST> mtu 1500 qdisc noop state DOWN qlen 1000    
[root@localhost ~]# ip a | grep -E "eth[[:digit:]]+:" | wc -l
6

[root@localhost ~]# grep -E "eth[[:digit:]]+" /proc/net/dev | awk -F' |:' '{print $3}' | sort
eth2
eth3
eth4
eth5
eth6
eth7

HowTo: Linux Show List Of Network Cards    
http://www.cyberciti.biz/faq/linux-list-network-cards-command/

列出bond的个数和组成

[root@localhost ~]# cat /etc/sysconfig/network-scripts/ifcfg-bond0 
DEVICE=bond0
BOOTPROTO=static
ONBOOT=yes
USERCTL=no
TYPE=Ethernet
BRIDGE=br1

查看绑定状态
[root@localhost ~]# cat /proc/net/bonding/bond0 
Ethernet Channel Bonding Driver: v3.6.0 (September 26, 2009)

Bonding Mode: adaptive load balancing
Primary Slave: None
Currently Active Slave: eth5    # 当前使用eth5
MII Status: up
MII Polling Interval (ms): 100
Up Delay (ms): 0
Down Delay (ms): 0

Slave Interface: eth3           # 绑定成员eth3
MII Status: up
Speed: 1000 Mbps
Duplex: full
Link Failure Count: 0
Permanent HW addr: 00:24:ec:20:00:fe
Slave queue ID: 0

Slave Interface: eth5           # 绑定成员eth5
MII Status: up
Speed: 1000 Mbps
Duplex: full
Link Failure Count: 0
Permanent HW addr: 00:24:ec:20:00:fd
Slave queue ID: 0

列出网桥的个数及其组成,和vnet的情况

1).查询网桥个数
    [root@localhost ~]# brctl show
    bridge name    bridge id        STP enabled    interfaces
    br0        8000.0024ec2000fb    no        eth4
                                tap0
    br1        8000.0024ec2000fe    no        bond0
                                tap1
                                vnet0
    virbr0        8000.525400d2af63    yes        virbr0-nic

    [root@localhost ~]# ifconfig | grep -E "^br[[:digit:]]"
    br0       Link encap:Ethernet  HWaddr 00:24:EC:20:00:FB  
    br1       Link encap:Ethernet  HWaddr 00:24:EC:20:00:FE  
    [root@localhost ~]# ifconfig | grep -E "^br[[:digit:]]" | wc -l
    2

2).查询vnet情况,使用sed,查找vnet开始到空行结束的行
    [root@localhost ~]# ifconfig | sed -n '/vnet[[:digit:]]/,/^$/p'
    vnet0     Link encap:Ethernet  HWaddr FE:24:EC:20:00:FF  
              inet6 addr: fe80::fc24:ecff:fe20:ff/64 Scope:Link
              UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
              RX packets:2669288 errors:0 dropped:0 overruns:0 frame:0
              TX packets:26731021 errors:0 dropped:0 overruns:0 carrier:0
              collisions:0 txqueuelen:500 
              RX bytes:323410384 (308.4 MiB)  TX bytes:2495782478 (2.3 GiB)

    [root@localhost ~]# 

两个网卡建立绑定

[root@localhost ~]# cat /etc/sysconfig/network-scripts/ifcfg-bond0 
DEVICE=bond0
BOOTPROTO=static
ONBOOT=yes
USERCTL=no
TYPE=Ethernet
BRIDGE=br1

echo "MASTER=bond0
SLAVE=yes" >> /etc/sysconfig/network-scripts/ifcfg-eth0

echo "MASTER=bond0
SLAVE=yes" >> /etc/sysconfig/network-scripts/ifcfg-eth1

一个绑定建立一个网桥

[root@localhost ~]# cat /etc/sysconfig/network-scripts/ifcfg-br0
DEVICE="br0"
BOOTPROTO=static
NM_CONTROLLED="yes"
ONBOOT=yes
IPADDR=192.168.110.30
PREFIX=24
GATEWAY=192.168.110.1
DEFROUTE=yes
TYPE=Bridge
[root@localhost ~]# cat /etc/sysconfig/network-scripts/ifcfg-br1
DEVICE="br1"
BOOTPROTO=none
NM_CONTROLLED="yes"
ONBOOT=yes
IPADDR=192.168.110.32
NETMASK=255.255.255.0
GATEWAY=192.168.110.1
DEFROUTE=yes
TYPE=Bridge

删除网桥,删除绑定

[root@localhost ~]# brctl delif br1 eth3
[root@localhost ~]# brctl delif br1 eth5
[root@localhost ~]# brctl delbr br1

service network stop
rm -f ifcfg-br*

A bonded machine look as below:

[root@localhost ~]# ls  /etc/sysconfig/network-scripts/
ifcfg-bond0  ifcfg-eth3  ifdown-bnep  ifdown-post    ifup          ifup-ipv6   ifup-ppp       init.ipv6-global
ifcfg-br0    ifcfg-eth4  ifdown-eth   ifdown-ppp     ifup-aliases  ifup-isdn   ifup-routes    net.hotplug
ifcfg-br1    ifcfg-eth5  ifdown-ippp  ifdown-routes  ifup-bnep     ifup-plip   ifup-sit       network-functions
ifcfg-eth0   ifcfg-lo    ifdown-ipv6  ifdown-sit     ifup-eth      ifup-plusb  ifup-tunnel    network-functions-ipv6
ifcfg-eth1   ifdown      ifdown-isdn  ifdown-tunnel  ifup-ippp     ifup-post   ifup-wireless
[root@localhost ~]# cat /etc/sysconfig/network-scripts/ifcfg-bond0 
DEVICE=bond0
BOOTPROTO=static
ONBOOT=yes
USERCTL=no
TYPE=Ethernet
BRIDGE=br1
[root@localhost ~]# cat /etc/sysconfig/network-scripts/ifcfg-br1
DEVICE="br1"
BOOTPROTO=none
NM_CONTROLLED="yes"
ONBOOT=yes
IPADDR=192.168.110.32
NETMASK=255.255.255.0
GATEWAY=192.168.110.1
DEFROUTE=yes
TYPE=Bridge
[root@localhost ~]# cat /etc/sysconfig/network-scripts/ifcfg-eth3
DEVICE=eth3
BOOTPROTO=static
ONBOOT=yes
MASTER=bond0
SLAVE=yes
USERCTL=no
TYPE=Ethernet
[root@localhost ~]# cat /etc/sysconfig/network-scripts/ifcfg-eth5
DEVICE=eth5
BOOTPROTO=static
ONBOOT=yes
MASTER=bond0
SLAVE=yes
USERCTL=no
TYPE=Ethernet
[root@localhost ~]# cat /etc/modprobe.d/modprobe.conf 
alias bond0 bonding
options bond0 miimon=100 mode=6
[root@localhost ~]# grep -r "bond0" /etc
/etc/modprobe.d/modprobe.conf:alias bond0 bonding
/etc/modprobe.d/modprobe.conf:options bond0 miimon=100 mode=6
/etc/sysconfig/network-scripts/ifcfg-eth5:MASTER=bond0
/etc/sysconfig/network-scripts/ifcfg-bond0:DEVICE=bond0
/etc/sysconfig/network-scripts/ifcfg-eth3:MASTER=bond0
[root@localhost ~]#

参考:


Other:

convert mysql to sqlite

安装 mysql

yum install mysql

安装 sqlite

yum install sqlite-devel

MySql常用命令

登陆数据库
[root@localhost ~] mysql -uroot -p123456 mysql

显示已有数据库
mysql> show databases;

进入数据库
mysql> use mytest;

数据库中的表
mysql> show tables;

脚本实现: mysql 转为 sqlite

mysql2sqlite.sh

终端操作记录

# 上传脚本
[root@localhost sqlite]# sshpass -p 1234 scp mysql2sqlite.sh 192.168.1.103:/root
[root@localhost sqlite]# ssh 172.16.50.117
[root@localhost ~]# ls
mysql2sqlite.sh

# 执行转换mysql数据库到sqlite数据库,转换后的文件为database.sqlite
[root@localhost ~]# ./mysql2sqlite.sh -u root -p123456 mysql | sqlite3 database.sqlite
memory
[root@localhost ~]# ls -lh
total 632K
-rw-r--r-- 1 root root 629K 2013-04-02 09:08 database.sqlite
-rwxr-xr-x 1 root root 3.0K 2013-04-02 08:49 mysql2sqlite.sh

# 打开数据库文件
[root@localhost ~]# sqlite3 database.sqlite  
SQLite version 3.6.17
Enter ".help" for instructions
Enter SQL statements terminated with a ";"
sqlite> .database                   # 显示数据库
seq  name             file                                                      
---  ---------------  ----------------------------------------------------------
0    main             /root/database.sqlite                                     
sqlite> .tables                     # 显示表格
columns_priv               proc                     
db                         procs_priv               
event                      servers                  
func                       tables_priv              
help_category              time_zone                
help_keyword               time_zone_leap_second    
help_relation              time_zone_name           
help_topic                 time_zone_transition     
host                       time_zone_transition_type
ndb_binlog_index           user                     
plugin                   
sqlite> select * from user;         # 查询表格信息
....                                # 此处省略输出
sqlite> .exit                       # 退出
[root@localhost ~]#

# 导出其他数据库mytest, 这里出现了错误,待解决.
[root@localhost ~]# ./mysql2sqlite.sh -u root -p123456 mytest | sqlite3 mytest.sqlite
memory
SQL error near line 746: near "COMMENT": syntax error
SQL error near line 808: no such table: main.user_log
SQL error near line 809: no such table: main.user_log
[root@localhost ~]# ls

reference:

use raid

Linux下建立raid1

1、先用fdisk在硬盘上划分出空间。
    fdisk /dev/sdc  (后面的根据自己实际情况修改,因为我这里是虚拟机,
    所以就用2个500M的来做成一个raid)
    然后将其的功能改为 Linux raid autodetect 
    (也就是在fdisk下面 用t选择功能然后输入 -->fd)

2、建立raid
    mdadm -C /dev/md1 -l 1 -n 2 /dev/sda /dev/sdb
    下面是在分区上建raid
    mdadm -C /dev/md1 -l 1 -n 2 /dev/sda -n 2 /dev/sda1 /dev/sda2
    mdadm -C raid阵列所在 -l raid等级 -n 设备数 设备分别是什么
    建立完毕之后,可以通过cat /proc/mdstat 查看是否成功

3、格式化
    mkfs.ext3 /dev/md1

4、挂载
    mount /dev/md1 /raid1

5、建立mdadm.conf文件
    echo 'DEVICE /dev/sd[ab]' > /etc/mdadm.conf
    mdadm -Ds >> /etc/mdadm.conf

6. view raid
    mdadm -D /dev/md1
    cat /proc/mdstat
    mdadm -E /dev/sda
    mdadm -E /dev/sdb

7. 配置mdadm开机自启动 
    设备配置文件只能使系统在开机时正常启用RAID设备,但自动挂载RAID设备还是要再修改/etc/fstab
    #vi /etc/fstab
    ===============================================
    /dev/md0 /mnt/raid ext3 defaults 0 0
    ===============================================

8. 停止与开启RAID设备:
    停止:
    #umount /mnt/raid
    #mdadm -S /dev/md0
    mdadm: stopped /dev/md0
    开启:
    使用配置文件时:
    #mdadm -As /dev/md0
    mdadm: /dev/md0 has been started with 3 drives and 1 spare.
    没有使用配置文件时:
    #mdadm -A /dev/md0 /dev/sd[bcde]
    mdadm: /dev/md0 has been started with 3 drives and 1 spare.

9. 故障演示
    1).将一个磁盘标记为faulty,模拟硬盘坏损
    #mdadm /dev/md0 -f /dev/sdb
    2).查看RAID5重建过程
    #mdadm -D /dev/md0    
    #cat /proc/mdstat 
    3).查看完成坏损设备后的RAID状态
    #mdadm -D /dev/md0
    #cat /proc/mdstat 
    4).移除坏损设备
    #mdadm /dev/md0 -r /dev/sdb
    mdadm: hot removed /dev/sdb
    5).添加新的设备
    #mdadm /dev/md0 -a /dev/sdb
    mdadm: added /dev/sdb
    6).查看最终状态
    #mdadm -D /dev/md0
    #cat /proc/mdstat 

10. 删除整个RAID:
    mdadm /dev/md0 --fail /dev/sda --remove /dev/sda
    mdadm /dev/md0 --fail /dev/sdb --remove /dev/sdb
    mdadm --stop /dev/md0
    mdadm --misc --zero-superblock /dev/sda
    mdadm --misc --zero-superblock /dev/sdb
    即: 先删除RAID中的所有设备,然后停止该RAID即可

mdadm语法

基本语法 : mdadm [mode] [options]
[mode] 有7种:
    Assemble:将以前定义的某个阵列加入当前在用阵列。
    Build:   Build a legacy array ,每个device 没有 superblocks
    Create:  创建一个新的阵列,每个device 具有 superblocks
    Manage: 管理阵列,比如 add 或 remove
    Misc:   允许单独对阵列中的某个 device 做操作,比如抹去superblocks 或 终止在用的阵列。
    Follow or Monitor:监控 raid 1,4,5,6 和 multipath 的状态
    Grow:   改变raid 容量或 阵列中的 device 数目
可用的 [options]:
-A, --assemble:加入一个以前定义的阵列
-B, --build:Build a legacy array without superblocks.
-C, --create:创建一个新的阵列
-Q, --query:查看一个device,判断它为一个 md device 或是 一个 md 阵列的一部分
-D, --detail:打印一个或多个 md device 的详细信息
-E, --examine:打印 device 上的 md superblock 的内容
-F, --follow, --monitor:选择 Monitor 模式
-G, --grow:改变在用阵列的大小或形态
-h, --help:帮助信息,用在以上选项后,则显示该选项信息
--help-options
-V, --version
-v, --verbose:显示细节
-b, --brief:较少的细节。用于 --detail 和 --examine 选项
-f, --force
-c, --config= :指定配置文件,缺省为 /etc/mdadm/mdadm.conf
-s, --scan:扫描配置文件或 /proc/mdstat以搜寻丢失的信息。配置文件/etc/mdadm/mdadm.conf
create 或 build 使用的选项:
-c, --chunk=:Specify chunk size of kibibytes. 缺省为 64.
--rounding=: Specify rounding factor for linear array (==chunk size)
-l, --level=:设定 raid level.
--create可用:linear, raid0, 0, stripe, raid1,1, mirror, raid4, 4, raid5, 5, raid6, 6, multipath, mp.
--build可用:linear, raid0, 0, stripe.
-p, --parity=:设定 raid5 的奇偶校验规则:eft-asymmetric, left-symmetric, right-asymmetric, right-symmetric, la, ra, ls, rs.缺省为left-symmetric
--layout=:类似于--parity
-n, --raid-devices=:指定阵列中可用 device 数目,这个数目只能由 --grow 修改
-x, --spare-devices=:指定初始阵列的富余device 数目
-z, --size=:组建RAID1/4/5/6后从每个device获取的空间总数
--assume-clean:目前仅用于 --build 选项
-R, --run:阵列中的某一部分出现在其他阵列或文件系统中时,mdadm会确认该阵列。此选项将不作确认。
-f, --force:通常mdadm不允许只用一个device 创建阵列,而且创建raid5时会使用一个device作为missing drive。此选项正相反。
-a, --auto{=no,yes,md,mdp,part,p}{NN}:

other:

1). check file system type
    before check, should mount the device first
    [root@localhost ]# df -T /mnt/raid1/
    Filesystem    Type   1K-blocks      Used Available Use% Mounted on
    /dev/md0      ext3   961433496    204572 912390856   1% /mnt/raid1
    [root@localhost ]# mount
    ubi0 on / type ubifs (rw)
    proc on /proc type proc (rw)
    sysfs on /sys type sysfs (rw)
    devpts on /dev/pts type devpts (rw,gid=5,mode=620)
    none on /var/tmp type tmpfs (rw)
    /dev/md0 on /mnt/raid1 type ext3 (rw)
    [root@localhost ]# df -T
    Filesystem    Type   1K-blocks      Used Available Use% Mounted on
    ubi0         ubifs      201464    191516      9948  96% /
    none         tmpfs       62136         0     62136   0% /var/tmp
    /dev/md0      ext3   961433496    204572 912390856   1% /mnt/raid1

    reference:
    (1). Linux how to determine the file system type
        www.cyberciti.biz/faq/linux-how-to-determine-find-out-file-system-type/ 
2).如果想了解你的kernel目前支持哪些文件系统,可以查看/proc/filesystems的内容 
    reference:
    (1).linux /etc/fstab 文件说明 www.wokeke.com/?p=350 

Reference:

我的一些raid操作:

1. stop raid
    mdadm -S /dev/md0
    mdadm -S /dev/md1
    mdadm -S /dev/md126
    mdadm -S /dev/md127
2. create raid 1
    mdadm -Cv -l1 -n2 /dev/sda /dev/sdb --assume-clean --home-host=raidtest
3. check states
    cat /proc/mdstat

系统中原来插有两块盘,分别显示为/dev/sda /dev/sdb
如果把第一块盘/dev/sda拔掉,重启后,第二块盘自动识别为/dev/sda
如果原来/dev/sda和/dev/sdb建了raid1,拔掉/dev/sda并重启系统后,做如下操作:
[root@localhost ]# cat /proc/mdstat 
Personalities : [linear] [raid0] [raid1] 
md0 : active raid1 sda[1]
      976761424 blocks super 1.2 [2/1] [_U]  # 正常应该是[UU]

unused devices: <none>
[root@localhost ]# mdadm --detail /dev/md0
/dev/md0:
        Version : 1.2
  Creation Time : Fri Mar 29 06:48:49 2013
     Raid Level : raid1
     Array Size : 976761424 (931.51 GiB 1000.20 GB)
  Used Dev Size : 976761424 (931.51 GiB 1000.20 GB)
   Raid Devices : 2
  Total Devices : 1
    Persistence : Superblock is persistent

    Update Time : Fri Mar 29 07:17:39 2013
          State : clean, degraded
 Active Devices : 1
Working Devices : 1
 Failed Devices : 0
  Spare Devices : 0

           Name : mytestraid:0
           UUID : c76eab52:2e80d8e1:67772808:024ea0b6
         Events : 6

    Number   Major   Minor   RaidDevice State
       0       0        0        0      removed     # 这里看到有一块盘被移除了
       1       8        0        1      active sync   /dev/sda
[root@localhost ]# 


正常情况
[root@localhost ]# cat /proc/mdstat 
Personalities : [linear] [raid0] [raid1] 
md0 : active raid1 sdb[1] sda[0]
      976761424 blocks super 1.2 [2/2] [UU]

unused devices: <none>

拔掉一块raid盘,删掉剩下的raid盘数据,重启后插入有raid数据的盘
[root@localhost ]# cat /proc/mdstat 
Personalities : [linear] [raid0] [raid1] 
md127 : inactive sdb[0](S)
      976761560 blocks super 1.2

connect wifi in linux terminal

##1.工具介绍

wireless-tools

使用到的工具 iwlist、iwconfig、dhclient
只适用于WEP加密方式, WPA方式需要使用wpa_supplicant

wpa_supplicant

可用于WPA加密方式

##2.编译源码

wireless-tools

1). 下载 wireless_tools.29.tar.gz
wget http://www.hpl.hp.com/personal/Jean_Tourrilhes/Linux/wireless_tools.29.tar.gz

2). 编译、安装
tar xvf wireless_tools.29.tar.gz
cd wireless_tools.29
make
make install

wpa_supplicant

1). 下载
wget http://www.openssl.org/source/openssl-0.9.8e.tar.gz
wget http://hostap.epitest.fi/releases/wpa_supplicant-2.0.tar.gz

2). Reference:
http://blog.csdn.net/penglijiang/article/details/8573946
http://blog.csdn.net/ti_tantbx/article/details/7037741

##3.操作步骤

wireless_tools

注意: 操作前提,无线路由器使用WEP方式加密

使用步骤:

  • 使用命令ifconfig查找无线网卡接口,通常为wlan0
  • 使用命令iwlist扫描无线网络
  • 使用命令iwconfig设置要连接的无线网络
  • 使用命令iwconfig设置无线网络密码
  • 使用命令dhclient开始连接网络
  • 使用命令ifconfig和ping验证是否无线网络设置成功

下面是使用TP-Link无线网卡连接无线路由器的操作记录

[root@localhost ~]# ifconfig
[root@localhost ~]# iwlist wlan0 scanning
[root@localhost ~]# iwconfig wlan0 essid TP_Link_Test
[root@localhost ~]# iwconfig wlan0 key s:12349
[root@localhost ~]# dhclient wlan0
[root@localhost ~]# ifconfig wlan0
wlan0     Link encap:Ethernet  HWaddr 14:CF:92:23:B5:BF  
          inet addr:192.168.1.101  Bcast:255.255.255.255  Mask:255.255.255.0
          inet6 addr: fe80::16cf:92ff:fe23:b5bf/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:1141 errors:0 dropped:53252 overruns:0 frame:0
          TX packets:37 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000 
          RX bytes:425292 (415.3 KiB)  TX bytes:6687 (6.5 KiB)

说明:
iwconfig wlan0 essid TP_Link_Test 这里的 TP_Link_Test 是无线网络名称
iwconfig wlan0 key s:12349 这里的s:表示密码为ASCII格式

wpa_supplicant

1). 生成配置文件
其中TP_Link_Test为无线网络名称, 1234567890为无线网络密码
wpa_passphrase TP_Link_Test "1234567890" > /etc/wpa_supplicant.conf

2). 修改配置/etc/wpa_supplicant/wpa_supplicant.conf

ctrl_interface=/var/run/wpa_supplicant
ctrl_interface_group=wheel

network={
    ssid="TP_Link_Test"
    proto=WPA
    key_mgmt=WPA-PSK
    pairwise=TKIP
    group=TKIP
    #psk="1234567890"
    psk=eed346e10ee03dd93b9d25abbc117d58be9f619cf7d6aa1c7f3c080bbe94b84e
}

3). 连接网络
wpa_supplicant -Dwext -iwlan0 -c /etc/wpa_supplicant/wpa_supplicant.conf

执行命令后可能出现"Operation not permitted",看似是错误信息,不用管。  
[root@localhost ~]# wpa_supplicant -Dwext -iwlan0 -c /etc/wpa_supplicant/wpa_supplicant.conf 
ioctl[SIOCSIWAP]: Operation not permitted
Trying to associate with 1c:92:73:b0:02:33 (SSID='TP_Link_Test' freq=2412 MHz)
Associated with 1c:92:73:b0:02:33
WPA: Key negotiation completed with 1c:92:73:b0:02:33 [PTK=TKIP GTK=TKIP]
CTRL-EVENT-CONNECTED - Connection to 1c:92:73:b0:02:33 completed (auth) [id=0 id_str=]
WPA: Group rekeying completed with 1c:fa:68:b9:18:16 [GTK=TKIP]

4). 获取IP
dhclient wlan0

5). 检查是否获取成功
ifconfig wlan0
ping 网关:
ping 192.168.1.1

6). 断开网络
wpa_cli disable_network 0
wpa_cli remove_network 0
dhclient -x wlan0

Reference:

##4.其他参考资料

install skype on fedora18 x86_64

  1. 保存以下脚本为 skype-on-fedora18.sh

    #!/bin/sh
    ################################################################################
    # Install skype on fedora 18 x86_64
    # Reference: http://www.if-not-true-then-false.com/2012/install-skype-on-fedora-centos-red-hat-rhel-scientific-linux-sl/ 
    ################################################################################
    
    yum install -y alsa-lib.i686 fontconfig.i686 freetype.i686 glib2.i686 libSM.i686 libXScrnSaver.i686 libXi.i686 libXrandr.i686 libXrender.i686 libXv.i686 libstdc++.i686 pulseaudio-libs.i686 qt.i686 qt-x11.i686 zlib.i686
    
    yum install -y qtwebkit.i686
    
    cd /tmp
    
    ## Skype 4.1 Dynamic for Fedora ##
    wget --trust-server-names http://www.skype.com/go/getskype-linux-dynamic
    
    mkdir /opt/skype
    
    ## Extract Skype 4.1 on Fedora ##
    tar xvf skype-4.1* -C /opt/skype --strip-components=1
    
    ln -s /opt/skype/skype.desktop /usr/share/applications/skype.desktop
    ln -s /opt/skype/icons/SkypeBlue_48x48.png /usr/share/icons/skype.png
    ln -s /opt/skype/icons/SkypeBlue_48x48.png /usr/share/pixmaps/skype.png
    
    touch /usr/bin/skype
    chmod 755 /usr/bin/skype
    
    cat <<EOF >> /usr/bin/skype
    #!/bin/sh
    export SKYPE_HOME="/opt/skype"
    
    \$SKYPE_HOME/skype --resources=\$SKYPE_HOME \$*
    EOF
    
  2. 增加可执行权限 chmod +x skype-on-fedora18.sh

  3. 执行脚本 ./skype-on-fedora18.sh

  4. 安装完毕后运行 skype

Reference :

Install Skype 4.1/4.0 on Fedora 18/17, CentOS/RHEL/SL 6.3

use rsync

rsync 文件同步


Linux Server : 192.168.1.63

  1. vim /etc/rsyncd.conf

    # /etc/rsyncd.conf
    #全局配置部分
    secrets file = /etc/rsyncd.secrets
    read only = no
    write only = no
    list = yes
    uid = root
    gid = root
    charset=UTF-8
    
    #允许指定的主机连接
    #hosts allow = 192.168.1.52 172.18.1.50
    
    #允许任何主机
    host allow = * 
    
    #禁止指定的主机连接
    #hosts deny = 192.168.1.1/24
    
    use chroot = no
    max connections = 10
    log file = /var/log/rsyncd.log
    pid file = /var/run/rsyncd.pid
    lock file = /var/run/rsync.lock
    
    #模块配置
    [rsynctest]
    comment = sync directory
    path = /home/dennis/Documents
    auth users = rsync
    secrets file = /etc/rsyncd.secrets
    
  2. create password file

    echo "rsync:rsync" > /etc/rsyncd.secrets
    chown root:root /etc/rsyncd.secrets
    chmod 600 /etc/rsyncd.secrets    
    
  3. start rsync

    # 查询是否存在rsync进程
    ps aux | grep rsync
    # 启动rsync
    rsync --daemon
    # 检查 rsync 使用的873端口正常监听
    netstat -an | grep 873
    

Linux Clint : 192.168.1.52

  1. create password file

    # 该步骤主要用于解决同步过程需要手动输入密码问题
    echo "rsync" > /etc/rsyncd.secrets
    chown root:root /etc/rsyncd.secrets
    chmod 600 /etc/rsyncd.secrets
    
  2. download

    # 需要手动输入密码
    rsync -vzrtopg --delete --progress rsync@192.168.1.63::rsynctest /root/rsyntest/
    
    # 不需要输入密码
    rsync -vzrtopg --delete --progress --password-file=/etc/rsyncd.secrets rsync@192.168.1.63::rsynctest /root/rsyntest/
    
  3. upload

    # 需要手动输入密码
    rsync -vzrtopg --delete --progress ./*.log rsync@192.168.1.63::rsynctest
    
    # 不需要输入密码
    rsync -vzrtopg --delete --progress --password-file=/etc/rsyncd.secrets ./*.log rsync@192.168.1.63::rsynctest
    

Windows Clint : 192.168.1.42

  1. Install cwRsync_2.0.10

  2. create password file

    mkdir d:/rsync
    echo "rsync" > d:/rsync/rsyncd.secrets
    
  3. change to then install directory

    cd C:\Program Files\cwRsync\bin 
    
  4. download

    rsync -vzrtopg --progress --delete --password-file=/cygdrive/d/rsync/rsyncd.secrets rsync@192.168.1.63::rsynctest /cygdrive/d/rsync
    
  5. upload

    rsync -vzrtopg --progress --delete --password-file=/cygdrive/d/rsync/rsyncd.secrets /cygdrive/d/rsync rsync@192.168.1.63::rsynctest
    

xml parse

这里贴一个xml解析到shell脚本,主要使用了sed,awk,vi等命令的正则表达式。

xml.sh

#!/bin/sh
################################################################################
# 解析xml配置文件
# 历史记录:
#   2013-03-05    v1.0    Dennis  Create
################################################################################

xml_file=/etc/mytest/tmp.xml

# function: set item value
setitemvalue ()
{
    if [ $# -lt 2 ]; then
        echo Usage: setitemvalue item value
        return 1
    fi

    # sed 中使用变量,要用单引号围起来
    case $1 in
        "name" | "ip" | "port")
            #vi -e -s -c ":%s#$1=\"\S*\"#$1=\"$2\"#g" -c ":wq" $xml_file
            # 修改所有匹配行
            #sed -i 's#'$1'=\"\S*\"#'$1'="'$2'"#g' $xml_file
            # 只修改第一个匹配行
            sed -i '1,/'$1'/{s#'$1'=\"\S*\"#'$1'="'$2'"#g}' $xml_file 
            ;;
        *)
            #vi -e -s -c ":%s#<$1>.*</$1>#<$1>$2</$1>#g" -c ":wq" $xml_file
            sed -i 's#<'$1\>'.*<#<'$1'\>'$2'<#g' $xml_file 
            ;;
    esac
    return 0
}

# function: dispaly item value
show()
{
    # parse special item
    # 方式1, 可能出现重复行, 命令过长
      #name=`grep "Remote name" $xml_file | head -n 1 | awk '{print $2}' | cut -d"=" -f2 | cut -d'"' -f2`
    # 方式2, 使用 -m 参数解决重复行问题,但整个命令比较长
      #name=`grep -m 1 "Remote name" $xml_file | awk '{print $2}' | cut -d"=" -f2 | cut -d'"' -f2`
    # 方式3, 可能出现重复行
      #name=`sed -n '/Remote name/p' $xml_file | cut -d'"' -f2`
    # 方式4, 解决重复行,并且简化命令
    name=`grep -m 1 "Remote name" $xml_file | cut -d'"' -f2`
      ip=`grep -m 1 "Remote name" $xml_file | cut -d'"' -f4`
    port=`grep -m 1 "Remote name" $xml_file | cut -d'"' -f6`
    echo name:$name
    echo ip:$ip
    echo port:$port

    # parse general item
    awk -F'<|>' '{if(NF>3){print $2 ":" $3}}' $xml_file
}

# function: help
help ()
{
    echo "Usage:  xml.sh [--show] [--set] "
    echo "  --show display all item info "
    echo "  --set  set values of items "
    echo "    e.g: xml.sh --set ip=192.168.1.38 port=1394"
}

# main
if [ $# -lt 1 ]; then
    help
    exit 1
else
    case $1 in
        "--show")
            show
            ;;
        "--set")
            # 删掉第一个参数
            shift
            # 循环所有参数
            for i in $* ; do
                item=`echo $i | cut -d"=" -f1`
                value=`echo $i | cut -d"=" -f2`
                if [ $item ] && [ $value ]; then
                    setitemvalue $item $value
                else
                    echo parameters "$i" error!
                    exit 1
                fi
            done
            ;;
        *)
            help
            exit 1
            ;;
    esac
fi

exit 0

/etc/mytest/tmp.xml

<?xml version="1.0" encoding="UTF-8"?>
<xmltest>
    <Remote name="testpc" ip="192.168.1.56" port="1022"/>
    <version>3.6.12</version>
    <os>Centos 5.2</os>
    <memory>4.0</memory>
    <cpu>3.8</cpu>
</xmltest>