shell2http

Executing shell commands via HTTP server
授权协议 MIT License
开发语言 SHELL
所属分类 应用工具、 终端/远程登录
软件类型 开源软件
地区 不详
投 递 者 艾泽语
操作系统 跨平台
开源组织
适用人群 未知
 软件概览

shell2http

HTTP-server to execute shell commands. Designed for development, prototyping or remote control.Settings through two command line arguments, path and shell command.By default bind to :8080.

Usage

shell2http [options] /path "shell command" /path2 "shell command2" ...
options:
    -host="host"      : host IP for http server (default bind to all interfaces)
    -port=NNNN        : port for http server, 0 - to receive a random port (default 8080)
    -form             : parse query into environment vars, handle uploaded files
    -cgi              : run scripts in CGI-mode:
                        - set environment variables with HTTP-request information
                        - write POST|PUT|PATCH-data to script STDIN (if is not set -form)
                        - parse headers from script (eg: "Location: URL\n\n")
    -export-vars=var  : export environment vars ("VAR1,VAR2,...")
                        by default export PATH, HOME, LANG, USER, TMPDIR
    -export-all-vars  : export all current environment vars
    -no-index         : don't generate index page
    -add-exit         : add /exit command
    -log=filename     : log filename, default - STDOUT
    -shell="shell"    : shell for execute command, "" - without shell (default "sh")
    -cache=N          : caching command out for N seconds
    -one-thread       : run each shell command in one thread
    -show-errors      : show the standard output even if the command exits with a non-zero exit code
    -include-stderr   : include stderr to output (default is stdout only)
    -cert=cert.pem    : SSL certificate path (if specified -cert/-key options - run https server)
    -key=key.pem      : SSL private key path
    -basic-auth=""    : setup HTTP Basic Authentication ("user_name:password"), can be used several times
    -timeout=N        : set timeout for execute shell command (in seconds)
    -no-log-timestamp : log output without timestamps
    -version
    -help

In the -form mode, variables are available for shell scripts:

  • $v_NNN -- data from query parameter with name "NNN" (example: http://localhost:8080/path?NNN=123)
  • $filepath_ID -- uploaded file path, ID - id from <input type=file name=ID>, temporary uploaded file will be automatically deleted
  • $filename_ID -- uploaded file name from browser

To setup multiple auth users, you can specify the -basic-auth option multiple times.The credentials for basic authentication may also be provided via the SH_BASIC_AUTH environment variable.You can specify the preferred HTTP-method (via METHOD: prefix for path): shell2http GET:/date date

Install

MacOS:

brew tap msoap/tools
brew install shell2http
# update:
brew upgrade shell2http

Using snap (Ubuntu or any Linux distribution with snap):

# install stable version:
sudo snap install shell2http

# install the latest version:
sudo snap install --edge shell2http

# update
sudo snap refresh shell2http

Notice: the snap-package has its own sandbox with the /bin, /usr/bin directories which are not equal to system-wide PATH directories.

Download binaries from: releases (OS X/Linux/Windows/RaspberryPi)

Docker users:

docker pull msoap/shell2http

Build from source (minimum Go version is 1.12):

go get -u github.com/msoap/shell2http
# set link to your PATH if needed:
ln -s $(go env GOPATH)/bin/shell2http ~/bin/shell2http

Examples

shell2http /top "top -l 1 | head -10"
shell2http /date date /ps "ps aux"
shell2http -export-all-vars /env 'printenv | sort' /env/path 'echo $PATH' /env/gopath 'echo $GOPATH'
shell2http -export-all-vars /shell_vars_json 'perl -MJSON -E "say to_json(\%ENV)"'
shell2http -export-vars=GOPATH /get 'echo $GOPATH'
HTML calendar for current year
shell2http /cal_html 'echo "<html><body><h1>Calendar</h1>Date: <b>$(date)</b><br><pre>$(cal $(date +%Y))</pre></body></html>"'
Get URL parameters (http://localhost:8080/form?from=10&to=100)
shell2http -form /form 'echo $v_from, $v_to'
CGI scripts
shell2http -cgi /user_agent 'echo $HTTP_USER_AGENT'
shell2http -cgi /set 'touch file; echo "Location: /another_path\n"' # redirect
shell2http -cgi /404 'echo "Status: 404"; echo; echo "404 page"' # custom HTTP code
Upload file
shell2http -form \
    GET:/form 'echo "<html><body><form method=POST action=/file enctype=multipart/form-data><input type=file name=uplfile><input type=submit></form>"' \
    POST:/file 'cat $filepath_uplfile > uploaded_file.dat; echo Ok'

Testing upload file with curl:

curl -i -F uplfile=@some/file/path 'http://localhost:8080/file'
Simple http-proxy server (for logging all URLs)Setup proxy as "http://localhost:8080/"
shell2http -log=/dev/null -cgi / 'echo $REQUEST_URI 1>&2; [ "$REQUEST_METHOD" == "POST" ] && post_param="-d@-"; curl -sL $post_param "$REQUEST_URI" -A "$HTTP_USER_AGENT"'
Test slow connection (http://localhost:8080/slow?duration=10)
shell2http -form /slow 'sleep ${v_duration:-1}; echo "sleep ${v_duration:-1} seconds"'
Proxy with cache in files (for debug with production API with rate limit)get ` http://api.url/` as `http://localhost:8080/get?url= http://api.url/`
shell2http -form \
    /form 'echo "<html><form action=/get>URL: <input name=url><input type=submit>"' \
    /get 'MD5=$(printf "%s" $v_url | md5); cat cache_$MD5 || (curl -sL $v_url | tee cache_$MD5)'
Remote sound volume control (Mac OS)
shell2http /get  'osascript -e "output volume of (get volume settings)"' \
           /up   'osascript -e "set volume output volume (($(osascript -e "output volume of (get volume settings)")+10))"' \
           /down 'osascript -e "set volume output volume (($(osascript -e "output volume of (get volume settings)")-10))"'
Remote control for Vox.app player (Mac OS)
shell2http /play_pause 'osascript -e "tell application \"Vox\" to playpause" && echo ok' \
           /get_info 'osascript -e "tell application \"Vox\"" -e "\"Artist: \" & artist & \"\n\" & \"Album: \" & album & \"\n\" & \"Track: \" & track" -e "end tell"'
Get four random OS X wallpapers
shell2http /img 'cat "$(ls "/Library/Desktop Pictures/"*.jpg | ruby -e "puts STDIN.readlines.shuffle[0]")"' \
           /wallpapers 'echo "<html><h3>OS X Wallpapers</h3>"; seq 4 | xargs -I@ echo "<img src=/img?@ width=500>"'
Mock service with JSON API
curl "http://some-service/v1/call1" > 1.json
shell2http -cgi /call1 'cat 1.json' /call2 'echo "Content-Type: application/json\n"; echo "{\"error\": \"ok\"}"'
Windows example

Returns value of var for run in Windows cmd (http://localhost:8080/test?var=value123)

shell2http.exe -form /test "echo %v_var%"
With HTTP headers

Send custom HTTP headers:

shell2http -cgi / 'echo "Content-Type: application/javascript\n"; echo "{\"error\": \"ok\"}"'

On Windows:

shell2http.exe -cgi / "echo Content-Type: application/javascript& echo.& echo body"

More examples ...

Run from Docker-container

Example of test.Dockerfile for server for get current date:

FROM msoap/shell2http
# may be install some alpine packages:
# RUN apk add --no-cache ...
CMD ["/date", "date"]

Build and run container:

docker build -f test.Dockerfile -t date-server .
docker run --rm -p 8080:8080 date-server

SSL

Run https server:

shell2http -cert=./cert.pem -key=./key.pem ...

Generate self-signed certificate:

go run $(go env GOROOT)/src/crypto/tls/generate_cert.go -host localhost

See also

  • Emergency web server - spark
  • Share your terminal as a web application - gotty
  • Create Telegram bot from command-line - shell2telegram
  • A http daemon for local development - devd
  • Turn any program that uses STDIN/STDOUT into a WebSocket server - websocketd
  • The same tool configurable via JSON - webhook
  • http://note.youdao.com/noteshare?id=de31d10e8ca8a982f4ba28f9dbecfb11&sub=787C741168EB4371A767257E09D12EAF

  • 在通过curl请求http获取response header时, 发现字符串拼接一个问题。 比如以下程序: hadoop@1:~$ ct=$(curl -s -I http://www.baidu.com | grep Content-Type | awk '{print $2}') hadoop@1:~$ echo $ct text/html hadoop@1:~$ echo $ct"_post

  • 正向shell和反向shell 正向shell:控制端主动发起连接去连接被控制端 反向shell:被控制端主动连接控制端 在实战中,大多数采用反向shell,因为正向shell有很多因素导致连接失败, 比如说硬件设备有防火墙,入侵防御系统等,还有网站防火墙,端口占用,权限不足等场景,特别是硬件设备如果你正向连接被防火墙拦截导致打草惊蛇,后期攻击相当繁琐。 反向shell:而被控制端主动向外发送的数

  • python shell怎么打开 1.简介:如何在python中运行shell(bash命令) 2.工具/原料:python库:os.py 3.方法:import os command = 'date'#直接运行令 os.system(command) #结果: (当前日期) script = 'sample.js' #调用一本(当然也可以是sh/py/rb等) os.system('node s

  • 前言 shell编程在unix/linux世界中使用得非常广泛,熟练掌握shell编程也是成为一名优秀的unix/linux开发者和系统管理员的必经之路。脚本调试的主要工作就是发现引发脚本错误的原因以及在脚本源代码中定位发生错误的行,常用的手段包括分析输出的错误信息,通过在脚本中加入调试语句,输出调试信息来辅助诊断错误,利用调试工具等。但与其它高级语言相比,shell解释器缺乏相应的调试机制和调试

  • 概述 脚本:本质是一个文件,文件里面存放的是特定格式的指令,系统可以使用脚本解析器翻译或解析指令并执行(它不需要编译) shell 既是一个用 C 语言编写的应用程序,又是一种脚本语言(应用程序 解析 脚本语言) Shell 提供了一个界面,用户通过这个界面访问操作系统内核的服务。 Ken Thompson 的 sh 是第一种 Unix Shell,Windows Explorer 是一个典型的图

  • 一、curl 参数 内容 -H 请求头 -d POST内容 -X 请求协议 -x 代理 1. get请求 curl命令默认下就是使用get方式发送http请求。 1 curl www.baidu.com 2. post请求 使用-d参数,形式如下: 1 curl -d "param1=value1&param2=value2" www.baidu.com 3.设置代理 1 curl -x 192.

  • Linux 使用shell发送http请求 一、curl 1. get请求 curl命令默认下就是使用get方式发送http请求。 curl www.baidu.com 2. post请求 curl -X POST www.baidu.com 使用-d带参数请求参数,形式如下: curl -X POST <url> -d <data> //curl -X 请求方式(POST|GET) 请求地

相关阅读

相关文章

相关问答

相关文档