[工具] Python 一个脚本调用另一个脚本的函数执行 Linux 命令并返回执行结果

介绍:

使用方法:
1. 将第一个脚本命名为 test_main.py,将第二个脚本命名为 test_sub.py,并将它们放在同一个2. 目录下
3. 给此脚本添加执行权限
4. 用 python3 命令执行第一个脚本

注意:执行此脚本之前要先安装 ansible 命令,并可以让 ansible 命令控制名为 test 的服务器

第一个脚本:

from cpu_sub import *

sename='test'

out1=cpu(sename)
out1=out1.decode().strip()

print(out1)

out2=mem(sename)
out2=out2.decode().strip()

print(out2)

out3=time(sename)
out3=out3.decode().strip()

print(out3)

第二个脚本:

import subprocess

def cpu(sname):
    out = subprocess.check_output('ansible -m shell -a \'cat /proc/cpuinfo | egrep "core id|physical id" | tr -d "\n" | sed s/physical/\\nphysical/g | grep -v ^$ | sort | uniq | wc -l\' %s | tail -1'%sname, shell=True)
    return(out)

def mem(sname):
    out = subprocess.check_output('ansible -m shell -a \'free -m\' %s | grep -i mem'%sname, shell=True)
    return(out)

def time(sname):
    out = subprocess.check_output('ansible -m shell -a \'uptime\' %s | tail -1'%sname, shell=True)
    return(out)

[命令] Linux 命令 curl (测试网页访问)

内容一:测试网页可以被访问的案例

# curl -v -k https://eternalcenter-may-1-2022.github.io

(补充:这里以直接访问 https://eternalcenter-may-1-2022.github.io 网页为例)

内容二:测试网页访问速度的案例
2.1 案例一:显示测试过程

# curl -o /dev/null -s -w %{http_code}:%{http_connect}:%{content_type}:%{time_namelookup}:%{time_redirect}:%{time_pretransfer}:%{time_connect}:%{time_starttransfer}:%{time_total}:%{speed_download} https://eternalcenter-may-1-2022.github.io

(补充:这里以访问并测试 https://eternalcenter-may-1-2022.github.io,并显示测试过程为例)

2.2 案例二:不显示测试过程

# curl -o /dev/null -w %{http_code}:%{http_connect}:%{content_type}:%{time_namelookup}:%{time_redirect}:%{time_pretransfer}:%{time_connect}:%{time_starttransfer}:%{time_total}:%{speed_download} https://
eternalcenter.com

(补充:这里以访问并测试 https://eternalcenter-may-1-2022.github.io,并不显示测试过程为例)

内容三:测试使用代理访问网页的案例
3.1 案例一:使用代理

# curl -v --proxy proxy.example.com:8080 https://eternalcenter-may-1-2022.github.io

(补充:这里以通过 proxy.example.com:8080 代理访问 https://eternalcenter-may-1-2022.github.io 网页为例)

3.2 案例二:使用代理并使用用户进行验证

# curl -v --<proxy user> user:<password> --proxy proxy.example.com:8080 https://eternalcenter-may-1-2022.github.io

(补充:这里以通过 proxy.example.com:8080 代理并使用 proxy user 和 password 进行验证访问 https://eternalcenter-may-1-2022.github.io 网页为例)

内容四: 测试使用证书访问网页的案例

# curl -v -k https://eternalcenter-may-1-2022.github.io --cacert /eternalcenter.com.pem

(补充:这里以通过 eternalcenter.com.pem 证书访问 https://eternalcenter-may-1-2022.github.io 网页为例)

内容五:其他测试访问网页的案例
5.1 案例一:将网页下载后另存为别的文件

# curl -v -k https://eternalcenter-may-1-2022.github.io -o eternalcentre.com

(补充:这里以下载 https://eternalcenter-may-1-2022.github.io 并另存为 eternalcentre.com 为例)

5.2 案例二:不使用安全策略下载的案例

# curl -v -k https://eternalcenter-may-1-2022.github.io --insecure

(补充:这里以不使用安全策略下载 https://eternalcenter-may-1-2022.github.io)

5.3 案例三:静默下载的案例

# curl -v -k https://eternalcenter-may-1-2022.github.io --silent

(补充:这里以静默下载 https://eternalcenter-may-1-2022.github.io 为例)

[工具] Shell 显示所有可升级的软件版本,并自动生成相应的升级命令 (openSUSE & SLE)

介绍:

作者:朱明宇
名称:openSUSE & SLE 显示所有可升级的软件版本,并自动生成相应的升级命令
作用:openSUSE & SLE 显示所有可升级的软件版本,并自动生成相应的升级命令

使用方法:
给此脚本添加执行权限
执行此脚本
执行此脚本大致会生成以下内容:

zypper update  MozillaFirefox-78.12.0-lp152.2.61.1 MozillaFirefox-translations-common-78.12.0-lp152.2.61.1 alsa-oss-1.1.8-lp152.4.3.1

脚本:

#!/bin/bash

m=''

for n in `zypper list-updates | tail -n +5 | awk '{print $7"-"$11}'`
do
        m="$m $n"
done

echo "zypper update $m"

[工具] Python 一个脚本调用另一个脚本的函数

介绍:

使用方法:
1. 将第一个脚本命名为 test_main.py,将第二个脚本命名为 test_sub.py,并将它们放在同一个目录下
2. 给此脚本添加执行权限
3. 用 python3 命令执行第一个脚本

第一个脚本:

from test_sub import * 

a=2
b=3

out=nsum(a,b)

print(out)

第二个脚本:

def nsum(x,y):
    out = x + y
    return(out)