Advertisement
Guest User

Untitled

a guest
Sep 22nd, 2017
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.98 KB | None | 0 0
  1. #!/bin/bash
  2.  
  3. # Take one arg, and int, return 1 if variable is an INT or 0 if not
  4. function ft_is_int()
  5. {
  6. # Check if value is numeric
  7. if [ "$1" -eq "$1" ] 2>/dev/null
  8. then
  9. return 0
  10. else
  11. echo -e "Value '`echo $1`' isn't numeric"
  12. return 1
  13. fi
  14. }
  15.  
  16. # Default conf
  17. default_port=21
  18.  
  19. usage="$(basename "$0") [-h] [-u username -i '127.0.0.1' -p '23' -s 'password'] -- send file from ftp protocol
  20. where:
  21. -h show this help text
  22. -u username for ftp server
  23. -i ipv4 address of remote ftp server
  24. -f file to transfert to remote ftp server
  25. -w password for ftp server
  26. -p special ssh port"
  27.  
  28. # Check arguments
  29. while getopts ":u:i:s:hf:p:" opt; do
  30. case $opt in
  31. h)
  32. echo "$usage"
  33. exit 0
  34. ;;
  35. f)
  36. filename=$OPTARG
  37. if [ ! -f ${filename} ]
  38. then
  39. echo "Invalid filename given, abort"
  40. exit 1
  41. fi
  42. ;;
  43. s)
  44. password=$OPTARG
  45. ;;
  46. u)
  47. username=$OPTARG
  48. ;;
  49. i)
  50. ipv4=$OPTARG
  51. ;;
  52. p)
  53. port=$OPTARG
  54. if [ ${#port} -eq 0 ]
  55. then
  56. port=${default_port}
  57. elif ! ft_is_int $port
  58. then
  59. exit 1
  60. fi
  61. ;;
  62. \?)
  63. echo "Invalid option: -$OPTARG" >&2
  64. exit 1
  65. ;;
  66. :)
  67. echo "Option -$OPTARG requires an argument." >&2
  68. exit 1
  69. ;;
  70. esac
  71. done
  72.  
  73. # Check if passwrd wat provide
  74. if [ ${#password} -eq 0 ]
  75. then
  76. echo "No password enter, usage: -p password"
  77. exit 1
  78. fi
  79.  
  80. # Check if User wat provide
  81. if [ ${#username} -eq 0 ]
  82. then
  83. echo "No user enter, usage: -u username"
  84. exit 1
  85. fi
  86.  
  87. # Check if a filename have been provided
  88. if [ ${#filename} -eq 0 ]
  89. then
  90. echo "No filename given, usage: -f <filename>"
  91. exit 1
  92. fi
  93.  
  94. # Check if IPv4 was provide
  95. if [ ${#ipv4} -eq 0 ]
  96. then
  97. echo "No IPv4 enter, usage: -i 42.42.42.42"
  98. exit 1
  99. fi
  100.  
  101. # Check if port is reachable
  102. reach=$(nc -zv ${ipv4} ${port} | grep 'Connection refused')
  103. if [ ${#reach} -ne 0 ]
  104. then
  105. echo "Can't reach host ${ipv4}, abort"
  106. exit 1
  107. fi
  108.  
  109. ftp -n -v ${ipv4} ${port} <<SCRIPT
  110. ascii
  111. user ${username} ${password}
  112. put ${filename}
  113. quit
  114. SCRIPT
  115.  
  116. echo "Your file is now on ftp server.
  117. Bye."
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement