Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/bin/bash
- # Take one arg, and int, return 1 if variable is an INT or 0 if not
- function ft_is_int()
- {
- # Check if value is numeric
- if [ "$1" -eq "$1" ] 2>/dev/null
- then
- return 0
- else
- echo -e "Value '`echo $1`' isn't numeric"
- return 1
- fi
- }
- # Default conf
- default_port=21
- usage="$(basename "$0") [-h] [-u username -i '127.0.0.1' -p '23' -s 'password'] -- send file from ftp protocol
- where:
- -h show this help text
- -u username for ftp server
- -i ipv4 address of remote ftp server
- -f file to transfert to remote ftp server
- -w password for ftp server
- -p special ssh port"
- # Check arguments
- while getopts ":u:i:s:hf:p:" opt; do
- case $opt in
- h)
- echo "$usage"
- exit 0
- ;;
- f)
- filename=$OPTARG
- if [ ! -f ${filename} ]
- then
- echo "Invalid filename given, abort"
- exit 1
- fi
- ;;
- s)
- password=$OPTARG
- ;;
- u)
- username=$OPTARG
- ;;
- i)
- ipv4=$OPTARG
- ;;
- p)
- port=$OPTARG
- if [ ${#port} -eq 0 ]
- then
- port=${default_port}
- elif ! ft_is_int $port
- then
- exit 1
- fi
- ;;
- \?)
- echo "Invalid option: -$OPTARG" >&2
- exit 1
- ;;
- :)
- echo "Option -$OPTARG requires an argument." >&2
- exit 1
- ;;
- esac
- done
- # Check if passwrd wat provide
- if [ ${#password} -eq 0 ]
- then
- echo "No password enter, usage: -p password"
- exit 1
- fi
- # Check if User wat provide
- if [ ${#username} -eq 0 ]
- then
- echo "No user enter, usage: -u username"
- exit 1
- fi
- # Check if a filename have been provided
- if [ ${#filename} -eq 0 ]
- then
- echo "No filename given, usage: -f <filename>"
- exit 1
- fi
- # Check if IPv4 was provide
- if [ ${#ipv4} -eq 0 ]
- then
- echo "No IPv4 enter, usage: -i 42.42.42.42"
- exit 1
- fi
- # Check if port is reachable
- reach=$(nc -zv ${ipv4} ${port} | grep 'Connection refused')
- if [ ${#reach} -ne 0 ]
- then
- echo "Can't reach host ${ipv4}, abort"
- exit 1
- fi
- ftp -n -v ${ipv4} ${port} <<SCRIPT
- ascii
- user ${username} ${password}
- put ${filename}
- quit
- SCRIPT
- echo "Your file is now on ftp server.
- Bye."
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement