使用python在linux终端打印二维码

其实主要就是使用qrcode.QRCode.print_ascii()方法在终端画二维码。接下来介绍使用方法。

准备python环境

sudo pip install qrcode

实际代码

$ python
Python 3.6.7 (default, Oct 22 2018, 11:32:17) 
[GCC 8.2.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> 
>>> import qrcode
>>> 
>>> str = 'https://www.zhukun.net'
>>> qr = qrcode.QRCode()
>>> qr.border = 1
>>> qr.add_data(str)
>>> qr.make()
>>> qr.print_ascii(out=None, tty=False, invert=False)
 ▄▄▄▄▄▄▄  ▄    ▄   ▄▄▄▄▄▄▄ 
 █ ▄▄▄ █ ██ █▄▀▀▄█ █ ▄▄▄ █ 
 █ ███ █ ▀▀▄▀▄█▀ ▄ █ ███ █ 
 █▄▄▄▄▄█ ▄▀█ █▀█ █ █▄▄▄▄▄█ 
 ▄     ▄ █▀▀  ▄▄▀ ▄▄  ▄▄▄  
  ▀▀ ▄ ▄▀█▄ ▀▄███▄█▄███▀█▄ 
  █▀█▀▀▄▀▀▀▄ ▀  ▄▀▀▄▀ ▀  █ 
 █▀ ▀▀ ▄ █▀▀ ▀ ▄█▀█▄█▄▄ █▄ 
 █ ▄▀█▄▄▀▀ ▀ █▄▄ ██▄█▄▀█ ▀ 
 ▄▄▄▄▄▄▄ ▀ █ ▄▄▀ █ ▄ █   ▄ 
 █ ▄▄▄ █  ▀▀█   ██▄▄▄█ ▄▄▄ 
 █ ███ █  ▀▀ ▀██▄▄ ▀  ▄▄▀█ 
 █▄▄▄▄▄█ ▄ █▄██ ▀██ ▀▀▄  █ 
Read More

systemd service log check

systemd服务一般使用systemctl命令来启动, 那么如果服务启动不正常, 如何查看它的日志呢?

journalctl -u myapp.service

如果想查看实时日志输出( follow logs in real time ), 可以加一个-f参数:

journalctl -u -f myapp.service

另外2个有用的参数:

-e          直接打印到日志末尾;
--no-pager  打印全部日志(无需再翻页).

其实systemctl也是可以直接查看日志的:

systemctl -l status myapp.service
Read More

ansible use timestamp as vars

ansible 使用时间戳作为变量

- hosts: app_group
  remote_user: root
  gather_facts: yes    # 需要设定为yes(也可以省略),不然会提示ansible_date_time' is undefined
  vars:
    - VERSION:  v1.0.0
    - GIT_APP_NAME: HelloWorld
    - GIT_ADDR: [email protected]/yourname/helloworld.git
    - LOCAL_CODE_DIR: '/opt/ansible/yourname/code'
    - CMD_TIME: "{{ ansible_date_time.iso8601_basic_short }}"    # 获取系统时间戳
  tasks:

    - name: sync code to build server
      synchronize:
        src: "{{ LOCAL_CODE_DIR }}/{{ GIT_APP_NAME }}/" 
        dest: "/opt/src-{{ VERSION }}-{{ CMD_TIME }}"

    - name: 编译
      shell: "cd /opt/src-{{ VERSION }}-{{ CMD_TIME }}; make clean && make -j12;"

    - name: 同步新版本文件到本地
      synchronize
        src: "/opt/src-{{ VERSION }}-{{ CMD_TIME }}/helloworld"
        dest: "{{ LOCAL_CODE_DIR }}/helloworld_online/"
        mode: pull

    - name: 同步新版本文件到线上
      synchronize:
        src: "{{ LOCAL_CODE_DIR }}/helloworld_online/helloworld"
        dest: "/opt/helloworld/helloworld"

    - name: 计算线上md5值
      shell: "md5sum /opt/helloworld/helloworld |awk '{print $1}'"
      register: helloworld_online

    - name: 计算本地md5值
      shell: "md5sum {{ LOCAL_CODE_DIR }}/helloworld_online/helloworld | awk '{print $1}'"
      register: helloworld_local
      connection: local    # 表示在本地执行相关命令

    - name: md5 check
      debug:
        msg: 'remote: {{ helloworld_online.stdout }} local: {{ helloworld_local.stdout }}'

    - name: 本地与线上md5值对比成功重启服务(随机sleep 5秒,确保服务可用率)
      when: helloworld_online.stdout == helloworld_local.stdout
      shell: "sleep $((RANDOM % 5)) && cd /opt/helloworld/ && sh ./start.sh"
      args:
        executable: /bin/bash

Read More

Linux shell get local ip address

Linux shell中获得本机IP的相关命令, 你知道几个?

hostname -I     # 不同系统返回不一样, 有些可能需要cut一下
ifconfig eth0 | grep inet | grep -v inet6 | awk '{print $2}'
ifconfig | awk '/inet addr/{print substr($2,6)}'
ifconfig | sed -n 's/.*inet addr:\([0-9.]\+\)\s.*/\1/p'
ip a s|sed -ne '/127.0.0.1/!{s/^[ \t]*inet[ \t]*\([0-9.]\+\)\/.*$/\1/p}'
ifconfig | perl -nle 's/dr:(\S+)/print $1/e'
curl ifconfig.me      # 适用于有公网IP的情况
Read More

Linux下的双向文件同步工具

最近有在2台机器双向同步文件的需求, 于是有了本文. 上网找了一些双向文件同步的工具, 比较典型的有syncthingmirror. 本文介绍mirror的使用方法(不过好像syncthing使用更广泛一些).

  1. 在2台机器上安装好java1.8版本(ubuntu使用sudo apt install openjdk-8-jre命令即可安装);
  2. 在2台机器上安装好Watchman(安装方法);
  3. 在2台机器上安装mirror工具(安装方法见github页面)

假设你的mirror安装在/data/apps/mirror/目录下, 2台机器需要同步/data/sftp/文件夹.

在server端运行mirror程序

写入systemd系统服务

$ vim /etc/systemd/system/mirror.service    #写入如下内容
[Unit]
Description=Mirror (File sync for two sftp servers[10.16.19.21,10.16.19.24])
After=network-online.target
After=network-online.target
Requires=network-online.target

[Service]
Type=simple
ExecStart=/bin/bash -l -c '/data/apps/mirror/mirror server'
Restart=on-failure

[Install]
WantedBy=multi-user.targe

启动程序

$ systemctl daemon-reload
$ systemctl restart mirror.service && systemctl status mirror.service
$ systemctl enable mirror.service

(more…)

Read More