Skip to content

Instantly share code, notes, and snippets.

@comodoro
Created August 19, 2020 09:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save comodoro/ea4a9b44f8e9b542cfc8e53d933dcffc to your computer and use it in GitHub Desktop.
Save comodoro/ea4a9b44f8e9b542cfc8e53d933dcffc to your computer and use it in GitHub Desktop.
Remote wake and ssh with Dragonfly on Windows POC
import socket
import subprocess
DELAY = 10
from dragonfly import get_engine, Function, Grammar, Key, MappingRule, RunCommand
def ping(host, port):
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.settimeout(DELAY/10)
if s.connect_ex((host, port)) == 0:
s.close()
return True
return False
class WakeChecker(object):
def __init__(self, host, port, action):
self._timer = None
self._host = host
self._port = port
self._action = action
def start(self):
self._timer = get_engine().create_timer(self.check, DELAY, True)
def check(self):
if ping(self._host, self._port):
self.stop()
# self._action.execute()
self._action()
def stop(self):
self._timer.stop()
class MyRule(MappingRule):
pronunciation = "My rule"
checker = WakeChecker('IP', 22,
#RunCommand(['start', 'c:\windows\sysnative\wsl.exe', 'ssh', 'USER@IP'], hide_window=False))
lambda: subprocess.Popen('start c:\windows\sysnative\wsl.exe ssh USER@IP', shell=True))
mapping = {
"connect to remote": RunCommand(['c:\programs\wolcmd\wolcmd.exe', '<MAC>', '<IP>', '255.255.255.0', '7']) +
Function(checker.start)
}
grammar = Grammar('MyGrammar')
grammar.add_rule(MyRule())
grammar.load()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment