[工具] Shell 批量实现多个远程服务器执行命令 (单条命令版)

介绍:

基本信息

作者:朱明宇
名称:批量实现多个远程服务器执行命令 (单条命令版)
作用:批量实现多个远程服务器执行命令 (单条命令版)

使用方法

1. 将此脚本和清单 $list 文件放在同一目录下
2. 清单 $list 里每一个远程服务器名或 IP 地址占用 1 行
3. 在此脚本的分割线内写入相应的内容
4. 给此脚本添加执行权限
5. 执行此脚本

脚本分割线里的变量

1. execute=”top -bn 1 | head -1″ #指定要执行的命令
2. list=servers.txt #指定服务器清单

注意

执行脚本的用户要在远程服务器中有同名用户,此用户拥有免密钥 sudo su 权限,且能被本服务器免密钥 ssh

脚本:

#!/bin/bash

####################### Separator ########################

execute="top -bn 1 | head -1"
list=servers.txt

####################### Separator ########################

echo "WARNING: before execute this, please execute \"screen -S update\" command first"

read -p "will execute $execute on servers in $list, if you agree please input y : " b
echo "you input $b"

if [ "$b" != "y" ];then
        echo "you don't agree so exit now"
        exit 0
fi

num=0

for i in `awk '{print $1}' $list`
do
        let num++
        echo "$num $i"
        ssh -t $i "sudo -u root su - root -c \"$execute\""
        echo "$i has been done"
        echo
done

[实验] LNMP 平台的搭建 (openSUSE Leap 15 版)

步骤一:LNMP 简介

LNMP 是一个实现网站服务的方法,它由 4 样东西组成:
1) Linux 系统
2) Nginx 网页服务
3) MariaDB 数据库
4) PHP 网页程序

步骤二:系统环境要求

1) 服务器的系统需要是 openSUSE 15 版本
2) 服务器要关闭防火墙
3) 服务器系统要配置好可用的软件源(最好是软件数量最多的官方版本)

步骤三:搭建 LNMP

3.1 Nginx 网页服务

3.1.1 安装 Nginx 网页服务
# zypper -n install nginx
3.1.2 配置 Nginx 网页服务的配置文件
3.1.2.1 删除原有的 Nginx 服务的配置文件
# rm /etc/nginx/nginx.conf
3.1.2.2 创建新的 Nginx 网页服务的配置文件
# cp /etc/nginx/nginx.conf.default /etc/nginx.conf
3.1.2.3 配置 Nginx 网页服务的配置文件
# vi /etc/nginx/nginx.conf

将其中的:

......
        location / {
            root   html;
            index  index.html index.htm;
        }
......
        #location ~ \.php$ {
        #    root           html;
        #    fastcgi_pass   127.0.0.1:9000;
        #    fastcgi_index  index.php;
        #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
        #    include        fastcgi_params;
        #}
......

修改为:

......
        location / {
            root   html;
            index  index.php index.html index.htm;
        }
......
        location ~ \.php$ {
            root           html;
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_index  index.php;
        #   fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
            include        fastcgi.conf;
        }
......

(补充:这里以让 Nginx 将对于 PHP 的请求传递到本机的 9000 端口为例)

3.1.3 启动 Nginx 网页服务
# systemctl start nginx

3.2 MariaDB 数据库

3.2.1 安装 MariaDB 数据库
# zypper -n install mariadb mariadb-server
3.2.2 启动 MariaDB 数据库
# systemctl start mariadb

3.3 PHP 环境和连接服务

3.3.1 安装 PHP 环境和连接服务
# zypper -n install php7 php7-fpm php7-mysql php7-gd php7-mbstring php7-opcache php7-json php7-xmlrpc php7-zlib
3.3.2 创建提供 PHP 连接服务的用户
# useradd php-fpm -s /sbin/nologin
3.3.3 配置 PHP 连接服务的配置文件
# vi /etc/php-fpm.conf

将以下内容:

......
user = nouser
group = nouser
......

修改为:

......
user = php-fpm
group = users
listen = 127.0.0.1:9000
......


补充:这里以
1) 以 php-fpm 用户和 users 用户组的身份启动 php-fpm
2) 让 php-fpm 监听本地 9000 端口为例

步骤四:后续工作

1) 给 MariaDB 数据库设置用于存储网页数据的用户和密码
2) 将 PHP 网页程序放到 Nginx 的网页目录下(/srv/www/htdocs)
3) 给 PHP 网页程序设置用于连接 MariaDB 数据库的用户和密码

步骤五:测试 LNMP 平台

使用浏览器访问服务器 IP 地址就可以看到对应 PHP 网页了

[步骤] Django 数据表的重建 (不影响原有的数据)

注意:

在重建 Django 数据表之前要先安装 Django 服务

正文:

(django_env) [root@python mysite]# python manage.py migrate
Operations to perform:
  Apply all migrations: admin, auth, contenttypes, sessions
Running migrations:
  Applying contenttypes.0001_initial... OK
  Applying auth.0001_initial... OK
  Applying admin.0001_initial... OK
  Applying admin.0002_logentry_remove_auto_add... OK
  Applying contenttypes.0002_remove_content_type_name... OK
  Applying auth.0002_alter_permission_name_max_length... OK
  Applying auth.0003_alter_user_email_max_length... OK
  Applying auth.0004_alter_user_username_opts... OK
  Applying auth.0005_alter_user_last_login_null... OK
  Applying auth.0006_require_contenttypes_0002... OK
  Applying auth.0007_alter_validators_add_error_messages... OK
  Applying auth.0008_alter_user_username_max_length... OK
  Applying sessions.0001_initial... OK

[工具] Python 随意生成一个 json 文件

介绍

使用方法

1. 给此脚本添加执行权限
2. 执行此脚本

注意

此脚本不能命名为 json.py

脚本

#!/usr/bin/python3

import json
hostlist = {}
hostlist["web"] = ["192.168.100.101", "192.168.100.102"]

hostlist["db"] = {
        "hosts" :["192.168.100.121", "192.168.100.122"],
        "vars" :{"ansible_ssh_user":"root", "ansible_ssh_pass":"1"}
        }

hostlist["192.168.100.110"] = {
        "ansible_ssh_user":"root", "ansible_ssh_pass":"pwd"
        }

print(json.dumps(hostlist))

[娱乐] HTML 朱明宇的个人网页

English

代码

<html>

<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

<head>

<title>朱明宇</title>

</head>

<body>

<div style="position:absolute; width:100%; height:100%; z-index:-1; left:0; top:0;"> 
<img src="https://filedn.eu/ltLpz9YGUS2hi1pJmklNfDb/zh_cn/dimfigure.JPG" height="100%" width="100%" style="position:absolute;left:0; top:0;"> 
</div>

<div style="width:300px;height:50px;float:left"> 
<form action="http://www.google.com/search" method="get"> 
<input type="text" name="q" size="20" maxlength="255" value="" /> 
<input type="submit" name="btnG" value="Google" /> 
</form>
<a href="https://mingyu-zhu.github.io" style="right: 15px; position: absolute; font-size:25px;color:red">English (GitHub)</a>
<a href="https://mingyuzhu.com" style="right: 205px; position: absolute; font-size:25px;color:red">English</a>
</div> 

<div style="left: 8px; position: absolute; top: 75px;font-size:45px;">朱明宇的个人网页</div>

<div style="left: 800px; position: absolute; top: 300px;font-size:15px;">“愿每个人都能公平地实现自我成就和自我幸福。”</div>

<a href="https://eternalcenter.com" target="_blank" style="left: 8px; position: absolute; top: 185px;font-size:20px;color:black">永恒中心(eternalcenter.com),朱明宇的个人网站</a>
<a href="https://e.pcloud.link/publink/show?code=kZukhJZUhQq66Fxs0yg5rL1LsAgrSD3ytMk" target="_blank" style="left: 8px; position: absolute; top: 215px;font-size:20px;color:black">用于克隆朱明宇个人网站的数据</a>
<a href="https://github.com/eternalcenter-now/eternalcenter-now" target="_blank" style="left: 8px; position: absolute; top: 245px;font-size:20px;color:black">用于克隆朱明宇个人网站的代码</a>
<a href="http://static.eternalcenter.com" target="_blank" style="left: 8px; position: absolute; top: 275px;font-size:20px;color:black">朱明宇个人网站的效果展示网页</a>
<a href="https://eternalcenter-now.github.io" target="_blank" style="left: 8px; position: absolute; top: 305px;font-size:20px;color:black">朱明宇个人网站的效果展示网页 (GitHub)</a>
<a href="https://e.pcloud.link/publink/show?code=kZoJhJZDx6DEf6sXYJUK7YxLjWhFRPr3i70" target="_blank" style="left: 8px; position: absolute; top: 335px;font-size:20px;color:black">朱明宇的个人公开资料</a>

</body>

<html/>

背景图片