-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathscpwrap
159 lines (129 loc) · 4.9 KB
/
scpwrap
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
#!/bin/python3
import argparse, sys
from os.path import exists, isdir, join, basename, abspath, dirname
from os import system
import shlex
#result = os.system('color')
BLUE = '\033[34m'
GREEN = '\033[32m'
RED = '\033[31m'
YELLOW = '\033[33m'
WHITE = '\033[0m'
RSTCOLORS = '\033[0m'
ERASE_LINE = '\x1b[2K'
parser = argparse.ArgumentParser(description='Wrapper around SCP using a control socket')
parser.add_argument('-s', help='Control Socket',dest='socket', required=True)
parser.add_argument('-d', help='Download Directory',dest='download_dir', required=True, default="./")
parser.add_argument('-u', help='Upload Directory',dest='upload_dir', required=True, default="./")
args = parser.parse_args()
UPLOAD_DIR = abspath(args.upload_dir)
DOWNLOAD_DIR = abspath(args.download_dir)
TARGET_DIR = ""
AVAIL_CONFIGS = ["upload-from","download-from","download-to"]
VERSION = 1.0
def print_info(msg):
print(BLUE + "[*] " + msg + RSTCOLORS)
def print_good(msg):
print(GREEN + "[+] " + msg + RSTCOLORS)
def print_bad(msg):
print(RED + "[-] " + msg + RSTCOLORS)
def doesFileExist(file):
return exists(file)
def doesDirExist(file):
return isdir(file)
def printSettings():
settings = YELLOW + f"\nUpload From: {UPLOAD_DIR}\nDownload To: {DOWNLOAD_DIR}\nDownload From: {TARGET_DIR}\n" + RSTCOLORS
print(settings)
def configSetting(setting):
global UPLOAD_DIR
global DOWNLOAD_DIR
global TARGET_DIR
s = setting.split("=")
if s[0] == "download-to":
if doesDirExist(dirname(s[1])):
DOWNLOAD_DIR = s[1]
print_good("Updated Settings")
else:
print_bad("Target Directory Does not Exist")
elif s[0] == "upload-from":
if doesDirExist(dirname(s[1])):
UPLOAD_DIR = s[1]
print_good("Updated Settings")
else:
print_bad("Target Directory Does not Exist")
if s[0] == "download-from":
print_good("Updated Settings")
TARGET_DIR = s[1]
def dowloadFile(srcFilename, destFilename):
cmd = f"scp -o ControlPath={args.socket} @:{srcFilename} {destFilename}"
print_info(f"{srcFilename} --> {destFilename}")
print_info("SCP Status:")
system(cmd)
def uploadFile(srcFilename, destFilename):
cmd = f" scp -o ControlPath={args.socket} {srcFilename} @:{destFilename}"
print_info(cmd)
print_info("SCP Status:")
system(cmd)
def main():
if not doesDirExist(UPLOAD_DIR):
print_bad("Upload Directory Does not Exist!")
sys.exit()
if not doesDirExist(DOWNLOAD_DIR):
print_bad("Download Directory Does not Exist!")
sys.exit()
if not doesFileExist(args.socket):
print_bad("Socket Control File Does not Exist!")
sys.exit()
userInput = ""
while True:
printSettings()
userInput = input(RED+"scp> "+RSTCOLORS)
if userInput == "exit" or userInput == "quit":
sys.exit()
else:
if "=" in userInput:
if userInput.split("=")[0] in AVAIL_CONFIGS:
print("changing configs")
configSetting(userInput)
elif "version" == userInput:
print_info(str(VERSION))
elif userInput.startswith("get "):
cmd = shlex.split(userInput) #https://stackoverflow.com/questions/79968/split-a-string-by-spaces-preserving-quoted-substrings-in-python
if len(cmd) == 2:
src = cmd[1]
dest = join(DOWNLOAD_DIR,basename(src))
elif len(cmd) == 3:
src = cmd[1]
if "/" in cmd[2]:
dest = cmd[2]
else:
dest = join(DOWNLOAD_DIR,basename(cmd[2]))
#print(dest)
if doesDirExist(dirname(dest)):
if TARGET_DIR != "":
src = join(TARGET_DIR,src)
dowloadFile(src,dest)
else:
print_bad("Download Directory Does Not Exist!")
elif userInput.startswith("put "):
cmd = shlex.split(userInput) #https://stackoverflow.com/questions/79968/split-a-string-by-spaces-preserving-quoted-substrings-in-python
if len(cmd) == 2:
src = cmd[1]
dest = join(UPLOAD_DIR,basename(src))
elif len(cmd) == 3:
src = cmd[1]
if "/" in cmd[2]:
dest = cmd[2]
else:
dest = join(UPLOAD_DIR,basename(cmd[2]))
#print(dest)
if doesFileExist(src):
if TARGET_DIR != "":
src = join(TARGET_DIR,dest)
uploadFile(src,dest)
else:
print_bad("Source File Does Not Exist!")
try:
main()
except KeyboardInterrupt:
print("^punt!")