Commandline File Upload

From Digi Developer

Jump to: navigation, search

Contents

Easier uploading of files from python development machines

No Authentication

Here is a simple shell script to make uploading files from Linux to a ConnectPort x4 gateway easier. This allows automated uploading of files directly to a gateway device without forcing you to use your web browser by hand. This version of the script expects no access controls.

Hopefully you will find it useful.

#/bin/bash
 
PROG=$(basename $0)
#CHANGE THIS TO MATCH YOUR SETUP:
DEFAULT=192.168.1.151
 
if [ -z $2 ]
then
        if [ -z $1 ]
        then
                echo "Usage: $PROG <host> <file>"
                exit
        else
                #assume only the filename was provided.
                HOST=$DEFAULT
                FILE=$1
        fi
else
#use both specified options
HOST=$1
FILE=$2
fi
 
#sends the file to the device
curl $HOST/Forms/python_files_1 \
        -F "currentdirectory=WEB/python"  \
        -F file=@$FILE

With Authentication

If you have setup your connectport with with a username and password use the script below:

#/bin/bash
 
#Login into a connectport and post a file to python folder.
 
PROG=$(basename $0)
#CHANGE THIS TO MATCH YOUR SETUP:
DEFAULT=http://192.168.1.151
 
if [ -z $4 ]
then
        if [ -z $3 ]
        then
                echo "Usage: $PROG <host> <file> <username> <password>"
                exit
        else
                #assume only the filename was provided.
                HOST=$DEFAULT
                FILE=$1
                USERNAME=$2
                PASSWORD=$3
        fi
else
#use both specified options
HOST=$1
FILE=$2
USERNAME=$3
PASSWORD=$4
fi
#Login in on behalf of the user
echo login started
curl -v -c "cookies.txt" \
           -b "C1=Cc423F98b5roy9T" \
           -d "cache=&protocol=file&username=$USERNAME&password=$PASSWORD&btLogin=Login" \
           $HOST/Forms/login_1
echo login finished
#sends the file to the device 
echo upload started
curl -v -b cookies.txt -c cookies.txt $HOST/Forms/python_files_1 -F "currentdirectory=WEB/python" -F file=@$FILE
echo upload finished

Notes

The "currentdirectory=" path cannot be changed to a USB device (e.g. A/). If you do the upload will still succeed to the "WEB/python" directory.

What is cURL?

Curl is a common Linux command line tool for doing URL manipulations and transfers. Invoke 'curl --help' or 'curl --manual' to get basic information about it.

Click this link for a basic cURL tutorial

Personal tools
Wiki Editing