bash递归检查文件中是否包含IP地址

检查项目组提交的代码以及配置文件中,是否包含写死IP地址的情况

#!/bin/bash

regex_ip="(2[0-4][0-9]|25[0-5]|1[0-9][0-9]|[1-9]?[0-9])(\.(2[0-4][0-9]|25[0-5]|1[0-9][0-9]|[1-9]?[0-9])){3}"
if [ ! -n "$1" ] ;then  
    echo "Usage:check_ip.sh [CHECK_PATH]"  
else  
    grep -rE "$regex_ip" $1
fi  

golang实现tcp反代/转发

package main

import (
    "bytes"
    "flag"
    "io"
    "log"
    "net"
    "os"
    "strings"
    "time"
)

type formatter struct {
    w      io.Writer
    offset uint
    prefix string
    tstamp bool
}

func min(a, b int) int {
    if a < b {
        return a
    }

    return b
}

func (f *formatter) Write(p []byte) (int, error) {
    var completedBytes int
    for i := 0; i < len(p); i += 16 {
        m := min(len(p), i+16)
        err := f.format(p[i:m])
        if err != nil {
            return completedBytes, err
        }
        completedBytes += m - i
        f.offset += uint(m - i)
    }
    return len(p), nil
}

var hex = []byte("0123456789abcdef")

func (f *formatter) format(buf []byte) error {

    // prefix addr:_(hex dump)+spaces+space+bar+chars+bar+newline

    // our line is 83 characters of formatting

    plen := len(f.prefix)

    if f.tstamp {
        plen += len(time.Stamp)
    }

    llen := 83 + plen

    ptr := 0

    line := make([]byte, llen)
    if f.tstamp {
        s := time.Now().Format(time.Stamp)
        copy(line, []byte(s))
        ptr += len(time.Stamp)
    }

    copy(line[ptr:], []byte(f.prefix))
    ptr += len(f.prefix)

    offs := f.offset

    ptr += 8

    line[ptr] = ':'
    ptr--

    for offs > 0 {
        line[ptr] = hex[offs&0x0f]
        ptr--
        offs >>= 4
    }

    for ptr >= plen {
        line[ptr] = '0'
        ptr--
    }

    ptr = plen + 9
    line[ptr] = ' '
    ptr++

    for i, b := range buf {
        if i%4 == 0 {
            line[ptr] = ' '
            ptr++
        }

        line[ptr] = hex[b>>4]
        ptr++
        line[ptr] = hex[b&0x0f]
        ptr++
        line[ptr] = ' '
        ptr++

    }

    // fill in rest of line
    for i := len(buf); i < 16; i++ {
        if i%4 == 0 {
            line[ptr] = ' '
            ptr++
        }

        line[ptr] = ' '
        ptr++
        line[ptr] = ' '
        ptr++
        line[ptr] = ' '
        ptr++
    }

    line[ptr] = ' '
    ptr++
    line[ptr] = ' '
    ptr++
    line[ptr] = '|'
    ptr++

    for _, v := range buf {
        if v > 32 && v < 127 {
            line[ptr] = v
        } else {
            line[ptr] = '.'
        }
        ptr++
    }

    line[ptr] = '|'
    ptr++

    line[ptr] = '\n'
    ptr++

    _, err := f.w.Write(line[:ptr])
    return err
}

func copyStream(conn io.WriteCloser, r io.Reader) {
    _, err := io.Copy(conn, r)
    if err != nil {
        log.Printf("error during copy: %v\n", err)
    }
    err = conn.Close()
    if err != nil {
        log.Printf("error during close: %v\n", err)
    }
}

func main() {

    proxy := flag.String("p", "9999:blog.bbzhh.com:80", "proxy line -- <lport>:<rhost>:<rport>")
    tstamps := flag.Bool("t", false, "add time-stamps when proxying")
    debug := flag.Bool("d", false, "print debug information when proxying")

    flag.Parse()

    // provided a proxy line
    if *proxy != "" {
        pieces := strings.Split(*proxy, ":")
        dst := pieces[1] + ":" + pieces[2]

        fprefix := "<= "
        tprefix := "=> "

        if *tstamps {
            fprefix = " <= "
            tprefix = " => "
        }
        var (
            fin  io.Writer
            fout io.Writer
        )
        if *debug {
            fin = &formatter{os.Stdout, 0, fprefix, *tstamps}
            fout = &formatter{os.Stdout, 0, tprefix, *tstamps}
        } else {
            buf := bytes.NewBuffer(make([]byte, 0))
            fin = &formatter{buf, 0, fprefix, *tstamps}
            fout = &formatter{buf, 0, tprefix, *tstamps}
        }

        ln, e := net.Listen("tcp", ":"+pieces[0])
        if e != nil {
            log.Fatal("listen error:", e)
        }

        log.Println("tcp server starting")

        for {
            lconn, err := ln.Accept()
            if err != nil {
                log.Println(err)
                continue
            }

            go func(lconn net.Conn) {
                tl := io.TeeReader(lconn, fout)
                rconn, err := net.Dial("tcp", dst)
                if err != nil {
                    log.Println("error connectiong to", dst, ":", err)
                    err := lconn.Close()
                    if err != nil {
                        log.Printf("error closing connection: %v\n", err)
                    }
                    return
                }
                tr := io.TeeReader(rconn, fin)
                go copyStream(rconn, tl)
                go copyStream(lconn, tr)
            }(lconn)
        }
    }

    if *tstamps {
        log.Println("-t only applies when proxying, ignoring")
    }

    fout := &formatter{os.Stdout, 0, "", false}

    var fin io.Reader

    // process stdin
    if flag.NArg() == 0 {
        fin = os.Stdin
    } else {
        fname := flag.Arg(0)
        var err error
        fin, err = os.Open(fname)
        if err != nil {
            log.Fatal(err)
            return
        }
    }

    _, err := io.Copy(fout, fin)
    if err != nil {
        log.Printf("error during copy: %v\n", err)
    }
}

tornado微信公众号

#!/usr/bin/env python3
# -*- coding: UTF-8 -*-

__author__ = 'tan9le'

import os
from wechatpy import parse_message, create_reply
from wechatpy.utils import check_signature
from wechatpy.exceptions import (
    InvalidSignatureException,
)
import schedule
from datetime import timedelta
import tornado.wsgi
from tornado.options import define, options

TOKEN = os.getenv('WECHAT_TOKEN', '填写token')
AES_KEY = os.getenv('WECHAT_AES_KEY', '填写EncodingAESKey(消息加解密密钥)')
APPID = os.getenv('WECHAT_APPID', '填写AppID(应用ID)')

define("port", default=8000, help="run on the given port", type=int)
class WeiXinHandler(tornado.web.RequestHandler):
    #用于微信公众号网页修改基本信息时的验证
    def get(self):
        # 获取微信公众平台发送的验证参数
        signature = self.get_argument('signature', '')
        timestamp = self.get_argument('timestamp', '')
        nonce = self.get_argument('nonce', '')
        echostr = self.get_argument('echostr', '')
        try:
            check_signature(TOKEN, signature, timestamp, nonce)
        except InvalidSignatureException:
            self.set_stauts('403')
            self.write('error,code 403')
        #按照约定,如果正常,则原样返回echostr
        self.write(echostr)
    def post(self):
        # 获取微信公众平台发送的验证参数
        signature = self.get_argument('signature', '')
        timestamp = self.get_argument('timestamp', '')
        nonce = self.get_argument('nonce', '')
        echostr = self.get_argument('echostr', '')
        # 获取所有值并解析
        msg = parse_message(self.request.body)
        if msg.type == 'text':
            content = msg.content.strip()
            
            reply = create_reply(content, msg)
            self.write(reply.render())
        else:
            help_str = "目前仅支持文字输入\n"
            reply = create_reply(help_str, msg)
            self.write(reply.render())




app = tornado.wsgi.WSGIApplication([
    # 这里需要根据修改为自己的URL匹配
    (r"/", WeiXinHandler),

])

if __name__=="__main__":
    # 启动tornado实例
    tornado.options.parse_command_line()
    http_server = tornado.httpserver.HTTPServer(app)
    http_server.listen(options.port)
    tornado.ioloop.IOLoop.instance().start()

然后使用

python main.py --port=8000

就可以启动应用啦
依赖:

tornado
wechatpy

windows上自动设置java环境变量的脚本

@echo off

:: TODO:设置java环境变量
:: Author: Gwt
color 02
::设置java的安装路径,可方便切换不同的版本
set input=
set /p "input=请输入java的jdk路径(或回车默认路径为C:\Program Files\Java\jdk1.7.0_71):"
if defined input (echo jdk已设置) else (set input=C:\Program Files\Java\jdk1.7.0_71)
echo jdk路径为%input%
set javaPath=%input%

::如果有的话,先删除JAVA_HOME
wmic ENVIRONMENT where "name='JAVA_HOME'" delete

::如果有的话,先删除ClASS_PATH 
wmic ENVIRONMENT where "name='CLASS_PATH'" delete

::创建JAVA_HOME
wmic ENVIRONMENT create name="JAVA_HOME",username="<system>",VariableValue="%javaPath%"

::创建CLASS_PATH
wmic ENVIRONMENT create name="CLASS_PATH",username="<system>",VariableValue=".;%%JAVA_HOME%%\lib\tools.jar;%

%JAVA_HOME%%\lib\dt.jar;"

::在环境变量path中,剔除掉变量java_home中的字符,回显剩下的字符串
call set xx=%Path%;%JAVA_HOME%\jre\bin;%JAVA_HOME%\bin

::echo %xx%

::将返回显的字符重新赋值到path中
wmic ENVIRONMENT where "name='Path' and username='<system>'" set VariableValue="%xx%"

pause

解释下命令

   @echo off 是关闭回显的,不会显示命令信息 on打开会显示命令信息

  color 02是设置输出文本颜色的,这里是控制命令台输出绿颜色

  set /p "input=请输入命令信息" 是用来接收控制台输入的文本信息的

  if else 是用来做判断 if defined input 是用来判断用户是否输入信息,回车的话,则表示未定义input的值

  echo "输出信息" 是用来显示信息的

  set javaPath=%input% 是用来吧变量input的值赋值给javaPath变量的

  wmic 是提供了批处理的命令,可以方便的操作环境变量值

  删除某一环境变量 就使用 wmic ENVIRONMENT where "name='JAVA_HOME'" delete命令

  创建某一环境变量 使用 wmic ENVIRONMENT create name="JAVA_HOME",username="<system>",VariableValue="%javaPath%"命令

      修改某一环境变量 使用 wmic ENVIRONMENT where "name='Path' and username='<system>'" set VariableValue="value"

注意事项:

  1、在上述批处理脚本命令中,要严格注意空格和大小写等的输入,不然执行不成功的

  2、要以管理员方式运行命令,不然提示拒绝执行命令的提示

解决MacOS下readlink: illegal option -- f

Mac下的readlink没有-f参数,诸如screenfetch又会去调用readlink -f,于是每次都会出现:

readlink: illegal option -- f
usage: readlink [-n] [file ...]

很是烦人,解决方案如下:
1.安装coreutils:

brew install coreutils

2.设置环境变量,编辑~/.bash_profile,添加:

export PATH="/usr/local/opt/coreutils/libexec/gnubin:$PATH"

3.创建别名,使用greadlink替代readlink,同样是编辑~/.bash_profile,添加:

alias readlink=greadlink