「工具」loon - 一个懒人适用的远程命令工具箱

相比于 Shell,Python 提供了更好的流程控制,因此,前一段时间我尝试使用 Python 完成了 sync-deploy 命令集的重写(见另一篇文章)。后续新特性的添加都只将在 loon 项目《https://github.com/ShixiangWang/loon》上,sync-deploy 只负责维护异常的修复。

值得一提的是,loon 的使用需要 Python 3的环境以及几个依赖包,所以 sync-deploy 可能更加轻量。


loon is a Python toolkit for operating remote host based on SSH. Idea for developing loon comes from sync-deploy, which is limited by its pure bash code. Therefore, I use Python to implement it and more features will be added to it in the future.

Installation

Install from pypi:

pip install loon

Install from GitHub:

pip install git+https://github.com/ShixiangWang/loon

Usage

Configuration

To access remote host without typing password, you have to generate ssh key with ssh-keygen command if it is not available.

$ ssh-keygen

Follow the commands, for simplicity, just type ENTER to proceed.

Copy your key to remote server, replace user with your username and host with your remote host ip address.

$ ssh-copy-id -i ~/.ssh/id_rsa user@host

Host management

  • Add a remote host
$ loon add -U wsx -H 127.0.0.1 -P 22
=> Added successfully!
# Default port is 22, so don't need to specify it
# And we can create a host alias, otherwise
# it is same as username of remote host
$ loon add -U wsx -H 127.0.0.2 -N host2      
=> Added successfully!
  • List all remote hosts
$ loon list
+-----+--------+----------+----+
|Alias|Username|IP address|Port|
+-----+--------+----------+----+
|<wsx>|wsx     |127.0.0.1 |22  |
+-----+--------+----------+----+
|host2|wsx     |127.0.0.2 |22  |
+-----+--------+----------+----+
<active host>
  • Rename alias
$ loon rename wsx host1
$ loon list
+-------+--------+----------+----+
|Alias  |Username|IP address|Port|
+-------+--------+----------+----+
|<host1>|wsx     |127.0.0.1 |22  |
+-------+--------+----------+----+
|host2  |wsx     |127.0.0.2 |22  |
+-------+--------+----------+----+
<active host>
  • Switch active remote host
$ loon switch -N host2
=> Activated.
$ loon list
+-------+--------+----------+----+
|Alias  |Username|IP address|Port|
+-------+--------+----------+----+
|host1  |wsx     |127.0.0.1 |22  |
+-------+--------+----------+----+
|<host2>|wsx     |127.0.0.2 |22  |
+-------+--------+----------+----+
<active host>
  • Delete a host
$ loon delete -N host2
=> Removing host from available list...
=> Removing active host...
=> Changing active host to host1
$ loon list
+-------+--------+----------+----+
|Alias  |Username|IP address|Port|
+-------+--------+----------+----+
|<host1>|wsx     |127.0.0.1 |22  |
+-------+--------+----------+----+
<active host>

Common tasks

  • Run commands
$ loon run 'ls -l ~'
total 168
drwxr-xr-x     2 wsx liulab     25 Apr  7 23:26 bin
drwxr-xr-x.    2 wsx liulab      6 Apr  4 10:36 Desktop
drwxr-xr-x.    2 wsx liulab      6 Apr  4 10:36 Documents
drwxr-xr-x.    3 wsx liulab     69 Jun 10 16:57 Downloads
drwxr-xr-x     2 wsx liulab      6 Sep 30 10:23 facet
drwxr-xr-x    11 wsx liulab   4096 Sep 22 20:13 metawho
drwxr-xr-x.    2 wsx liulab      6 Apr  4 10:36 Music
drwxr-xr-x     3 wsx liulab     60 Apr 30 17:50 Notebooks
drwxr-xr-x.    2 wsx liulab      6 Apr  4 10:36 Pictures
drwxr-xr-x     6 wsx liulab    114 Sep 27 17:33 projects
drwxr-xr-x     6 wsx liulab     96 Jun 27 16:50 projects_bk
drwxr-xr-x.    2 wsx liulab      6 Apr  4 10:36 Public
drwxr-xr-x.    2 wsx liulab      6 Apr  4 10:36 Templates
drwxr-xr-x     5 wsx liulab   4096 Oct  3 12:24 test
drwxr-xr-x  3480 wsx liulab 114688 Oct  3 13:44 tmp
drwxr-xr-x     3 wsx liulab     32 Aug 22 17:13 tools
drwxr-xr-x.    2 wsx liulab      6 Apr  4 10:36 Videos
  • Run local scripts

This will upload scripts to remote host firstly, then run them.

$ loon run -f ../../tests/scripts/t*.py
=> Starting upload...

t1.py                                          100%   50    49.0KB/s   00:00    
t2.py                                          100%   50    77.6KB/s   00:00    

=> Finished uploading in 1s
=> Getting results:
This is t1 script.
This is t2 script.
  • If input contains both files and directories, all files in directory will not be executed. This is a way to include child scripts which does not need to be executed.
  • If input is only a directory, all scripts (not including scripts in subdirectories) under it will be executed. This is the way to maintain an independent project.

You can include data directory using --data flag, specify program like bash or python using --prog flag and set remote directory using --dir flag.

  • Upload and download files

Use them like cp command. At default, use scp command to do the job, set --rsync to use rsync command (--rsync is disabled in Windows).

$ loon upload -h
usage: loon upload [-h] [-v] [--rsync] source [source ...] destination

positional arguments:
  source         Source files to upload
  destination    Remote destination directory

optional arguments:
  -h, --help     show this help message and exit
  -v, --verbose  set loglevel to INFO
  --rsync        Use rsync instead of scp

optional arguments:
  -h, --help     show this help message and exit
  -v, --verbose  set loglevel to INFO

$ loon download -h
usage: loon download [-h] [-v] [--rsync] source [source ...] destination

positional arguments:
  source         Source files to download
  destination    Local destination directory, note '~' should be quoted in
                 some cases

optional arguments:
  -h, --help     show this help message and exit
  -v, --verbose  set loglevel to INFO
  --rsync        Use rsync instead of scp
  • Batch process commands

By providing a structed file (CSV, TSV etc) and a sample command with placeholders {index} refer to column index (0 based) of file, batch command can be used to execute a batch of commands. Users can set thread number by -T flag and use --dry flag to dry run the code.

$ loon batch -f src/loon/data/samplefile.csv 'echo hello {0}'
hello TCGA-2A-A8VO-01
hello TCGA-2A-A8VT-01
hello TCGA-2A-A8VV-01
hello TCGA-2A-A8VX-01

$ loon batch -f src/loon/data/samplefile.csv 'echo hello {0}' -T 4
hello TCGA-2A-A8VO-01
hello TCGA-2A-A8VT-01
hello TCGA-2A-A8VV-01
hello TCGA-2A-A8VX-01

$ loon batch -f src/loon/data/samplefile.csv 'echo hello {0}' --dry
=> Running echo hello TCGA-2A-A8VO-01
=> Running echo hello TCGA-2A-A8VT-01
=> Running echo hello TCGA-2A-A8VV-01
=> Running echo hello TCGA-2A-A8VX-01

PBS management and tasks

  • pbstemp - Generate a PBS template file
  • pbsgen - Generate a batch of PBS files
  • pbsgen_example - Generate example files for pbsgen command
  • pbssub - Submit PBS tasks
  • pbsdeploy - Upload a target directory and submit containing PBS files (have .pbs extension)
  • pbscheck - Check status of PBS job on remote host

More details please see -h option of the commands above.

Current usage info

usage: loon [-h] [--version] [--author]
            {add,delete,switch,list,rename,run,upload,download,batch,pbstemp,pbsgen,pbsgen_example,pbssub,pbsdeploy,pbscheck}
            ...

Be an efficient loon.

optional arguments:
  -h, --help            show this help message and exit
  --version             show program's version number and exit
  --author              show info of program's author

subcommands:
  {add,delete,switch,list,rename,run,upload,download,batch,pbstemp,pbsgen,pbsgen_example,pbssub,pbsdeploy,pbscheck}
                        description
    add                 Add a remote host
    delete              Delete a remote host
    switch              Switch active remote host
    list                List all remote hosts
    rename              Rename host alias
    run                 Run commands or scripts on remote
    upload              Upload files to active remote host
    download            Download files from active remote host
    batch               Batch process commands with placeholders
    pbstemp             Generate a PBS template file
    pbsgen              Generate a batch of PBS files
    pbsgen_example      Generate example files for pbsgen command
    pbssub              Submit PBS tasks
    pbsdeploy           Deploy target destination to remote host
    pbscheck            Check status of PBS job on remote host
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 199,711评论 5 468
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 83,932评论 2 376
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 146,770评论 0 330
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 53,799评论 1 271
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 62,697评论 5 359
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 48,069评论 1 276
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 37,535评论 3 390
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,200评论 0 254
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 40,353评论 1 294
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,290评论 2 317
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,331评论 1 329
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,020评论 3 315
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 38,610评论 3 303
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,694评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 30,927评论 1 255
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 42,330评论 2 346
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 41,904评论 2 341

推荐阅读更多精彩内容

  • rljs by sennchi Timeline of History Part One The Cognitiv...
    sennchi阅读 7,267评论 0 10
  • NAME dnsmasq - A lightweight DHCP and caching DNS server....
    ximitc阅读 2,785评论 0 0
  • 炎热的夏天,是不是连门都不愿意出? 游戏进行中,是不是觉得饭来张口就好了? 周末在家,是不是希望吃个饭一觉睡到天荒...
    喜欢航平的少女阿阅读 263评论 0 0
  • 她删掉了那些聊天记录,把他从聊天列表中移除,把手机里存着的他的照片删掉。 她想这次真的要好好放下了,她想重新开始 ...
    熊家小妞阅读 199评论 0 0
  • 我习惯在地铁上,带着耳机,听罗胖的“得到”或者其他我特别喜欢的音频,还一边听一边写笔记,以至于有好几次都错过了站...
    马如龙卜冉达阅读 404评论 0 2