分类目录归档:运维

作为运维人,必懂的知识,全在这里啦

python複製多層子文件到目標目錄(API)

#!/usr/local/env python3
# -*- coding:utf-8 -*-

import psutil, shutil, os

# 杀死进程
def kill_process_name(process_name):
    pid_list = psutil.pids()
    for pid in pid_list:
        try:
            each_pro = psutil.Process(pid)
            if process_name.lower() in each_pro.name().lower():
                # logger.info('find and kill {}'.format(process_name))
                each_pro.terminate()
                each_pro.wait(timeout=3)

        except psutil.NoSuchProcess as pid:
            pass

# copy文件
def copy_file(s_dir, d_dir):
    if os.path.exists(d_dir):
        shutil.rmtree(d_dir) # 删除zzinfo工作目录
    os.makedirs(d_dir)
    for root, dirs, files in os.walk(s_dir):
        for d in dirs:
            s_dir_name = os.path.join(root, d) # 列出workspace里面的子目录
            d_dir_name = s_dir_name.replace(s_dir, d_dir)
            if not os.path.exists(d_dir_name):
                os.makedirs(d_dir_name) # 创建zzinfo里面的子目录

        for f in files:
            s_file_name = os.path.join(root, f) # 列出workspace里面的所有文件的绝对路径
            d_file_name = s_file_name.replace(s_dir, d_dir) # 列出目标文件的绝对路径
            shutil.copyfile(s_file_name, d_file_name)


# main函数入口
if __name__ == '__main__':

    os.environ['BUILD_ID'] = "dontKillMe"

    kill_process_name("gkfmc64.exe")

    # copy lib部分
    lib_s_dir = os.getenv("WORKSPACE") + r'\lib'
    lib_d_dir = r"d:\zzinfo\gkfmc64\lib"
    copy_file(lib_s_dir, lib_d_dir)

    # copy bin部分
    bin_s_dir = os.getenv("WORKSPACE") + r'\target\classes'
    bin_d_dir = r"d:\zzinfo\gkfmc64\bin"
    copy_file(bin_s_dir, bin_d_dir)

    # 启动gkfmc64.exe
    os.popen(r"start /d d:\zzinfo\PTJYgkfmc64  gkfmc64.exe")

CentOS部署samba服務

服務端

# yum 安裝
[root@localhost ~]# yum install samba samba-client samba-swat -y

# 配置文件
[root@localhost ~]# cat /etc/samba/smb.conf

###########################################
[global]
        workgroup = MYGROUP
        server string = Samba Server Version %v
        hosts allow = 10.0.8.0/24 
        log file = /var/log/samba/log.%m
        max log size = 50
        security = share
        passdb backend = tdbsam
        load printers = yes
        cups options = raw

[share]
comment = share
path = /project/tools
browseable = yes
guest ok = yes
writable = yes


[homes]
        comment = Home Directories
        browseable = no
        writable = yes

[printers]
        comment = All Printers
        path = /var/spool/samba
        browseable = no
        guest ok = no
        writable = no
        printable = yes
###########################################

訪問

[root@localhost ~]# yum install cifs-utils -y

# 挂載
[root@localhost ~]# mkdir -p /samba_share
[root@localhost ~]# mount -t cifs //xxx.xxx.xxx.xx/share /samba_share

非root用戶沒有權限運行docker命令

問題描述

[yuy@localhost ~]$ docker ps
Got permission denied while trying to connect to the Docker daemon socket at unix:///var/run/docker.sock: Get http://%2Fvar%2Frun%2Fdocker.sock/v1.26/containers/json: dial unix /var/run/docker.sock: connect: permission denied

問題分析

Manage Docker as a non-root user

The docker daemon binds to a Unix socket instead of a TCP port. By
default that Unix socket is owned by the user root and other users can
only access it using sudo. The docker daemon always runs as the root
user.

If you don’t want to use sudo when you use the docker command, create
a Unix group called docker and add users to it. When the docker daemon
starts, it makes the ownership of the Unix socket read/writable by the
docker group.

解決辦法

# 添加docker用户组
[yuy@localhost ~]$ sudo groupadd docker

# 将登陆用户加入到docker用户组中
[yuy@localhost ~]$ sudo gpasswd -a $USER docker

# 更新用户组
[yuy@localhost ~]$ sudo newgrp docker

# 重啓 docker 服務
[yuy@localhost ~]$ sudo systemctl restart docker.service

# 授權 docker.sock 
[yuy@localhost ~]$ sudo chmod a+rw /var/run/docker.sock

批量新增用戶至docker用戶組脚本

#!/bin/bash
#author by Michael Ho

# 用戶列表
user_array=(
    zhouj
    zhangmy
    caimz
    yuy
)

# 新增用戶到docker群組中
add_user() {

    if [[ $EUID -ne 0 ]]; then
        echo -ne "\033[31mThis scripts must be run as root ..\033[0m"
        exit 1
    fi

    groupadd docker

    for i in ${user_array[*]}; do
        gpasswd -a $i docker
        echo -ne "\033[32m 已將 $i 加入到 docker 用戶組! \033[0m"
    done

    newgrp docker
}

# main函式
main() {
    add_user

    # 重啓 docker 服務
    systemctl restart docker.service

    # 授權 docker.sock 
    chmod a+rw /var/run/docker.sock
}

# 程式入口
main

windows安裝pip報錯問題

安裝pip時報錯現象如下

D:\>python get-pip.py
C:\Python38\lib\site-packages\setuptools\distutils_patch.py:25: UserWarning: Distutils was imported before Setuptools. This usage is discouraged and may exhibit undesirable behaviors or errors. Please use Setuptools' objects directly or at least import Setuptools first.
  warnings.warn(
Looking in indexes: http://pypi.douban.com/simple
WARNING: The repository located at pypi.douban.com is not a trusted or secure host and is being ignored. If this repository is available via HTTPS we recommend you use HTTPS instead, otherwise you may silence this warning and allow it anyway with '--trusted-host pypi.douban.com'.
ERROR: Could not find a version that satisfies the requirement pip
ERROR: No matching distribution found for pip
WARNING: The repository located at pypi.douban.com is not a trusted or secure host and is being ignored. If this repository is available via HTTPS we recommend you use HTTPS instead, otherwise you may silence this warning and allow it anyway with '--trusted-host pypi.douban.com'.

D:\>curl https://bootstrap.pypa.io/get-pip.py | python3。
'python3。' 不是内部或外部命令,也不是可运行的程序
或批处理文件。

D:\>
D:\>curl https://bootstrap.pypa.io/get-pip.py | python
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100 1882k  100 1882k    0     0   171k      0  0:00:11  0:00:11 --:--:--  152k
C:\Python38\lib\site-packages\setuptools\distutils_patch.py:25: UserWarning: Distutils was imported before Setuptools. This usage is discouraged and may exhibit undesirable behaviors or errors. Please use Setuptools' objects directly or at least import Setuptools first.
  warnings.warn(
Looking in indexes: http://pypi.douban.com/simple
WARNING: The repository located at pypi.douban.com is not a trusted or secure host and is being ignored. If this repository is available via HTTPS we recommend you use HTTPS instead, otherwise you may silence this warning and allow it anyway with '--trusted-host pypi.douban.com'.
ERROR: Could not find a version that satisfies the requirement pip
ERROR: No matching distribution found for pip
WARNING: The repository located at pypi.douban.com is not a trusted or secure host and is being ignored. If this repository is available via HTTPS we recommend you use HTTPS instead, otherwise you may silence this warning and allow it anyway with '--trusted-host pypi.douban.com'.

解決辦法

  • 在系统目录C:\Users\用户\AppData\Roaming新建pip文件夹
  • 在pip文件夹下新建pip.ini文件,并写入如下配置:
[global]
index-url = http://mirrors.aliyun.com/pypi/simple/
[install]
trusted-host = mirrors.aliyun.com

注意

  • 安裝Windows版python.exe時,一定要以管理員模式打開並安裝,否則pip要人肉安裝,比如我
  • curl get-pip.py 時,可以在任意盤符目錄,但當前用戶一定能可以訪問到該目錄
  • 這個報錯是 pip 版本 >=7 時,使用鏡像源時,會提示源地址不受信任或不安全,需要ssl安全認證

CentOS7根目录磁盘扩容(/dev/mapper/centos-root 空间不足)

0.查看根分区大小

[root@master ~]# df -h
Filesystem                      Size  Used Avail Use% Mounted on
devtmpfs                         63G     0   63G   0% /dev
tmpfs                            63G     0   63G   0% /dev/shm
tmpfs                            63G  1.3G   62G   2% /run
tmpfs                            63G     0   63G   0% /sys/fs/cgroup
/dev/mapper/cl_geekthings-root   50G   38G   13G  76% /
/dev/sda1                      1014M  311M  704M  31% /boot
/dev/mapper/cl_geekthings-home  918G   12G  906G   2% /home
tmpfs                            13G   12K   13G   1% /run/user/42
tmpfs                            13G     0   13G   0% /run/user/0
overlay                          50G   38G   13G  76% /var/lib/docker/overlay2/70dffe831895bb55d79b4a8f483364a4f736637ccb2f6694f1cf82ba7ff1743d/merged

1.查看磁盘编号

[root@master ~]# ls /dev/sd*
/dev/sda  /dev/sda1  /dev/sda2

2.这里在vMware vCenter中新增一块硬盘(编辑->新增设备) 再次查看磁盘

[root@master ~]# ls /dev/sd*
/dev/sda  /dev/sda1  /dev/sda2  /dev/sdb

3.对新增硬盘进行分区, 这里新增的磁盘名称叫 /dev/sdb

[root@localhost ~]# fdisk /dev/sdb

# 输入 m, 得到帮助,可以按照提示操作进行分区了
# 因为这块硬盘只有 100G,所以我只打算分一个区,这里是需要输入 n,然后接下来都按 Enter 键,默认即可,最后输入 w 保存退出
[root@localhost ~]# fdisk /dev/sdb
Welcome to fdisk (util-linux 2.23.2).

Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.

Command (m for help): m
Command action
   a   toggle a bootable flag
   b   edit bsd disklabel
   c   toggle the dos compatibility flag
   d   delete a partition
   g   create a new empty GPT partition table
   G   create an IRIX (SGI) partition table
   l   list known partition types
   m   print this menu
   n   add a new partition
   o   create a new empty DOS partition table
   p   print the partition table
   q   quit without saving changes
   s   create a new empty Sun disklabel
   t   change a partition's system id
   u   change display/entry units
   v   verify the partition table
   w   write table to disk and exit
   x   extra functionality (experts only)

4.将分区后的磁盘格式化成xfs类型

[root@localhost ~]# mkfs.xfs /dev/sdb1

5.创建pv

[root@master ~]# pvcreate /dev/sdb1
  Physical volume "/dev/sdb1" successfully created.

6.查看vg组

[root@master ~]# vgs
  VG            #PV #LV #SN Attr   VSize    VFree
  cl_geekthings   1   3   0 wz--n- <999.00g 4.00m

7.扩展vg

[root@master ~]# vgextend cl_geekthings /dev/sdb1
  Volume group "cl_geekthings" successfully extended

[root@master ~]# vgs
  VG            #PV #LV #SN Attr   VSize VFree  
  cl_geekthings   2   3   0 wz--n- 1.17t 200.00g

8.扩展lv

[root@master ~]# lvextend -L +200G /dev/mapper/cl_geekthings-root
  Size of logical volume cl_geekthings/root changed from 50.00 GiB (12800 extents) to 250.00 GiB (64000 extents).
  Logical volume cl_geekthings/root successfully resized.

9.系统重新读取大小

[root@master ~]# xfs_growfs /dev/mapper/cl_geekthings-root
meta-data=/dev/mapper/cl_geekthings-root isize=512    agcount=4, agsize=3276800 blks
         =                       sectsz=512   attr=2, projid32bit=1
         =                       crc=1        finobt=0 spinodes=0
data     =                       bsize=4096   blocks=13107200, imaxpct=25
         =                       sunit=0      swidth=0 blks
naming   =version 2              bsize=4096   ascii-ci=0 ftype=1
log      =internal               bsize=4096   blocks=6400, version=2
         =                       sectsz=512   sunit=0 blks, lazy-count=1
realtime =none                   extsz=4096   blocks=0, rtextents=0
data blocks changed from 13107200 to 65536000

10.查看扩容是否成功

[root@master ~]# df -h
Filesystem                      Size  Used Avail Use% Mounted on
devtmpfs                         63G     0   63G   0% /dev
tmpfs                            63G     0   63G   0% /dev/shm
tmpfs                            63G  1.3G   62G   2% /run
tmpfs                            63G     0   63G   0% /sys/fs/cgroup
/dev/mapper/cl_geekthings-root  250G   38G  213G  16% /
/dev/sda1                      1014M  311M  704M  31% /boot
/dev/mapper/cl_geekthings-home  918G   12G  906G   2% /home
tmpfs                            13G   12K   13G   1% /run/user/42
tmpfs                            13G     0   13G   0% /run/user/0
overlay                         250G   38G  213G  16% /var/lib/docker/overlay2/70dffe831895bb55d79b4a8f483364a4f736637ccb2f6694f1cf82ba7ff1743d/merged
[root@master ~]# lsblk
NAME                   MAJ:MIN RM   SIZE RO TYPE MOUNTPOINT
sda                      8:0    0  1000G  0 disk 
├─sda1                   8:1    0     1G  0 part /boot
└─sda2                   8:2    0   999G  0 part 
  ├─cl_geekthings-root 253:0    0   250G  0 lvm  /
  ├─cl_geekthings-swap 253:1    0  31.5G  0 lvm  
  └─cl_geekthings-home 253:2    0 917.5G  0 lvm  /home
sdb                      8:16   0   200G  0 disk 
└─cl_geekthings-root   253:0    0   250G  0 lvm  /
sr0                     11:0    1   4.1G  0 rom

CentOS記錄所有用戶登陸操作詳細日志

setp1 : 在 /etc/profile 末尾追增以下代碼

############################ 用戶登陸操作歷史脚本 ############################

USER_IP=`who -u am i 2>/dev/null| awk '{print $NF}'|sed -e 's/[()]//g'`
HISTDIR=/tmp/log/history
DT=`date +%Y-%m-%d`

if [ -z $USER_IP ]; then
    USER_IP=`hostname`
fi

pdf="."
if [[ ! $USER_IP == *${pdf}* ]]; then
    USER_IP=`hostname`
fi

if [ ! -d $HISTDIR ]; then
    mkdir -p $HISTDIR
    chmod 300 $HISTDIR
fi

if [ ! -d $HISTDIR/${DT} ]; then
    mkdir -p $HISTDIR/${DT}
    chmod 300 $HISTDIR/${DT}
fi

export HISTFILESIZE=10000 # 定義文件中最多的只有HISTFILESIZE行
export HISTSIZE=10000 # 定義history命令輸出的記錄數
DT2=`date +%Y-%m-%d_%H:%M:%S`
export HISTFILE="$HISTDIR/${DT}/${LOGNAME}@${USER_IP}_$DT2"
export HISTTIMEFORMAT="%Y-%m-%-d_%H:%M:%S # "
chmod 600 $HISTDIR/${DT}/*_* 2>/dev/null

##############################################################################

step2: 刷新生效

[root@localhost ~]# source /etc/profile

CentOS两种方式安装7z命令

一、安装(以CentOS为例)

1.yum安装(能访问互联网的,用此方式)

建议更换国内源 :中国科学技术大学镜像源

  • 安装epel源
    [root@linux-host1 opt]# sudo yum install epel-release -y
  • yum安装
    [root@linux-host1 opt]# sudo yum install p7zip -y
2.二进制安装

7z 最新版本链接
7z 官网

  • 下载到/usr/local
    [root@linux-host1 ~]# cd /usr/local && wget https://nchc.dl.sourceforge.net/project/p7zip/p7zip/16.02/p7zip_16.02_src_all.tar.bz2
  • 解压
    [root@linux-host1 local]# sudo tar xjvf p7zip_16.02_src_all.tar.bz2
  • 安装
    [root@linux-host1 local]# cd p7zip_16.02 && sh install.sh

二、命令

[root@linux-host1 opt]# 7za x frontend_7_06_JsTest_0303.7z  -r -o./
# 参数含义:
# x  代表解压缩文件,并且是按原始目录树解压(还有个参数 e 也是解压缩文件,但其会将所有文件都解压到根下,而不是自己原有的文件夹下)
# frontend_7_06_JsTest_0303.7z是压缩文件,这里我用phpadmin做测试。这里默认使用当前目录下的phpMyAdmin-3.3.8.1-all-languages.7z
# -r 表示递归解压缩所有的子文件夹
# -o 是指定解压到的目录,-o后是没有空格的,直接接目录。

[root@linux-host1 opt]# 7za a -t7z -r frontend_7_06_JsTest_0303.7z /opt/frontend_7_06_JsTest_0303/*
# 参数含义:
# a  代表添加文件/文件夹到压缩包
# -t 是指定压缩类型,这里定为7z,可不指定,因为7za默认压缩类型就是7z
# -r 表示递归所有的子文件夹
# frontend_7_06_JsTest_0303.7z 是压缩好后的压缩包名
# /opt/frontend_7_06_JsTest_0303/*   :是压缩目标。
# 7za不仅仅支持.7z压缩格式,还支持.tar.bz2等压缩类型的。如上所述,用-t指定即可。

CentOS7下使用Oracle 11g sqlplus信息显示乱码

CentOS7下使用Oracle 11g sqlplus信息显示乱码

现象
[oracle@toa ~]$ sqlplus /nologin

SQL*Plus: Release 11.2.0.1.0 Production on ������ 1�� 20 23:43:44 2021

Copyright (c) 1982, 2009, Oracle.  All rights reserved.

SQL*Plus: Release 11.2.0.1.0 Production

Copyright (c) 1982, 2009, Oracle.  All rights reserved.

ʹ�� SQL*Plus ִ�� SQL, PL/SQL �� SQL*Plus ���䡣

�÷� 1: sqlplus -H | -V

    -H             ��ʾ SQL*Plus �汾��
                   �÷�������
    -V             ��ʾ SQL*Plus �汾��

�÷� 2: sqlplus [ [<option>] [{logon | /nolog}] [<start>] ]

  <option> Ϊ: [-C <version>] [-L] [-M "<options>"] [-R <level>] [-S]

    -C <version>   ����Ӱ���������ļ���������Ϊ
                   <version> ָ���İ汾���ð汾����
                   "x.y[.z]" ��ʽ������, -C 10.2.0
    -L             ֻ���Ե�¼һ��, ������
                   �ڳ���ʱ�ٴ���ʾ��
    -M "<options>" �����������Զ� HTML ���ǡ�ѡ��
                   �ĸ�ʽΪ:
                   HTML [ON|OFF] [HEAD text] [BODY text] [TABLE text]
                   [ENTMAP {ON|OFF}] [SPOOL {ON|OFF}] [PRE[FORMAT] {ON|OFF}]
    -R <level>     ��������ģʽ, �Խ������ļ�ϵͳ������
                    SQL*Plus �����������
                   �� 1, 2 �� 3���������Ƽ���Ϊ -R 3, �ü���
                   �������ļ�ϵͳ������
                   �����û����
    -S             ��������ʾģʽ, ��ģʽ����
                   ������ SQL*Plus ����, ��ʾ�ͻ���
                   ����ʾ��
解决
# 查看 Oracle 环境变量
[oracle@toa ~]$ echo $NLS_LANG
SIMPLIFIED CHINESE_CHINA.ZHS16GBK

# 修改环境变量为 SIMPLIFIED CHINESE_CHINA.AL32UTF8 [这种修改方式,只在当前 bash 中生效]
[oracle@toa ~]$ export NLS_LANG="SIMPLIFIED CHINESE_CHINA.AL32UTF8"

# 检查是否还乱码 (如下图所示)
[oracle@toa ~]$ sqlplus /nolog

SQL*Plus: Release 11.2.0.1.0 Production on 星期三 1月 20 23:21:51 2021

Copyright (c) 1982, 2009, Oracle.  All rights reserved.

SQL> exit
[oracle@toa ~]$ sqlplus / as sysdba

SQL*Plus: Release 11.2.0.1.0 Production on 星期三 1月 20 23:22:05 2021

Copyright (c) 1982, 2009, Oracle.  All rights reserved.

连接到:
Oracle Database 11g Enterprise Edition Release 11.2.0.1.0 - 64bit Production
With the Partitioning, OLAP, Data Mining and Real Application Testing options
扩展阅读
  • NLS_LANG 是一个环境变量,用于定义语言,地域以及字符集属性。对于非英语的字符集,NLS_LANG 的设置就非常重要。

  • NLS(National Language Support) 当我们设定一种 NLS 的时候实际上我们是为 Oracle 在存放数据时指定了他的语种所特有的一些表达形式,比如我们选择 Chinese, 那么它的中文字符如何存放,按什么规则排序,货币如何表示,日期格式也就被设定了。

  • NLS_LANG 参数由以下部分组成

    NLS_LANG=<Language>_<Territory>.<Clients Characterset>
    • Language

      • Oracle(错误)信息的语言
      • 日和月份的名称
        注意:NLS_LANGUAGE与插入和查询的数据的语言无关。
    • Territory

      • 货币和数字格式
      • 计算星期和天数的范围和惯例
    • Clients Characterset(客户端字符集)

      • 定义Oracle客户端,客户应用使用的编码
      • 或者它要符合您 Microsoft Windows 代码页( GUI 工具的 ACP, 命令提示符的 CHCP 值)
      • 或者为 Unicode WIN32 应用设置为 UTF8/AL32UTF8