-
Notifications
You must be signed in to change notification settings - Fork 0
/
extract.py
92 lines (83 loc) · 3.57 KB
/
extract.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
81
82
83
84
85
86
87
88
89
90
91
92
# Script for running specific file extractor on Linux/AIX servers and downloading the output file to local machine (Python 2.7)
#!/usr/bin/env python
import pxssh
import pexpect
import getpass
import csv
import datetime
# Creating extracted file path
def createFILEpath(hostname, dateString):
extractedFile = "/xxx/XXX_" + str(dateString) + "_" + str(hostname.lower()) + ".extension"
return extractedFile
# Creating list of servers from which output files will be extracted
def createServerList(serverListTextFile):
try:
with open(serverListTextFile, "r") as f:
serverList = f.readlines()
serverList = set([x.strip() for x in serverList if x])
serverList = [x for x in serverList if x]
return serverList
except IOError as e:
print str(e)
# Output file extraction
def outputFileExtract(hostname, ipaddress, username, password, destinationPath):
try:
session = pxssh.pxssh()
outputFilePath = createFILEpath(hostname, dateString)
session.login(ipaddress, username, password)
session.sendline('sudo ./extraction_script')
session.expect(['password :', 'Password:', pexpect.EOF, pexpect.TIMEOUT])
session.sendline(password)
session.expect (['Finished', pexpect.EOF, pexpect.TIMEOUT])
session.sendline('sudo chown ' + username + ' ' + outputFilePath)
o = session.expect(['Password:', '$', pexpect.EOF, pexpect.TIMEOUT])
if o == 1:
session.sendline(password)
session.expect(['$', 'system.', pexpect.EOF, pexpect.TIMEOUT])
session.logout()
elif o == 2 or o == 3 or o == 4:
session.logout()
child = pexpect.spawn('scp ' + username + '@' + ipaddress + ':' + outputFilePath + ' ' + destinationPath)
child.expect(['assword:', pexpect.EOF, pexpect.TIMEOUT])
child.sendline(password)
child.expect([pexpect.EOF, pexpect.TIMEOUT])
child.close()
print (hostname + ' --- done')
except pxssh.ExceptionPxssh as e:
print "pxssh failed to login to " + hostname + " --- Reason: " + str(e)
# Main loop - opening csv file, calling extract method
def mainLoop(serversDataBaseCsv):
foundList = []
notFoundList = []
try:
with open("server_database.csv", "r") as csvFile:
myCsv = csv.DictReader(csvFile)
for dictFromCsv in myCsv:
for i in range(0, len(serverList)):
if dictFromCsv['server'].lower() == serverList[i].lower():
ipaddress = dictFromCsv['ip']
hostname = serverList[i].lower()
outputFileExtract(hostname, ipaddress, username, password, destinationPath)
foundList.append(str(serverList[i].lower()))
else:
notFoundList.append(str(serverList[i].lower()))
notFoundList = set(notFoundList)
notFoundList = [x for x in notFoundList if x not in foundList]
if notFoundList != []:
print "The following servers have not been found in .csv file: " + str(notFoundList)
except IOError as e:
print str(e)
# Clean variables
serverList = []
hostname = ''
# Variables
username = 'userID'
serverListTxt = 'from_which_extract.txt'
serversDataBaseCsv = 'server_list.csv'
destinationPath = '/home/XXXXX/Desktop/XXXX/'
# Beginning of a script
date = datetime.datetime.now()
dateString = str(date.strftime("%d%b%Y").upper())
serverList = createServerList(serverListTxt)
password = getpass.getpass('Server(s) password:')
mainLoop(serversDataBaseCsv)