diff --git a/CNU.txt b/CNU.txt new file mode 100644 index 0000000..e75e7ed --- /dev/null +++ b/CNU.txt @@ -0,0 +1,2 @@ +austin:pr0ject.px +This is a triumph diff --git a/Modules/file_parse/README.md b/Modules/file_parse/README.md new file mode 100644 index 0000000..eec68ba --- /dev/null +++ b/Modules/file_parse/README.md @@ -0,0 +1,10 @@ + + +# Parsing Module + +File Parsing +=========== + +This module helps us extract credential files from text files that are taken by the information center. + +Inorder to run a testfile choose one of the files in the testfiles folder and run the python script with the filename and extension after it. This would be such as testing the extraction of credentials from the file sample.txt by running: `python __file_parse__.py sample.txt` \ No newline at end of file diff --git a/Modules/file_parse/__init__.py b/Modules/file_parse/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/Modules/file_parse/__file_parse__.py b/Modules/file_parse/file_parse.py similarity index 58% rename from Modules/file_parse/__file_parse__.py rename to Modules/file_parse/file_parse.py index 32f3975..02742be 100644 --- a/Modules/file_parse/__file_parse__.py +++ b/Modules/file_parse/file_parse.py @@ -1,4 +1,6 @@ +import sys + #methods for obtaining credentials from a sample file. #The paramemter passed is the file itself once opened. #these methods are based on the sample of how the credentials will be sent remotely. @@ -7,7 +9,7 @@ def obt_login(text): first_line = text[0] line_list = first_line.split(',') #indentation can cause an error - return line_list[0].replace(" ", "") + return line_list[0].replace(" ", "") def obt_pass(text): first_line = text[0] @@ -15,23 +17,23 @@ def obt_pass(text): return line_list[1].replace(" ", "") def obt_message(text): - mess_line = text[2] + mess_line = text[1] return mess_line -#file concatination: -file = open("sample.txt") -text = file.readlines() - -#Printing the test file credentials - -usr_login = obt_login(text) -print(usr_login) - -usr_pass = obt_pass(text) -print(usr_pass) - -usr_mess = obt_message(text) -print(usr_mess) - - +##file concatination: +#file = open('testfiles/'+sys.argv[1]) +#text = file.readlines() +# +##Printing the test file credentials +# +#usr_login = obt_login(text) +#print(usr_login) +# +#usr_pass = obt_pass(text) +#print(usr_pass) +# +#usr_mess = obt_message(text) +#print(usr_mess) +# +# diff --git a/Modules/file_parse/sample.txt b/Modules/file_parse/testfiles/sample.txt similarity index 57% rename from Modules/file_parse/sample.txt rename to Modules/file_parse/testfiles/sample.txt index 5309366..4547618 100644 --- a/Modules/file_parse/sample.txt +++ b/Modules/file_parse/testfiles/sample.txt @@ -1,3 +1,2 @@ - login, pass - +login, pass This is a messsage diff --git a/Modules/file_parse/testfiles/sample1.txt b/Modules/file_parse/testfiles/sample1.txt new file mode 100644 index 0000000..475e9c5 --- /dev/null +++ b/Modules/file_parse/testfiles/sample1.txt @@ -0,0 +1,2 @@ +usr,1234 +Hello World diff --git a/__init__.py b/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/creds.txt b/creds.txt new file mode 100644 index 0000000..419b521 --- /dev/null +++ b/creds.txt @@ -0,0 +1 @@ +This is a triumph diff --git a/file_parse.py b/file_parse.py new file mode 100644 index 0000000..02742be --- /dev/null +++ b/file_parse.py @@ -0,0 +1,39 @@ + +import sys + +#methods for obtaining credentials from a sample file. +#The paramemter passed is the file itself once opened. +#these methods are based on the sample of how the credentials will be sent remotely. + +def obt_login(text): + first_line = text[0] + line_list = first_line.split(',') +#indentation can cause an error + return line_list[0].replace(" ", "") + +def obt_pass(text): + first_line = text[0] + line_list = first_line.split(',') + return line_list[1].replace(" ", "") + +def obt_message(text): + mess_line = text[1] + return mess_line + + +##file concatination: +#file = open('testfiles/'+sys.argv[1]) +#text = file.readlines() +# +##Printing the test file credentials +# +#usr_login = obt_login(text) +#print(usr_login) +# +#usr_pass = obt_pass(text) +#print(usr_pass) +# +#usr_mess = obt_message(text) +#print(usr_mess) +# +# diff --git a/file_parse.pyc b/file_parse.pyc new file mode 100644 index 0000000..5841755 Binary files /dev/null and b/file_parse.pyc differ diff --git a/sric_script.py b/sric_script.py index 93cb81a..f86330c 100644 --- a/sric_script.py +++ b/sric_script.py @@ -1,55 +1,63 @@ import subprocess -from Modules.file_parse.__file_parse__ import obt_login -from Modules.file_parse.__file_parse__ import obt_pass -from Modules.file_parse.__file_parse__ import obt_message +from ftplib import FTP # I'd rather use ftputil, since it's a nice wrapper for ftplib, but it might make things slower? +from file_parse import obt_login +from file_parse import obt_pass +from file_parse import obt_message # The IP of the server we're downloading/uploading things to/from -server = 192.168.1.1 +ftp_addr = '192.168.1.200' # The names of the files were dealing with -creds = 'credentials.txt' -upload = 'upload-package.txt' +download = 'team1.txt' +upload = open('upload_package.txt', 'r') +creds_file = open('credentials.txt', 'wb') -# Download creds -down_user = 'tmp' -down_pass = 'tmp' +# Where we want to put the file +upload_name = 'CNU/IMPRINT_upload_package.txt' +team_dir = '/home/austin' -# Upload Creds -up_user = 'tmp' -up_pass = 'tmp' -up_mess = 'tmp' +# Set up the FTP object +FTP.set_debuglevel(2) # Loop forever! while True: - # Verify connection - output = subprocess.check_output(['ping', '-n', '1', server]) + # Verify connection before attempting to create FTP instance so we don't have to wait for 30 second timeout + # Does ftplib automatically check connections? If so we should remove this whole block and set ftplib's timeout to like, 1s + output = subprocess.check_output(['ping', '-n', '1', server]) # Need to fix this command to work on linux.... if output = 'tmp string for network down': continue + + try: + ftp = FTP(ftp_addr, timeout = 30) + except ftplib.all_errors, e: + print "FTP ERROR:", e + continue + + # Connect to the FTP object and Login anonymously + ftp = FTP(ftp_addr) + ftp.login() - # Using SFPT because I'm not sure if we can use SCP - with pysftp.Connection('http://' + server, username = down_user, password = down_pass) as sftp: - # Get the download payload - sftp.get(creds, '/creds.txt') - - # Open download file - cred_file = open(creds) + # Change directory if needed + if team_dir: + ftp.cwd(team_dir) + + # Need to do more testing with this line, we might be able to just use storelines or retrlines... +# ftp.retrbinary('RETR', creds_file.write) + ftp.retrlines('RETR ' + download, lambda s, w = creds_file.write: w(s + '\n')) # Parse it mytext reads all lines of the file contents - # up_usr and up_pass defined in file contents - text = cred_file.readlines() - - up_user = obt_login(text) - up_pass = obt_pass(text) - up_mess = obt_message(text) + text = creds_file.readlines() - with pysftp.Connection(server, username = up_user, password = up_pass) as sftp: - # Put the upload payload - sftp.put(uplpad) + # Login with the new creds and put the file + ftp.quit() + ftp = FTP(ftp_addr, obt_login(text), obt_pass(text), timeout = 30) + ftp.storlines('STOR ', upload_name, upload) - # Close cred_file (After the put since we need to get that done as fast as possible) - cred_file.close() # End this script to save resources for other things + ftp.quit() + cred_file.close() + upload.close() break - # Not currently connected... Time to try again +# Not currently connected... Time to try again diff --git a/upload_package.txt b/upload_package.txt new file mode 100644 index 0000000..27efb2e --- /dev/null +++ b/upload_package.txt @@ -0,0 +1 @@ +CNU IMPRINT UAS - Upload Task SRIC