【Shell】标准的 Shell 脚本

记录一下 shell 脚本模板。 #!/usr/bin/env bash args_num=$# action="${1}" ALLOWED_ACTION_ARGS=("set" "unset" "test") function print_ok() { local msg="${1}" echo "${msg}" } function print_err() { local msg="${1}" echo "${msg}" > /dev/stderr } function contain() { local list="${1}" local ele="${2}" for i in ${list[*]}; do if [ "${ele}" == "${i}" ]; then return 0 fi done return 1 } function check_args_num() { if [ ${args_num} != 1 ]; then print_err "Wrong args num" return 1 fi } function check_action_arg() { if ! contain "${ALLOWED_ACTION_ARGS[*]}" "${action}"; then print_err "Action arg must be [ set || unset || test ]" return 1 fi } function set_proxy () { export ALL_PROXY=http://www.vksir.zone print_ok "Set proxy success" } function unset_proxy() { unset ALL_PROXY print_ok "Unset proxy success" } function test_proxy() { if curl -k https://www.google.com --connect-timeout 3 >/dev/null 2>&1; then print_ok "Proxy is available" else print_err "Proxy is not available" fi } if ! check_args_num; then return 1 fi if ! check_action_arg; then return 1 fi if [ "${action}" == "set" ]; then set_proxy elif [ "${action}" == "unset" ]; then unset_proxy elif [ "${action}" == "test" ]; then test_proxy fi return $? source proxy set source proxy unset source proxy test 注意,这里如果想写 source 脚本,那就不能使用 exit,否则会使 ssh 会话退出。 ...

九月 1, 2022  |  198 字  |  总阅读

【Python】进程管理之 subprocess

非常强大的子进程管理模块,你想要的它都有。 环境:Windows 10 上一篇文章讲的 Pexpect,功能是不错,但它有的 Subprocess 都能做到,且更加完美。 一个好的子进程管理需要满足什么功能需求? 无阻塞 / 阻塞 标准输入 / 输出 信号发送 / kill 其实也不多。 开始 import subprocess proc = subprocess.Popen('ping 127.0.0.1', shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, stdin=subprocess.PIPE) print(proc.stdout.read().decode('gbk')) # 因为是 windows 系统,默认编码是 ‘gbk’ 正在 Ping 127.0.0.1 具有 32 字节的数据: 来自 127.0.0.1 的回复: 字节=32 时间<1ms TTL=128 来自 127.0.0.1 的回复: 字节=32 时间<1ms TTL=128 来自 127.0.0.1 的回复: 字节=32 时间<1ms TTL=128 来自 127.0.0.1 的回复: 字节=32 时间<1ms TTL=128 127.0.0.1 的 Ping 统计信息: 数据包: 已发送 = 4,已接收 = 4,丢失 = 0 (0% 丢失), 往返行程的估计时间(以毫秒为单位): 最短 = 0ms,最长 = 0ms,平均 = 0ms subprocess 主要有两个运行命令的方法: ...

八月 7, 2021  |  786 字  |  总阅读

【Python】进程管理之 pexpect

相对有名的可用于自动化应用程序交互的模块,可用于 ssh、ftp 等程序。 环境:Ubuntu 20.04 LTS 最近想做一个游戏服务器管理器,以 Python 为主语言,对 Linux 上的常驻程序进行管理。需要满足: 无阻塞启动程序 实时读取程序输出 实时进行程序输入 定时进行程序重启 等功能要求。 要求不是很多,也不是很复杂,但 Pexpect 完成得不是很好。 开始 child = pexpect.spawn('ssh uesr@IP') # 执行命令 child.expect('password:') # 期待程序输出 'password:' child.sendline(my_password) # 向程序输入 my_password spawn() pexpect.spawn(command, args=[], timeout=30, maxread=2000, searchwindowsize=None, logfile=None, cwd=None, env=None, ignore_sighup=False, echo=True, preexec_fn=None, encoding=None, codec_errors='strict', dimensions=None, use_poll=False) command # 推荐直接使用命令字符串 child = pexpect.spawn('ls -latr /tmp') # pexpect 不解释 shell 元字符,如重定向、管道或通配符(>,|,*),因此需要如下使用 child = pexpect.spawn('/bin/bash -c "ls -l | grep LOG > logs.txt"') timeout 默认 30s,超时则报错。 logfile wiki: 日志文件成员打开或关闭日志记录。所有输入和输出都将复制到给定的文件对象。将 logfile 设置为 None 以停止记录。这是默认设置。将日志文件设置为 sys.stdout 以将所有内容回显到标准输出。每次写入后都会刷新日志文件。 ...

七月 27, 2021  |  388 字  |  总阅读

【优质工具】Oh My Zsh 终端

一款极其好用的终端工具,简单易上手。 环境:CentOS 7 安装 # 使用 zsh chsh -s /bin/zsh # 重启生效 reboot # 安装 Oh My Zsh sh -c "$(wget -O- https://raw.githubusercontent.com/ohmyzsh/ohmyzsh/master/tools/install.sh)" 有什么好处呢?用了就知道了! 主题 第一个好处就是漂亮! 有多少种主题? ls ~/.oh-my-zsh/themes 太多了,我选 ys。 vim ~/.zshrc # 修改 ZSH_THEME="ys" # 重载配置 source ~/.zshrc pure 其实也不错,但是它没有被集成到 Oh My Zsh 中,需要额外安装。 # 下载 mkdir -p "$HOME/.zsh" git clone https://github.com/sindresorhus/pure.git "$HOME/.zsh/pure" # 配置 vim ~/.zshrc # 修改 #ZSH_THEME="ys" # Em 没错~ 注意把原主题注释掉 fpath+=$HOME/.zsh/pure autoload -U promptinit; promptinit prompt pure #重载配置 source ~/.zshrc 插件 必装插件:highlighting && autosuggestions。直接 CV 就完事! ...

三月 6, 2020  |  188 字  |  总阅读