Register
It is currently Thu Apr 24, 2014 8:02 am

Uploading a file via FTP if host can be contacted


All times are UTC - 6 hours


Post new topic Reply to topic  [ 8 posts ] 
Author Message
 PostPosted: Wed Nov 16, 2011 4:16 pm   

Joined: Wed Nov 16, 2011 4:02 pm
Posts: 3
I am working for the Catholic Education Office in Australia and have recently been having trouble with kids at different schools brining their personal laptops into the school, signing a contract saying they will not access the network (some schools just do not have the capacity to take on any more devices) and doing it anyway.
Being students, the first thing they do is go to youtube and stream video all day, and the second is start all their torrents they have downloaded from home. Don't ask me why, but we are not allowed to block torrenting.

To combat this, I have been writing a bash script for macs that will attempt to contact a host and, if succesfull, write a whole lot of data to a file and upload it to a local server via FTP.

I have finished the main functionality of the script but think I have been staring at it for too long and cannot figure out why I am getting
Code:
./MacPing.sh: line 45: syntax error: unexpected end of file
after executing. I have gone over all the code so many times, tried different tweaks and everything I can think of but cannot get it to work!

Pretty much, I am asking for help as I consider myself a rookie with bash scripting.

Code:
#!/bin/bash

# Script Name: MacPing.sh
# Author: Cameron Exley
# Date: 13.11.2011
# Details: Script will try to ping the (6776) FNP. If it can, it will upload a file to specified server.
#          The point of this script is to ensure students who have been allowed to bring laptops on site do not access
#          the network.

# Set the Varialbles
HOST="6776fnp01.ceoparr.local"                                                                # Host you wish to contact
OWNER="Cameron Exley"                                                   # The name of the student - for identification purposes
WriteFile="~/Sites/written.txt"                                         # Text file you wish text to be written to
FTP="mac01-6776.ceoparr.local"                                                  # The FTP server you wish to upload the output file to
FTPUser="reports"                                                       # The username used to authenticate to the FTP server
FTPPass="not-secure"                                                    # The (insecure, clear text) password for the FTP username
USER=$(whoami)                                                          # Current User


if ping -c 1 $HOST                                                      # Attempt to make contact with specified host
then                                                                    # If Successfull

   MAC=$(ifconfig | grep ether | cut -c 7-23)                           # Dump all MAC addresses into variable "MAC"

   TIME=$(date)                                                         # Dump the time & date into variable "TIME"

   echo "The computer containing the following MAC addresses:           # Write all collected information to specified text file
   $MAC
   accessed the network on this time:
   $TIME
   The owner of this device is: $OWNER
   This script was run under the user: $USER"  > "$WriteFile.txt"

   ftp -n $hostname <<EOF                                               # Create an FTP sesion with the specified host
   quote user $FTPUser                                                  # Use the specified username
   quote pass $FTPPass                                                  # Use the specified password
   binary                                                               # Set binary mode
   put $WriteFile "Reports/$OWNER - $TIME.txt"                          # Transfer the file
   quit                                                                 # Close the FTP shell, killing the session at the same time
   EOF                                                                 

else
   echo "im not accessing the network"
fi
#There will be a repeat function in here for every 5 minutes - Unfinished


Top
 Profile  
 PostPosted: Wed Nov 16, 2011 5:10 pm   

Joined: Mon Mar 02, 2009 3:03 am
Posts: 512
hi,

this is because of the second EOF that must start at first column of script file
Code:
      anycommand <<EOF
   text
   text
EOF
you could use tabulations (only tabulations, no spaces) to push it under "text"s, if you write EOF with a leading dash:
Code:
      anycommand<<-EOF


Top
 Profile  
 PostPosted: Sun Nov 20, 2011 8:40 pm   

Joined: Wed Nov 16, 2011 4:02 pm
Posts: 3
Hi Mate,

Thanks for your help, I have tried both solutions with no success, I am still getting the same error.

Have you got any other tips for me?

Thanks a lot,
Cameron.


Top
 Profile  
 PostPosted: Mon Nov 21, 2011 11:38 am   
Moderator
User avatar

Joined: Wed May 03, 2006 2:05 pm
Posts: 242
Hi Cameron!

If I had to guess, just based on pasting that code into my text editor, I think that the problem is the copious amount of white space after your EOF.

You might also run into issues with the comments there, if you're running the script that way. Since they are between the EOF's, they won't be treated as comments.

The final EOF needs to be all by itself, no spaces between it and the end of the line.

I hope this helps!
-J


Top
 Profile YIM  
 PostPosted: Wed Nov 23, 2011 1:55 am   

Joined: Wed Nov 16, 2011 4:02 pm
Posts: 3
Hi Mate,

I am still having the same error...

The code now looks like this:

Code:
#!/bin/bash

# Script Name: MacPing.sh
# Author: Cameron Exley
# Date: 13.11.2011
# Details: Script will try to ping the Marian College (6776) FNP. If it can, it will upload a file to specified server.
#          The point of this script is to ensure students who have been allowed to bring laptops on site do not access
#          the network.

# Set the Varialbles
HOST="6776fnp01"                                                                # Host you wish to contact
OWNER="Cameron Exley"                                                   # The name of the student - for identification purposes
WriteFile="~/Sites/written.txt"                                         # Text file you wish text to be written to
FTP="mac01-6776.ceoparr.local"                                                  # The FTP server you wish to upload the output file to
FTPUser="reports"                                                       # The username used to authenticate to the FTP server
FTPPass="not-secure"                                                    # The (insecure, clear text) password for the FTP username
USER=$(whoami)                                                          # Current User


if ping -c 1 $HOST                                                      # Attempt to make contact with specified host
then                                                                    # If Successfull

   MAC=$(ifconfig | grep ether | cut -c 7-23)                           # Dump all MAC addresses into variable "MAC"

   TIME=$(date)                                                         # Dump the time & date into variable "TIME"

   echo "The computer containing the following MAC addresses:           # Write all collected information to specified text file
   $MAC
   accessed the network on this time:
   $TIME
   The owner of this device is: $OWNER
   This script was run under the user: $USER" > "$WriteFile.txt"

ftp -n $hostname <<EOF
quote user $FTPUser                                                                             
quote pass $FTPPass                                                                             
binary                                                                               
put $WriteFile "Reports/$OWNER - $TIME.txt"                                             
quit
EOF

else
echo "all safe"
fi


Should I remove all comments from the script?


Top
 Profile  
 PostPosted: Wed Nov 23, 2011 7:26 am   
Moderator
User avatar

Joined: Wed May 03, 2006 2:05 pm
Posts: 242
That's odd... the code runs for me, just cutting and pasting from your code block. I don't think any other comments would be causing a problem (though there is one more contained inside the quotes for your 'echo The computer...' statment).

There is a lot of trailing white space where you removed those comments, after the quote, binary, and put lines. You could try removing that. But really I can't see a reason that you should still be getting the same error.

As long as your first and last EOF's have no trailing white space, and the last one has no leading whitespace, it *should* be fine... Still thinking about it... I'll let you know if I come up with anything.


Top
 Profile YIM  
 PostPosted: Wed Nov 23, 2011 10:06 am   
User avatar

Joined: Wed Jun 08, 2011 8:27 am
Posts: 189
Location: outer Shpongolia
Check carefully your variable names.

----

Code:
#!/bin/bash

# Script Name: MacPing.sh
# Author: Cameron Exley
# Date: 13.11.2011
# Details: Script will try to ping the Marian College (6776) FNP. If it can, it will upload a file to specified server.
#          The point of this script is to ensure students who have been allowed to bring laptops on site do not access
#          the network.

# Set the Varialbles
host=6776fnp01                                         # Host you wish to contact
owner='Cameron Exley'                                  # The name of the student - for identification purposes
WriteFile=~/Sites/written.txt                          # Text file you wish text to be written to
ftp=mac01-6776.ceoparr.local                           # The FTP server you wish to upload the output file to
FTPUser=reports                                        # The username used to authenticate to the FTP server
FTPPass=not-secure                                     # The (insecure, clear text) password for the FTP username
user=$USER                                             # Current User


if ! ping -c1 -w1 "$host" > /dev/null 2>&1; then       # Attempt to make contact with specified host; if not successfull
    echo all safe
    exit
fi

while read -r; do
    if [[ $REPLY = *ether* ]]; then
        mac_addrs+=("${REPLY##*[[:blank:]]}")          # Dump all MAC addresses into array "mac_addrs"
    fi 
done < <(ifconfig -a)

curr_time=$(date)                                      # Dump the time & date into variable "curr_time"

                                                       # Write all collected information to specified text file
printf %s\\n > "$WriteFile" "\
The computer containing the following MAC addresses:
$(IFS=$'\n'; echo "${mac_addrs[*]}")
accessed the network on this time: $curr_time

The owner of this device is: $owner
This script was run under the user: $user"

ftp -n "$ftp" << EOC
quote user $FTPUser
quote pass '$FTPPass'
binary
put $WriteFile "Reports/$owner - $curr_time.txt"
quit
EOC


Not tested.


Last edited by jsz on Wed Nov 23, 2011 10:11 am, edited 1 time in total.

Top
 Profile  
 PostPosted: Wed Nov 23, 2011 10:10 am   
Moderator
User avatar

Joined: Wed May 03, 2006 2:05 pm
Posts: 242
Oh, true! I meant to mention that before, but I corrected it in my test script and forgot about it :)

EDIT: I don't think that explains the unexpected EOF error though. I ran then non-corrected version earlier, and didn't get the EOF error.


Top
 Profile YIM  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 8 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: No registered users and 2 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  


BashScripts | Promote Your Page Too
Powered by phpBB © 2011 phpBB Group
© 2003 - 2011 USA LINUX USERS GROUP