[工具] 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 -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} eternalcenter.com

(补充:这里以访问并测试 eternalcenter.com ,并显示测试过程为例)

案例二:

# 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://api.weixin.qq.com/sns/user

(补充:这里以访问并测试 eternalcenter.com ,并不显示测试过程为例)

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

介绍:

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

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

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)