-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpullLocal.py
80 lines (61 loc) · 1.93 KB
/
pullLocal.py
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
# take argument -date yyyy-mm-dd
# scp from remote server to local directory
import os
import argparse
import paramiko
import re
parser = argparse.ArgumentParser()
parser.add_argument('-date', help='date to pull from remote server')
parser.add_argument('-ip', help='ip for remote server')
parser.add_argument('-user', help='username for remote server')
parser.add_argument('-password', help='password for remote server')
args = parser.parse_args()
date = args.date
# check date
if date is None:
print('date (-date yyyy-mm-dd) is not provided')
exit()
# check ip
if args.ip is None:
print('ip (-ip) is not provided')
exit()
# check username
if args.user is None:
print('WARN: username (-user) is not provided')
# check password
if args.password is None:
print('WARN: password (-password) is not provided')
# check if date is valid
# regex
# r'\d{4}-\d{2}-\d{2}'
# four digits, - , two digits, - , two digits
# note to self: learn regex, don't let copilot do it all
datePattern = re.compile(r'\d{4}-\d{2}-\d{2}')
if datePattern.match(date):
# print('date is valid')
pass
else:
print('date is invalid')
print("date should be in format yyyy-mm-dd")
exit()
# try to pull
filePath = '/root/bus/'
fileName = 'busData-' + date + '.json'
try:
ssh = paramiko.SSHClient()
ssh.load_host_keys(os.path.expanduser(os.path.join("~", ".ssh", "known_hosts")))
server = args.ip
username = args.user
password = args.password
ssh.connect(server, username=username, password=password)
sftp = ssh.open_sftp()
localpath = os.path.join(os.getcwd(), fileName) # Local directory where Python is running
remotepath = os.path.join(filePath, fileName) # Remote path of the file on the server
sftp.get(remotepath, localpath) # Use 'get' method to copy from remote to local
sftp.close()
ssh.close()
print("success")
except Exception as e:
print('scp failed')
print(e)
exit()