diff --git a/HISTORY.md b/HISTORY.md new file mode 100644 index 0000000..06b98d6 --- /dev/null +++ b/HISTORY.md @@ -0,0 +1,11 @@ +# 历史版本 + +## 1.5.0 + +2019-06-21 + +- 增加 kindle 推送功能 +- 增加获取网络请求的耗时和状态码功能 +- 增加查看当前用户大文件的功能 + + diff --git a/README.md b/README.md index 9106686..64fe21e 100644 --- a/README.md +++ b/README.md @@ -3,12 +3,12 @@ Wshell 是集成化的 Linux 服务器脚本,主旨是尽量将常用软件的复杂安装过程和操作一键化,提高工作效率。 **适配** + - MacOS - Ubuntu - Debian - CentOS - ## 安装 ### MacOS @@ -43,37 +43,53 @@ $ curl -L https://raw.githubusercontent.com/wxnacy/wshell/master/wshell-installe **配置环境** - ```bash $ echo 'export WS_HOME=${HOME}/.wshell' >> ~/.bashrc $ echo 'export PATH=${WS_HOME}/bin:$PATH' >> ~/.bashrc -$ echo '. ${WS_HOME}/conf/system/bashrc' >> ~/.bashrc +$ echo '. ${WS_HOME}/conf/bash/bashrc' >> ~/.bashrc $ source ~/.bashrc ``` **查看版本** + +**更新到最新版本** + +```bash +$ ws update +``` + +## 语法 + +```bash +$ ws[hell] [args...] +``` + +## 命令 + +### 查看版本 + ```bash $ ws version ``` -**更新** +### 更新版本 + +更新 Wshell 到最新版 ```bash $ ws update ``` - -## 语法 +### 检查支持软件的安装情况 ```bash -$ ws[hell] [args...] +$ ws check ``` -## 参数 -### install +### 安装系统软件 -一键下载软件,如 +一键安装软件,如 ```bash $ wshell install java @@ -109,17 +125,9 @@ zlib1g.dev, libgdbm-dev, libssl-dev, libsqlite3-dev, libbz2-dev, libreadline-dev c++, pcre, pcre-devel, openssl, openssl-devel, epel-release, zlib, zlib-devel, readline, readline-devel, readline-static, openssl-static, sqlite-devel, bzip2-devel, bzip2-libs ``` -### update - -更新 Wshell 到最新版 -### check -查看 Wshell 支持软件的安装情况 - -### os - -查看系统版本信息 +### 查看系统信息 ```bash $ ws os @@ -127,9 +135,9 @@ Darwin 10.14.3 brew # 平台名称 版本号 使用包管理工具 ``` -### hash +### 计算签名 -计算 hash 值,可以传入字符串或者文件名 +计算签名,可以传入字符串或者文件名 ```bash $ ws @@ -145,3 +153,58 @@ cf4b95753d3382d3560b1ad4f068db01 $ cat README.md | ws md5 # 接收管道信息 cf4b95753d3382d3560b1ad4f068db01 ``` + +### 查看大文件 + +```bash +$ ws bf # 查看当前用户下大小超过 100M 的文件 +$ ws bf 1000 # 查看当前用户下大小超过 1000M 的文件 +``` + +### 网络请求 + +```bash +$ ws http code https://wxnacy.com # 查看网络请求的返回状态码 +200 + +$ ws http time https://wxnacy.com # 查看网络请求的耗时情况 + + time_namelookup: 0.520085 # DNS 域名解析的时候,就是把网站转换成 ip 地址的过程 + time_connect: 0.527951 # TCP 连接建立的时间,就是三次握手的时间 + time_appconnect: 0.559406 # SSL/SSH 等上层协议建立连接的时间,比如 connect/handshake 的时间 + time_redirect: 0.000000 # 网址重镜像的时间 + time_pretransfer: 0.559430 # 从请求开始到响应开始传输的时间 + time_starttransfer: 0.571773 # 从请求开始到第一个字节将要传输的时间 + ----------------------------- + time_total: 0.571860 # 总时间 + + 0.005082 0.011133 0.028282 0.007518 0.000074 +|-- DNS --|-- TCP --|-- SSL --|-- SVR --|-- DTS --| +|------------------ TOTAL 0.052110 ---------------| +``` + +### 时间 + +```bash +$ ws date # 获取当前日期 +2019-06-20 + +$ ws time # 获取当前时间 +21:16:19 + +$ ws datetime # 获取当前日期和时间 +2019-06-20 21:17:56 +``` + +### Kindle + +如果你本地配置了 mutt 邮件发送,然后安装如下方式配置好你的 Kindle 邮箱即可推送 + +```bash +# 配置邮箱 +$ echo "email=your_kindle@kindle.cn" > ~/.config/wshell/kindle +``` + +```bash +$ ws kindle push # 推送 +``` diff --git a/VERSION b/VERSION index b2e46d1..bc80560 100644 --- a/VERSION +++ b/VERSION @@ -1 +1 @@ -1.4.8 +1.5.0 diff --git a/bin/base/bf b/bin/base/bf new file mode 100755 index 0000000..318f7ab --- /dev/null +++ b/bin/base/bf @@ -0,0 +1,5 @@ +#!/usr/bin/env bash + +wshell bf.py $@ + + diff --git a/bin/base/bf.py b/bin/base/bf.py new file mode 100755 index 0000000..de84739 --- /dev/null +++ b/bin/base/bf.py @@ -0,0 +1,56 @@ +#!/usr/bin/env python +# -*- coding:utf-8 -*- +# Author: wxnacy(wxnacy@gmail.com) +# Description: 查找大于某个值的文件,该值默认 100M + +import os + +def format_size(size: int): + '''格式化大小''' + unit = { + 0: 'B', + 1: 'K', + 2: 'M', + 3: 'G', + 4: 'T', + } + + for i in range(6): + if 1024 ** i <= size < 1024 ** ( i + 1 ): + if i > 0: + size = size / 1024 ** i + return '{:0.1f}{}'.format( size, unit[i]) + return '{}B'.format(size) + +if __name__ == "__main__": + import sys + import timeit + b = timeit.default_timer() + args = sys.argv[1:] + comp_size = 100 # 默认 100 M + if len(args) >= 1: + comp_size = int(args[0]) + + COMP_SIZE = comp_size * 1024 * 1024 # 查找大于该值的文件 + home = os.getenv("HOME") + filelists = [] + join = os.path.join + exists = os.path.exists + getsize = os.path.getsize + for root, dirs, files in os.walk(home): + for f in files: + filepath = join(root, f) + if exists(filepath): + size = getsize(filepath) + if size >= COMP_SIZE: + filelists.append(dict(size = size, filepath = filepath)) + line = '{}\t{}'.format(format_size(size), filepath) + print(line) + + filelists.sort(key=lambda x: x['size'], reverse=True) + print('\n排序后,最大的 10 个文件') + + for f in filelists[:10]: + line = '{}\t{}'.format(format_size(f['size']), f['filepath']) + print(line) + print('\ntime used', timeit.default_timer() - b) diff --git a/bin/base/size.py b/bin/base/size.py index ee22d49..4c76853 100755 --- a/bin/base/size.py +++ b/bin/base/size.py @@ -36,24 +36,20 @@ def get_size1(path): def format_size(size: int): '''格式化大小''' - unit = { 0: 'B', 1: 'K', 2: 'M', - 3: 'T', - 4: 'P', + 3: 'G', + 4: 'T', } - for i in range(1, 6): - max_size = 1024 ** i - if size < max_size: - j = i - 1 - if j < 1: - res = size - else: - res = size / 1028 * j - return '{:0.1f}{}'.format( res, unit[j]) + for i in range(6): + if 1024 ** i <= size < 1024 ** ( i + 1 ): + if i > 0: + size = size / 1024 ** i + return '{:0.1f}{}'.format( size, unit[i]) + return '{}B'.format(size) class File(): def __init__(self, path, *args, **kwargs): diff --git a/bin/base/test_size.py b/bin/base/test_size.py index 9ea33c6..ad87f48 100644 --- a/bin/base/test_size.py +++ b/bin/base/test_size.py @@ -32,8 +32,11 @@ def test_func(self): self.assertEqual def test_format_size(self): + self.assertEqual(format_size(0), '0B') self.assertEqual(format_size(234), '234.0B') self.assertEqual(format_size(1234), '1.2K') + self.assertEqual(format_size(1111111), '1.1M') + self.assertEqual(format_size(11111111111), '10.3G') def speed(count, func, *args): b = timeit.default_timer() diff --git a/bin/http/time b/bin/http/time index f151baa..9e87dbe 100755 --- a/bin/http/time +++ b/bin/http/time @@ -1,4 +1,22 @@ #!/usr/bin/env bash curl -sIL -w "@${WS_HOME}/bin/http/time_format" -o /dev/null $1 +echo '' +ws http time.py $1 +# time=$(curl -sIL -w "%{time_namelookup} %{time_connect} %{time_appconnect} \ +# %{time_redirect} %{time_pretransfer} %{time_starttransfer} %{time_total}" -o /dev/null $1) + +# echo $time +# all=($time) +# echo ${all[@]} +# dns=${all[0]} +# conn=${all[1]} +# echo $dns +# echo $((conn - dns)) + +# out=""" +# DNS: ${dns} +# TCP: $((conn - dns)) +# """ +# echo $out diff --git a/bin/http/time.py b/bin/http/time.py new file mode 100755 index 0000000..b8df9c4 --- /dev/null +++ b/bin/http/time.py @@ -0,0 +1,48 @@ +#!/usr/bin/env python +# -*- coding:utf-8 -*- +# Author: wxnacy(wxnacy@gmail.com) +# Description: + +import subprocess +import shlex +from decimal import Decimal + +def get_request_time(url): + cmd = ''' + curl -sIL -w "{} {} {} {} {} {} {}" -o /dev/null {} + '''.format('%{time_namelookup}', '%{time_connect}', '%{time_appconnect}', + '%{time_redirect}', '%{time_pretransfer}', '%{time_starttransfer}', + '%{time_total}', url) + cmds = shlex.split(cmd) + p = subprocess.Popen(cmds, stdout=subprocess.PIPE) + p.wait() + byte_data = p.stdout.read() + p.stdout.close() + time = byte_data.decode('utf-8') + return time.split(" ") + +if __name__ == "__main__": + import sys + args = sys.argv[1:] + if len(args) < 1: + raise Error('url not null') + url = args[0] + times = get_request_time(url) + dns = Decimal(times[0]) + conn = Decimal(times[1]) + tcp = conn - dns + app_conn = Decimal(times[2]) + ssl = app_conn - conn if app_conn > 0 else 0 + pretransfer = Decimal(times[4]) + starttransfter = Decimal(times[5]) + total = Decimal(times[6]) + server = starttransfter - pretransfer + data_trans = total - starttransfter + l1 = ' {:0.6f} {:0.6f} {:0.6f} {:0.6f} {:0.6f}'.format( + dns, tcp, ssl, server, data_trans) + l3 = '|-- DNS --|-- TCP --|-- SSL --|-- SVR --|-- DTS --|' + l4 = '|------------------ TOTAL {:0.6f} ---------------|'.format(total) + print(l1) + print(l3) + print(l4) + diff --git a/bin/http/time_format b/bin/http/time_format index cad1cb0..86d1b10 100644 --- a/bin/http/time_format +++ b/bin/http/time_format @@ -4,5 +4,5 @@ time_redirect: %{time_redirect}\n time_pretransfer: %{time_pretransfer}\n time_starttransfer: %{time_starttransfer}\n - ----------\n + -----------------------------\n time_total: %{time_total}\n diff --git a/bin/kindle/push b/bin/kindle/push new file mode 100755 index 0000000..8e206a9 --- /dev/null +++ b/bin/kindle/push @@ -0,0 +1,19 @@ +#!/usr/bin/env bash +# Author: wxnacy(wxnacy@gmail.com) +# Description: 推送书籍到 kindle +# vim ${HOME}/.config/wshell/kindle +# email=your_kindle@kindle.cn + +. ${HOME}/.config/wshell/kindle + +path=$1 + +if [ ! -f $path ] +then + echo "Can not find the file '$path'" + exit 0 +fi + +mutt $email -a $path < /dev/null +echo 'Success!' +