在OpenWrt/LEDE系统中处理JSON

在OpenWrt/LEDE系统中处理JSON的方法,这里记录一下。

假设有一个JSON文件

cat /etc/ss.json 
{
	"server": "8.8.8.8",
	"server_port": 443,
	"local_address": "0.0.0.0",
	"local_port": 7070,
	"password": "fuckgfang",
	"method": "aes_128_ctr",
	"timeout": "60",
	"protocol": "auth_aes128_sha1",
	"fast_open": false
}

处理方式1

$ jsonfilter -i /etc/ss.json -e "@.server"
8.8.8.8

$ CONF=/etc/ss.json
$ a=$(jsonfilter -i $CONF -e "@.server")
$ echo $a
8.8.8.8

(more…)

Read More

Linux详细查看进程启动时间

Linux系统下查看进程启动时间和运行时间的命令

ps -o lstart -p PID         #根据PID来查询

ps -o lstart,etime -p PID   #根据PID来查询,打印出启动时间和已经运行的时间

ps -eO lstart | grep PROCESS    #根据进程名字查询

提示:
1, 大写的O表示preloaded,小写的o表示user-defined,e表示all processes
2, 如果查出来的时间是1-16:15:58, 则表示该运程运行了1天16小时15分钟
3, etime表示elapsed time, 即程序已经运行的时间

以下几个例子

$ ps -o lstart,etime -p 13062    #根据PID打印出启动时间和持续时间
                 STARTED     ELAPSED
Thu Sep 21 09:41:40 2017  1-01:50:35

$ ps -eo pid,lstart,cmd    #打印出所有进程(-e)的PID,启动时间
3428 Mon Nov  6 20:55:20 2017 /sbin/udevd -d
3495 Mon Nov  6 20:55:22 2017 /sbin/rsyslogd -i /var/run/syslogd.pid -c 5
3508 Mon Nov  6 20:55:22 2017 /usr/sbin/nscd
3532 Mon Nov  6 20:55:29 2017 /usr/sbin/snmpd -LS 2 d -Lf /dev/null -p /var/run/snmpd.pid -a
3548 Mon Nov  6 20:55:29 2017 /usr/sbin/sshd
3557 Mon Nov  6 20:55:29 2017 ntpd -u ntp:ntp -p /var/run/ntpd.pid -g
3568 Mon Nov  6 20:55:29 2017 crond


#依次打印出某进程的pid,uid,user,启动时间,持续时间,占用CPU时间(cputime),以及cmd
$ ps -eo pid,euid,euser,lstart,etime,cputime,cmd | grep searcher_server
 7259  7259 Fri Sep 22 11:36:21 2017       00:01 S pts/0    00:00:00 grep 13062
13062 13062 Thu Sep 21 09:41:40 2017  1-01:54:42 S ?        1-15:50:04 /export/App/jd_search/searcher/server/bin/searcher_server
Read More

阿里云/京东云等进行端口转发

假设我们有个应用建立在国外的某个服务器上(如8.8.8.8:443端口上),现在希望使用阿里云/京东云等作为跳板来转发一下这个端口

开启端口转发

vim /etc/sysctl.conf
将 net.ipv4.ip_forward=0
修改成 net.ipv4.ip_forward=1

编辑后使用命令让配置马上生效
sysctl -p

相同端口的转发(将阿里云的443端口转发到8.8.8.8的443端口)

iptables -t nat -A PREROUTING -p tcp --dport 443 -j DNAT --to-destination 8.8.8.8
iptables -t nat -A PREROUTING -p udp --dport 443 -j DNAT --to-destination 8.8.8.8

iptables -t nat -A POSTROUTING -p udp -d 8.8.8.8 --dport 443 -j SNAT --to-source 192.168.0.3
iptables -t nat -A POSTROUTING -p tcp -d 8.8.8.8 --dport 443 -j SNAT --to-source 192.168.0.3

请注意:一般来说,阿里云/京东云有个内网IP和公网IP,我们假设这里的192.168.0.3是阿里云/京东云的内网IP,这里不能写公网IP

不同端口的转发(将阿里云的5270端口转发到8.8.8.8的443端口)

iptables -t nat -A PREROUTING -p udp --dport 5270 -j DNAT --to-destination 8.8.8.8:443
iptables -t nat -A PREROUTING -p udp --dport 5270 -j DNAT --to-destination 8.8.8.8:443

iptables -t nat -A POSTROUTING -p udp -d 8.8.8.8 --dport 443 -j SNAT --to-source 192.168.0.3
iptables -t nat -A POSTROUTING -p tcp -d 8.8.8.8 --dport 443 -j SNAT --to-source 192.168.0.3
Read More

运维实例:批量替换配置文件+批量重启服务

工作中遇到的一个小问题,记录一下。假设有一千台机器运行着nginx但每台机器上的nginx目录可能不太一样,比如
/export/servers/openresty-local/nginx/
/export/servers/openresty/nginx/
/export/servers/openresty-ngx-ump/nginx/
现在要替换掉nginx目录下的某个脚本,并且重启这个nginx服务。需要用到的工具有Ansbile和自己写的批量替换脚本batch_op.py

#准备远程机器列表
$ vim ip_list
10.191.172.201
10.190.143.38
10.187.110.4
10.190.49.237
10.190.198.192
10.190.163.211
#准备替换脚本
$ vim /tmp/b.sh
#!/bin/bash

if [ $(id -u) != "0" ]; then
    echo "Error: need root to run this script."
    exit 1
fi

a=`ps -ef | grep openresty | grep -v grep | awk '{ print $11 }' | cut -d\/ -f1,2,3,4`

if [ ! $a ]; then
    echo 'Error! Not found $a directory.'
elif [ -f $a/lualib/resty/nginx_diviner.lua ]; then
   cp $a/lualib/resty/nginx_diviner.lua $a/lualib/resty/nginx_diviner.lua.bak.`date +%Y%m%d%H%M%S` && \
   wget -q -O $a/lualib/resty/nginx_diviner.lua http://172.22.193.65:8003/nginx_diviner.lua && \
   chown admin:admin $a/lualib/resty/nginx_diviner.lua* && \
   $a/nginx/sbin/nginx -s reload && \
   echo "Found and replaced $a/lualib/resty/nginx_diviner.lua"
else
   echo "Not Found $a/lualib/resty/nginx_diviner.lua"
   exit 1
fi

if [ "$a" != "/export/servers/openresty" ] && [ -f /export/servers/openresty/lualib/resty/nginx_diviner.lua ]; then
   cp /export/servers/openresty/lualib/resty/nginx_diviner.lua /export/servers/openresty/lualib/resty/nginx_diviner.lua.bak.`date +%Y%m%d%H%M%S` && \
   wget -q -O /export/servers/openresty/lualib/resty/nginx_diviner.lua http://172.22.193.65:8003/nginx_diviner.lua && \
   chown admin:admin /export/servers/openresty/lualib/resty/nginx_diviner.lua && \
   echo "Extra Operation! also found and replaced /export/servers/openresty/lualib/resty/nginx_diviner.lua"
fi

(more…)

Read More

Excel转换txt & 批量筛选IP & 批量扫描服务器信息

假设,有一个智障同事,发了一个Excel过来(里面含有多张表格),要求扫描里面所有的IP是否有网卡故障,比如下面这样的
Excel转换txt

先不说IP数量多少的问题,里面有多张表格,并且有多个列,每一列都有不少IP,IP呢,还有分为172开头的(物理机),和10开头的(Docker),看起来好像复杂死了。

这里说一下我的处理方式。

找一个Excel转换成JSON/TXT的工具(比如这个Excel To Formula View),转换以后的效果如下
Excel To Formula View (more…)

Read More

ansible文件内容替换+在远程主机上运行多个command

ansible在远程主机上进行文件内容替换可以使用lineinfile来实现,可以使用正则筛选到所需要的行,然后进行替换,本文进行了演示。

ansible在远程主机上运行多个command,标准写法为

  - name: install an apple
    command: "{{item}}"
    with_items:
     - tar -xvzf /tmp/apple.tar.gz creates=/tmp/apple
     - /tmp/apple/install.sh --default creates=/etc/apple
     - rm -rf /tmp/apple

实例
假设有一个process.ymal

- hosts: new
  user: admin
  vars:
     ps_env: "{{ ps_env }}"
  tasks:
   - name: push ~/.ssh/id_rsa/pub to remote .ssh/authorized_keys
     authorized_key: user=admin state=present key="{{ lookup('file', '/home/admin/.ssh/id_rsa.pub') }}"

   - name: push ump_agent_code to remote
     copy: src=/opt/zhukun/ump_agent/bin dest=/home/admin/zhukun owner=admin group=admin mode=644 backup=yes follow=yes

   - name: prepare the product/test enviroment of PS
     command: sed -i "s,stage,{{ ps_env }},g" /home/admin/zhukun/bin/config.json

   - name: prepare the product/test enviroment of PS
     lineinfile:
       dest: /home/admin/zhukun/bin/config.json
       regexp: '^.*[PRODUCT].*$'
       line: '"PRODUCT": "{{ ps_env }}"'

   - name: reboot the observer-agent service
     command: "{{ item }}"
     with_items:
      - ps -ef | grep /export/App/observer-agent.jd.local/bin | grep -v grep | awk '{ print $2}' | xargs kill
      - /export/App/observer-agent.jd.local/bin/start.sh

   - name: reboot the PS service
     command: ps -ef | grep /export/Packages/prediction_service/latest/prediction_service | grep -v grep | awk '{ print $2}' | xargs kill

(more…)

Read More

ansible批量推送ssh key+批量推送文件(夹)

ansible批量推送ssh key的方法,这里记录一下。

假设有以下主机列表

$ cat /opt/list
[new]
172.20.121.109
172.20.114.69
172.20.114.57
172.20.114.58
172.20.114.59

1,使用ansible命令来推送SSH Key
命令比较简单,一行即可搞定

$ ansible -i /opt/list new -m authorized_key -a "user=admin key='{{ lookup('file','/home/admin/.ssh/id_rsa.pub') }}'" -k

这里解释一下命令:
-i表示指定IP列表文件位置,后面的new表示文件里的分组
-k表示询问目标IP的密码
-a后面表示authorized_key模块的参数,user和key是2个必需的参数,也可以加一个默认参数state=present(加不加均可),如果设置state=absent则表示删除这一条key
经测试,反复运行多次此命令,远程机器上的~/.ssh/authorized_keys文件里只会存在一行,不会存在多行key。

2,使用playbook来推送SSH key

$ vim key.ymal 
- hosts: new
  user: admin
  tasks:
   - name: Set authorized key took from file
     authorized_key: user=admin state=present key="{{ lookup('file', '/home/admin/.ssh/id_rsa.pub') }}"

文件解释:
hosts: 表示/opt/list文件里的分组
user: 表示本地用户,这里如果写错可能导致执行失败

$ ansible-playbook -i /opt/list key.ymal -k

(more…)

Read More