如何从智能手机激活笔记本电脑上的脚本

我想从我的智能手机(iPhone,即iPhone)远程运行我的笔记本电脑上的Linux脚本。 为简单起见,假设脚本名为myscript.sh ,它位于/root/Desktop 。 我以root用户身份运行(是的,我知道我正在以root身份运行。)

根据我收集的信息,这些是可能的解决方案:
1.为iPhone写一些应用程序进行交流(对我来说听起来很辛苦,太复杂了。)
2.发送电子邮件和使用“Procmail”的东西
3.发送电子邮件和使用“Postfix”的东西

上面提到的所有方法对我来说都是未知的; 直到最近我才听说过他们。 所以任何人都可以通过任何方法启发我,让我从我的智能手机上运行我的笔记本电脑的Linux。

谢谢!

编辑:顺便说一句,我没有在家里运行的服务器。

选项1:SSH + ngrok

  1. 我相信在22端口上侦听的SSH服务器默认出现在Ubuntu上。
  2. 使用sudo apt-get install ngrok-client
  3. 在ngrok.com上注册以获取身份validation令牌(需要使用非http协议)。
  4. 让当地的ngrok了解您的帐号echo 'auth_token: YOUR_AUTH_TOKEN' > ~/.ngrok
  5. 运行ngrok -proto=tcp 22

你会得到类似的东西

 Tunnel Status online Version 1.6/1.6 Forwarding tcp://ngrok.com:52418 -> 127.0.0.1:22 Web Interface 127.0.0.1:4040 # Conn 0 Avg Conn Time 0.00ms 

ngrok.com:52418是您可以从任何地方访问的本地SSH服务器。 默认情况下,端口是随机给出的,但您可以在配置文件中设置它。 有关详细信息,请参阅文档 。

使用ssh ngrok.com -p 52418测试。 您可以从智能手机的任何SSH客户端连接到它(我正在使用VX ConnectBot )并使用本地计算机上的命令行执行所有操作,例如执行脚本:]

选项2:简单的HTTP服务器+ ngrok

为简化示例,我将在~/test/创建两个文件:

  • test.sh ,它将输出当前时间戳并将其附加到log.txt

     #! /bin/sh date +%s date +%s >> log.txt 
  • server.py ,它将等待远程HTTP请求并执行test.sh

     #!/usr/bin/env python3 import http.server, os, subprocess class HTTPRequestHandler(http.server.BaseHTTPRequestHandler): def do_GET(self): path = 'test.sh' # Make path absolute if path[0] != '/': path = os.getcwd() + '/' + path # If file exists, execute it and return output if os.path.isfile(path): if os.access(path, os.X_OK): p = subprocess.Popen(path, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE) stdout, stderr = p.communicate() content = stdout.decode('UTF-8') else: content = path + ' is not executable' self.send_response(200) self.send_header('Content-type', 'text/html') self.end_headers() self.wfile.write(content.encode('UTF-8')) else: self.send_header('Content-type', 'text/html') self.send_response(404, 'File Not Found') self.end_headers() PORT = 8000 handler_class = HTTPRequestHandler httpd = http.server.HTTPServer(('', PORT), handler_class) print('Listening at port', PORT) try: httpd.serve_forever() except KeyboardInterrupt: pass httpd.server_close() print('Server stopped') 

现在我们可以生成server.py可执行文件,运行它,然后在浏览器中打开http://127.0.0.1:8000并看到该脚本实际工作 – 它将在我们的浏览器窗口中输出当前时间戳+将其附加到~/test/log.txt

但是127.0.0.1只是localhost,我们想从互联网上访问我们的服务器。 这就是ngrok用武之地。用sudo apt-get install ngrok-client安装它并执行ngrok 8000 ,它会给你类似的东西:

 Tunnel Status online Version 1.6/1.6 Forwarding http://a1b2c3d4.ngrok.com -> 127.0.0.1:8000 Forwarding https://a1b2c3d4.ngrok.com -> 127.0.0.1:8000 Web Interface 127.0.0.1:4040 # Conn 0 Avg Conn Time 0.00ms 

https://a1b2c3d4.ngrok.com是公共地址(即可从互联网访问),它实际上由您的本地服务器提供服务(将记住执行您的脚本:))。 默认情况下,子域名是随机给出的,但您可以在配置文件中设置它。 有关详细信息,请参阅文档 。

您可以在后台运行服务器和ngrok,但这是另一个故事:]

选项3:TeamViewer(或VNC + ngrok)

您甚至可以在笔记本电脑上使用TeamViewer主机和智能手机上的TeamViewer客户端应用程序进行全图形远程控制。 容易,但移动使用可能太耗费流量。

您可以使用笔记本电脑上的VNC服务器+ ngrok和智能手机上的VNC客户端代替TeamViewer。

虽然这绝不是回答问题的最快或最先进的方式,但我认为这可能是最容易实现和理解的方式。

我通常使用的方法是设置一个虚拟电子邮件帐户,并每秒轮询它以获取新的电子邮件。 这通常需要大约5-10秒,从您在手机上发送电子邮件到脚本运行。 请注意,您必须在您创建的电子邮件地址中启用imap。

还要确保您的bash脚本以该行开头:

#!/usr/bin/env bash

我使用的python脚本如下所示。 当然,您必须在前四个全大写变量中输入相关信息。 打开文本编辑器并将以下内容保存为email-run.py

 #!/usr/bin/env python import imaplib,time,serial,subprocess IMAP_SERVER = 'imap.gmail.com' USERNAME = 'username' PASSWORD = 'password' PATH_TO_SCRIPT = '/root/Desktop/myscript.sh' EXECUTE_IF_EMAIL_HAS = 'run' def main(): imap_conn = imaplib.IMAP4_SSL(IMAP_SERVER) imap_conn.login(USERNAME,PASSWORD) imap_conn.select() while(True): text = get_new_body(imap_conn) if text is not None: if EXECUTE_IF_EMAIL_HAS.lower() in text.lower(): subprocess.call(PATH_TO_SCRIPT) imap_conn.store(1,'+FLAGS','\\Deleted') imap_conn.expunge() time.sleep(1) def get_new_body(imap_conn): imap_conn.recent() (typ, data) = imap_conn.search(None, 'ALL') if data[0] == '': return None (typ, data) = imap_conn.fetch(1,'(RFC822)') msg = data[0][1].split('\r\n\r\n') index = 0 while(True): if "text/plain" in msg[index]: break index += 1 return msg[index+1] if __name__ == "__main__": main() 

你可以运行它:

python email-run.py

看看Pushbullet吧 。 在智能手机上,向PC发送消息(“推送”)。 一个简单的dbus监听器在您的PC上运行,并在收到推送时运行脚本。 有一个Ubuntu AppIndicator,允许您在没有浏览器的情况下监听推送。 看到这里 。