Shell将换行符换成空格(sed)

Shell将换行符换成空格的办法,用sed和tr均可实现。

sed命令

cat country.txt | sed ‘:label;N;s/\n/ /;b label’

tr命令

cat country.txt | tr "\n" " "

把tab换成换行符

cat country.txt | sed ‘s/\t/\n/g’

awk ‘BEGIN { OFS = "\n" } { $1=$1; print }’ input.txt > output.txt

tr ‘\t’ ‘\n’ < input.txt > output.txt

参考文章:
Convert a tab-delimited file to use newlines
sed命令替换换行符

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

为Nginx添加basic_auth

为Nginx添加basic_auth,意思就是访问页面的时候需要弹出来一个用户和密码验证的东西,本文基于CentOS 6

1, 安装密码生成工具htpasswd并生成用户密码文件

yum install httpd-tools               #适用centos
sudo apt-get install apache2-utils    #适用ubuntu

生成用户密码文件

$ htpasswd -c /var/www/html/.htpasswd user1  #回车会要求输入两遍密码,会清除所有用户!
$ htpasswd -bc /var/www/html/.htpasswd user1 password  #不用回车,直接指定user1的密码为password
$ htpasswd -b /var/www/html/.htpasswd user2 password   #添加一个用户,如果用户已存在,则是修改密码
$ htpasswd -D /var/www/html/.htpasswd user2  #删除用户

2, 为Nginx添加basic_auth配置

server {
	listen		80;
#	root		/tmp;
#	index		index.html index.htm;
	server_name	zhukun.net www.zhukun.net;

	location / {
		auth_basic		"input you user name and password";
		auth_basic_user_file	/export/servers/.htpasswd;
		proxy_pass http://127.0.0.1:9000;
	}
}

然后再次访问zhukun.net时便会弹出验证框要求输入用户名和密码。 (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

使用命令/脚本增加/删除crontab项

使用命令/脚本增加/删除crontab项,记录一下吧.

一, 删某一项cron任务

方法1(仅适用root,不推荐)
修改/var/spool/cron/root文件

这个方法有以下问题:
1, 只有root用户可以修改,其它用户均没有权限,因为/var/spool/cron这个目录的属主及属组均是root,且目录权限是700, 因此其它用户没有权限进入此目录去修改自己的/var/spool/cron/username文件.

方法2
如果某个用户要删除自己的cron任务, 那么只需要执行

crontab -l

crontab -l | grep -v 'config-edit.163.com/config_dir/newsyn' | crontab -

crontab -l | grep -v 'tomcatRoot/jd_product/data/jd_product.txt' | crontab -

如果root需要删除某个用户的cron任务, 那么

crontab -u USERNAME -l

crontab -u USERNAME -l | grep -v 'config-edit.163.com/config_dir/newsyn'  | crontab -u USERNAME -

crontab -u USERNAME -l | grep -v 'tomcatRoot/jd_product/data/jd_product.txt'  | crontab -u USERNAME -

提示: -u参数仅有root可以调用.

也可以这么做

crontab -l | grep -v 'config-edit.163.com/config_dir/newsyn' > cron.base
crontab cron.base

(more…)

Read More