Press "Enter" to skip to content

月与灯依旧 Posts

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

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

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

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

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

Leave a Comment

Linux指定yum使用proxy server

1,在第三方机器上建立一个proxy server
推荐使用这个Repo,一个其于Pyhon的小巧的HTTP/HTTPS代理服务器,体积仅有200K。

git clone https://github.com/inaz2/proxy2.git
cd proxy2
python proxy2.py 3128

这样,一个建立在3128端口上的HTTP/HTTPS代理就建立好了。

2,配置本机使用HTTP代理

$ export http_proxy=http://172.22.193.65:3128
$ export https_proxy=http://172.22.193.65:3128

然后,你会发现,yum/wget/ez_install可以正常使用了。

curl的话,可以使用如下命令使用proxy server

$ curl -L --fail https://github.com/docker/compose/releases/download/1.12.0/run.sh > ~/1.sh -x http://172.22.193.65:3128

3,解决yum提示[Errno 14] Peer cert cannot be verified or peer cert invalid
这种是由于proxy server上面的ca证书不被认可,解决方案为Adding “sslverify=false” in the ‘/etc/yum.conf’ file。

$ vim /etc/yum.conf  #添加或修改如下选项
sslverify=false

$ yum repolist all
Leave a Comment

Dropbox vs Google Drive vs pcloud

本文来聊聊我最近使用的几个云存储服务吧。

大约两周前,我使用了近5年的Dropbox满了,达到了25G的容量,已无法再上传新文件,看了下Dropbox的收费方案,最低套餐也需要9.9美元/月(一年99美元),觉得略贵,于是只好找找其它的云存储服务了。

这首先映入眼帘的就是Google Drive。在官网查到,Google Drive最便宜的一款套餐为100G容量1.99美元/月(20美元/年),价格非常合适。而让我觉得放心的是,作为世界上属一属二的互联网公司,Google Drive的服务肯定不会差吧,于是掏出信用卡支付了20美元一年的费用……然尔事实证明,我还是too young too simple了。

首先,Google Drive的客户端不支持设置代理,这意示着,你的电脑必须可以全局穿墙才行,这是首先被我发现不如Dropbox的地方,但是对我这种大牛来说,也不算问题,我家的OpenWrt路由器上部署了SS服务,可以智能穿墙。所以,客户端不能设置支持代理,也无所谓了。在接下来的使用中,我发现了更加严重的问题。

近日购得Macbook Air一款,在macOS上安装了Google Drive客户端。我像迁移Dropbox那样,迁移了Google Drive里的文件。流程为:
1,在Windows上卸载Google Drive客户端;
2,在macOS上安装Google Drive客户端;
3,使用移动硬盘将Windows上面的Google Drive文件夹拷贝到macOS的同步目录下;

然后,神奇的事情发生了:
1,Google Drive文件夹里出现了大量的诸如”文件名.jpg”和”文件名(1).jpg”形式的重复文件,意思是说,对于重复上传的同一个文件,Google Drive并不会覆盖之,而是将它重命名为”文件名(1).jpg”(不仅仅是图片文件,任意格式的都是)。这意味着,如果你要更换一台电脑,你将不能使用拷贝文件的方式来迁移。(难道Google Drive的工程师都是吃翔的吗?同名的文件不会对比一下md5/sha1值再决定要不要覆盖吗??)
2,Google Drive文件夹里丢失了一部分子文件夹。我上网页版Google Drive看了一下,在网页版的回收站里找到了那些文件夹,我不知道Google Drive是如何判断我删除了那些文件夹的。

8 Comments

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
Leave a Comment

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
Leave a Comment

Python读取配置文件模块ConfigParser

1,ConfigParser模块简介
假设有如下配置文件,需要在Pyhton程序中读取

$ cat config.ini
[db]
db_port = 3306
db_user = root
db_host = 127.0.0.1
db_pass = xgmtest

[SectionOne]
Status: Single
Name: Derek
Value: Yes
Age: 30
Single: True

[SectionTwo]
FavoriteColor = Green
[SectionThree]
FamilyName: Johnson

[Others]
Route: 66

如何在Python中读取呢

>>> import ConfigParser
>>> Config = ConfigParser.ConfigParser()
>>> Config
<ConfigParser.ConfigParser instance at 0x00BA9B20>
>>> Config.read("config.ini")
['config.ini']
>>> Config.sections()
['db', 'Others', 'SectionThree', 'SectionOne', 'SectionTwo']
>>> Config.get("db", "db_host")
'127.0.0.1'
>>> Config.getint("db", "db_port")
3306
4 Comments

使用pytest进行批量测试

Pytest是python的一个测试模块,可以编写一些简单的测试用例,也可以用来进行一些批量测试,下面简单介绍一下。

1,安装

$ pip install -U pytest
or
$ easy_install -U pytest

$ py.test --version

2,基础语法
[code lang=”python”]
#!/usr/bin/python
import pytest

def func(x):
return x + 1

def test_answer(): #测试用例,方法需要以test_开头
assert func(3) == 5

运行之

$ py.test 2.py    #注意需要用py.test命令来运行之

稍微复杂一点儿的写法:
[code lang=”python”]
#!/usr/bin/python
import pytest

class TestClass:

def test_one(self):
x = "this"
assert "h" in x

def test_two(self):
x = 3
assert x > 2

Leave a Comment