适用代码收集

1 实现批量ping代码

python实现:

1 import os

2 for i in range(1,6):

3     ret=os.system('ping -c 1 -W 1 192.168.100.%d &> /dev/null' % i)

4     if not ret:

5         print 'ping 192.168.100.%d ok' % i
#!/usr/bin/env python

#coding:utf-8

import os



v=('192.168.100.%d' % (x) for x in range(1,6))

for it in v:

    ret=os.system('ping -c 1 -W 1 %s &> /dev/null' % it)

    if ret:

        print 'ping %s fail' % it

    else:

        print 'ping %s ok' % it

shell实现:

1 #!/bin/bash

2 

3 for i in {1..254}

4 do

5         if ping -c 1 -W 1 192.168.1.$i &> /dev/null

6         then

7                 echo 192.168.1.$i

8         fi  

9 done

2 windows系统访问linux系统主机上的文件的小脚本

1 open 服务IP

2 user 用户名 密码

3 bin

4 prompt

5 cd 文件路径

6 lcd 本地路径

7 mget *

8 bye

9 exit

保存为*.bat文件,执行:ftp -n -s:"*.bat"

3 c++线程封装(收藏自http://bbs.chinaunix.net/thread-3754383-2-2.html

 1 #include <my_pthread.h>

 2 #include <stdio.h>

 3 

 4 struct foo

 5 {

 6     int n;

 7     void *test(void *arg)

 8     {

 9         for(int i = 0; i < 10; ++i)

10             printf("%d \n", ++n);

11                 return NULL;

12     }

13 };

14 

15 int main()

16 {

17     foo f;

18     pthread_t id;

19     my_pthread_create(&f, &id, NULL, &foo::test, NULL);

20     pthread_join(id, NULL);

21     return 0;

22 }

以下为 my_pthread.h

 1 #ifndef INC_THREAD_PROXY_H_

 2 #define INC_THREAD_PROXY_H_

 3 

 4 #include <pthread.h>

 5 #include <semaphore.h>

 6 

 7 template <class T>

 8 class thread_proxy_imp

 9 {

10 protected:

11     typedef struct

12     {

13         void *(T::*start_routine_)(void *);

14         void      *arg_;

15         T         *obj_;

16         sem_t      sem_;

17     } thread_param;

18 

19     static void *thread_proxy_entry(void *arg)

20     {

21         thread_param *param_ptr = reinterpret_cast<thread_param *>(arg);

22         thread_param  param     = *param_ptr;

23         sem_post(&param.sem_);

24         return (param.obj_->*param.start_routine_)(param.arg_);

25     }

26 

27 public:

28     int create(

29         T                    *obj,

30         pthread_t            *thread,

31         const pthread_attr_t *attr,

32         void            *(T::*start_routine) (void *),

33         void                 *arg)

34     {

35         int rt;

36         thread_param param = {start_routine, arg, obj};

37         

38         sem_init(&param.sem_, 0, 0);

39         rt = pthread_create(thread,

40                             attr,

41                                                         thread_proxy_imp::thread_proxy_entry,

42                                                         reinterpret_cast<void *>(&param));

43         if(rt == 0) sem_wait(&param.sem_);

44         sem_destroy(&param.sem_);

45         return rt;

46     }

47 };

48 

49 

50 template<class T>

51 int my_pthread_create(

52         T                    *obj,

53     pthread_t            *thread,

54     const pthread_attr_t *attr,

55     void            *(T::*start_routine) (void *),

56     void                 *arg)

57 {

58     return thread_proxy_imp<T>().create(obj, thread, attr, start_routine, arg);

59 }

60 

61 #endif

 

你可能感兴趣的:(代码)