通过本地web服务器向几乎任何交互式命令行工具发送输入

muxnect的Python项目详细描述


Pypi VersionBuild Status

通过本地 网络服务器。

muxnect是一个工具,它调用tmux来创建会话,然后包装 围绕它向终端发送鼠标事件或按键的方法 通过本地Web服务器。

快速介绍

一个简单的方法来连接任何交互式cli工具:

$ muxnect -w <tmux_window_name> -c <some_interactive_tool>

现在就把一些发帖请求 http://localhost:6060/muxnect/tmux_window_name

让我们尝试使用python:

>>>importrequests>>>url='http://localhost:6060/muxnect/<tmux_window_name>'>>>requests.post(url,data={'keys':'wonderful keystrokes'})<Response[200]>

就这样,我们的web服务器刚刚将wonderful keystrokes发送到 <some_interactive_tool>

示例

好吧,那可能会让你困惑。

下面是一些很酷的例子:

你好世界-Python

在这里,我们将通过muxnect在Python中打印hello world

让我们调用muxnect来启动一个python控制台 打印Hello World:

$ muxnect -w hello_world -c python

现在连接另一个Python控制台并使用它让我们发送一个POST 请求muxnect的服务器:

>>>importrequests>>>url='http://localhost:6060/muxnect/hello_world'>>>hello_world='print("Hello World!")'# send return key after it is done sending `keys`>>>requests.post(url,data={'keys':hello_world,'enter':'true'})<Response[200]>

(当然,您可以使用任何好的方式来发出post请求,而不仅仅是 仅限于python请求)

我们通过python控制台启动了Hello World!。 穆克斯内克

现在,让我们看一下separator参数:

# send KeyboardInterrupt after sending keys>>>requests.post(url,data={'keys':'send Ctrl+c; C-c','separator':'; '})<Response[200]># separator will split keys to preserve any special keys# here it will send "send Ctrl+c" and then immediately "C-c" (Ctrl+c, KBInterrupt)# you can use any number of separator blocks in `keys` param# try similar code without using separators>>>requests.post(url,data={'keys':'no Ctrl+c; C-c'})<Response[200]># note how it sends raw keys directly# send return key and EOF (Ctrl+d) to end our python session>>>requests.post(url,data={'keys':'Enter; C-d','separator':'; '})<Response[200]>

当我们需要发送 原始密钥和特殊密钥没有它,keyparam将是 原汁原味地解释。

tmux official docs, 以下是所有可用的特殊键:

tmux allows a command to be bound to most keys, with or without a prefix key. When specifying keys, most represent themselves (for example ‘A’ to ‘Z’). Ctrl keys may be prefixed with ‘C-’ or ‘^’, and Alt (meta) with ‘M-’. In addition, the following special key names are accepted: Up, Down, Left, Right, BSpace, BTab, DC (Delete), End, Enter, Escape, F1 to F12, Home, IC (Insert), NPage/PageDown/PgDn, PPage/PageUp/PgUp, Space, and Tab.

我们完了。用Ctrl +D.

退出MUXNECT中运行的TMUX会话

控制媒体播放

对于一个真实的例子,让我们尝试在 mpv-player

如果还没有apt的mpv,您可以安装它

让我们使用muxnect通过mpv播放一些视频:

$ muxnect -w playback -c "mpv --loop-file https://github.com/mediaelement/mediaelement-files/raw/master/big_buck_bunny.mp4"

稍等视频出现,然后我们将向此发送输入 MPV运行实例:

>>>importrequests>>>url='http://localhost:6060/muxnect/playback'# space key pauses the video in mpv by default>>>requests.post(url,data={'keys':' '})<Response[200]># kill this tmux window>>>requests.post(url,data={'kill':'true'})<Response[200]>

语法糖

muxnect还为python提供了一个简单的api来发出post请求:

>>>importmuxnect>>>url='http://localhost:6060/muxnect/cute_cli'>>>client=muxnect.Client(url,default_data={'enter':'true'})>>>client.send('type this, press enter and kill session',data={'kill':'true'})

安装

必须安装tmux才能使用 这个。如果它不在 您的apt存储库。

muxnect最适合Python 3

安装pypa的最新稳定版本:

$ pip install muxnect

或安装最新的开发版本:

$ git clone https://github.com/ritiek/muxnect
$ cd muxnect
$ python setup.py install

用法

usage: muxnect [-h] -c CMD -w WINDOW_NAME [-d] [-s SESSION_NAME]
                 [-b BIND_ADDRESS] [-p PORT]

Send input to just about any interactive command-line tool through a local web
server

optional arguments:
  -h, --help            show this help message and exit
  -d, --detach          detach from ongoing session (default: False)
  -s SESSION_NAME, --session-name SESSION_NAME
                        tmux's session name (default: muxnect)
  -b BIND_ADDRESS, --bind-address BIND_ADDRESS
                        address to bind on, local network: 0.0.0.0 (default:
                        127.0.0.1)
  -p PORT, --port PORT  port number to listen on (default: 6060)

required arguments:
  -c CMD, --cmd CMD     interactive command to send input to (default: None)
  -w WINDOW_NAME, --window-name WINDOW_NAME
                        tmux's window name (default: None)

URL以以下格式生成:

http://<hostaddress>:<port>/<session_name>/<window_name>

POST请求可以采用以下参数:

keys - mouse events/keystrokes to send (Default: None)
separator - split `keys` parameter on a character or string (Default: None)
enter - send enter key immediately after sending `keys` (Default: False)
kill - kill tmux window after proceeding with any other params (Default: False)

安全说明

由于目前无法进行身份验证,请仅在完全信任的系统(和本地网络)上使用此工具。如果攻击者不知何故知道了muxnect正在监听的url,没有任何东西可以阻止他们运行任意shell命令,并完全扰乱您的正常工作。请小心!

进一步延伸

有没有想过控制你笔记本电脑上运行的电影,但却被放在离你几米远的地方好吧,现在您可以使用android应用程序,比如HTTP-Shortcuts(由@Waboodoo构建),它可以用于创建自定义http请求。

剩下的留给你:)

为什么要muxnect

tmux+connect=muxnect:心脏:

许可证

License

欢迎加入QQ群-->: 979659372 Python中文网_新手群

推荐PyPI第三方库


热门话题
java实现双锁并发队列   java如何更改SpringWS 2.4.4版或更高版本中maxOccurs的默认限制?   java Selenium RC如何处理动态行?   java何时引入Javac StringBuilder/StringBuffer优化?   java mediaplayer在R.raw Android上失败   java JPA2:不区分大小写,就像在任何地方匹配一样   如何从JDK15中提取java预览类文件?   java与mySQL删除查询不一致   从Shutdownow()返回的java列表<Runnable>无法转换为提交的Runnable   java如何从回调接口获取对象值?   java如何获得视图的高度,在xml中可见性和高度定义为wrap_内容?   postgresql Mybatis在启动时遇到“由以下原因引起:java.net.UnknownHostException:localhost”,为什么?   带有实例调用的方法调用的java intellij格式化程序换行   由于特定的第三方库,java Proguard产生运行时错误   动画JAVA JPanel同时滑出和滑入