Advertisement
Guest User

Untitled

a guest
Aug 5th, 2017
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.13 KB | None | 0 0
  1. sftp://user@host.net/some/random/path
  2.  
  3. #!/usr/bin/env python
  4.  
  5. import os
  6. from urlparse import urlparse
  7.  
  8. uri = os.environ['NAUTILUS_SCRIPT_CURRENT_URI']
  9. result = urlparse(uri)
  10. user, host = result.netloc.split('@')
  11. path = result.path
  12. print('user=', user)
  13. print('host=', host)
  14. print('path=', path)
  15.  
  16. #!/bin/bash
  17.  
  18. # extract the protocol
  19. proto="$(echo $1 | grep :// | sed -e's,^(.*://).*,1,g')"
  20. # remove the protocol
  21. url="$(echo ${1/$proto/})"
  22. # extract the user (if any)
  23. user="$(echo $url | grep @ | cut -d@ -f1)"
  24. # extract the host
  25. host="$(echo ${url/$user@/} | cut -d/ -f1)"
  26. # by request - try to extract the port
  27. port="$(echo $host | sed -e 's,^.*:,:,g' -e 's,.*:([0-9]*).*,1,g' -e 's,[^0-9],,g')"
  28. # extract the path (if any)
  29. path="$(echo $url | grep / | cut -d/ -f2-)"
  30.  
  31. echo "url: $url"
  32. echo " proto: $proto"
  33. echo " user: $user"
  34. echo " host: $host"
  35. echo " port: $port"
  36. echo " path: $path"
  37.  
  38. url: user@host.net/some/random/path
  39. proto: sftp://
  40. user: user
  41. host: host.net
  42. port:
  43. path: some/random/path
  44.  
  45. #!/bin/bash
  46.  
  47. # extract the protocol
  48. proto="$(echo $1 | grep :// | sed -e's,^(.*://).*,1,g')"
  49.  
  50. # remove the protocol -- updated
  51. url=$(echo $1 | sed -e s,$proto,,g)
  52.  
  53. # extract the user (if any)
  54. user="$(echo $url | grep @ | cut -d@ -f1)"
  55.  
  56. # extract the host -- updated
  57. host=$(echo $url | sed -e s,$user@,,g | cut -d/ -f1)
  58.  
  59. # by request - try to extract the port
  60. port="$(echo $host | sed -e 's,^.*:,:,g' -e 's,.*:([0-9]*).*,1,g' -e 's,[^0-9],,g')"
  61.  
  62. # extract the path (if any)
  63. path="$(echo $url | grep / | cut -d/ -f2-)"
  64.  
  65. # extract the protocol
  66. proto="`echo $DATABASE_URL | grep '://' | sed -e's,^(.*://).*,1,g'`"
  67. # remove the protocol
  68. url=`echo $DATABASE_URL | sed -e s,$proto,,g`
  69.  
  70. # extract the user and password (if any)
  71. userpass="`echo $url | grep @ | cut -d@ -f1`"
  72. pass=`echo $userpass | grep : | cut -d: -f2`
  73. if [ -n "$pass" ]; then
  74. user=`echo $userpass | grep : | cut -d: -f1`
  75. else
  76. user=$userpass
  77. fi
  78.  
  79. # extract the host -- updated
  80. hostport=`echo $url | sed -e s,$userpass@,,g | cut -d/ -f1`
  81. port=`echo $hostport | grep : | cut -d: -f2`
  82. if [ -n "$port" ]; then
  83. host=`echo $hostport | grep : | cut -d: -f1`
  84. else
  85. host=$hostport
  86. fi
  87.  
  88. # extract the path (if any)
  89. path="`echo $url | grep / | cut -d/ -f2-`"
  90.  
  91. if ! echo "$url" | grep -q '^[[:blank:]]*ftp://[[:alnum:]]+:[[:alnum:]]+@[[:alnum:].]+/.*[[:blank:]]*$'; then return 1; fi
  92.  
  93. login=$( echo "$url" | sed 's|[[:blank:]]*ftp://([^:]+):([^@]+)@([^/]+)(/.*)[[:blank:]]*|1|' )
  94. pass=$( echo "$url" | sed 's|[[:blank:]]*ftp://([^:]+):([^@]+)@([^/]+)(/.*)[[:blank:]]*|2|' )
  95. host=$( echo "$url" | sed 's|[[:blank:]]*ftp://([^:]+):([^@]+)@([^/]+)(/.*)[[:blank:]]*|3|' )
  96. dir=$( echo "$url" | sed 's|[[:blank:]]*ftp://([^:]+):([^@]+)@([^/]+)(/.*)[[:blank:]]*|4|' )
  97.  
  98. #!/bin/bash
  99.  
  100. test_ftp_url()
  101. {
  102. local url="$1"
  103.  
  104. if ! echo "$url" | grep -q '^[[:blank:]]*ftp://[[:alnum:]]+:[[:alnum:]]+@[[:alnum:].]+/.*[[:blank:]]*$'; then return 1; fi
  105.  
  106. local login=$( echo "$url" | sed 's|[[:blank:]]*ftp://([^:]+):([^@]+)@([^/]+)(/.*)[[:blank:]]*|1|' )
  107. local pass=$( echo "$url" | sed 's|[[:blank:]]*ftp://([^:]+):([^@]+)@([^/]+)(/.*)[[:blank:]]*|2|' )
  108. local host=$( echo "$url" | sed 's|[[:blank:]]*ftp://([^:]+):([^@]+)@([^/]+)(/.*)[[:blank:]]*|3|' )
  109. local dir=$( echo "$url" | sed 's|[[:blank:]]*ftp://([^:]+):([^@]+)@([^/]+)(/.*)[[:blank:]]*|4|' )
  110.  
  111. exec 3>&2 2>/dev/null
  112. exec 6<>"/dev/tcp/$host/21" || ( exec 2>&3 3>&-; return 2 )
  113.  
  114. echo -e "USER $loginn" >&6; read <&6
  115. if ! echo "$REPLY" | grep -q '^220'; then exec 2>&3 3>&- 6>&-; return 3; fi # 220 vsFTPd 3.0.2+ (ext.1) ready...
  116.  
  117. echo -e "PASS $passn" >&6; read <&6
  118. if ! echo "$REPLY" | grep -q '^331'; then exec 2>&3 3>&- 6>&-; return 4; fi # 331 Please specify the password.
  119.  
  120. echo -e "CWD $dirn" >&6; read <&6
  121. if ! echo "$REPLY" | grep -q '^230'; then exec 2>&3 3>&- 6>&-; return 5; fi # 230 Login successful.
  122.  
  123. echo -e "QUITn" >&6; read <&6
  124. if ! echo "$REPLY" | grep -q '^250'; then exec 2>&3 3>&- 6>&-; return 6; fi # 250 Directory successfully changed.
  125.  
  126. exec 2>&3 3>&- 6>&-
  127. return 0
  128. }
  129.  
  130. test_ftp_url 'ftp://fz223free:fz223free@ftp.zakupki.gov.ru/out/nsi/nsiProtocol/daily'
  131. echo "$?"
  132.  
  133. #!/bin/bash
  134.  
  135. FIELDS=($(echo "sftp://user@host.net/some/random/path"
  136. | awk '{split($0, arr, /[/@:]*/); for (x in arr) { print arr[x] }}'))
  137. proto=${FIELDS[1]}
  138. user=${FIELDS[2]}
  139. host=${FIELDS[3]}
  140. path=$(echo ${FIELDS[@]:3} | sed 's/ ///g')
  141.  
  142. #!/bin/bash
  143.  
  144. FIELDS=($(echo "sftp://user@host.net/some/random/path"
  145. | grep -o "[a-z0-9.-][a-z0-9.-]*" | tr 'n' ' '))
  146. proto=${FIELDS[1]}
  147. user=${FIELDS[2]}
  148. host=${FIELDS[3]}
  149. path=$(echo ${FIELDS[@]:3} | sed 's/ ///g')
  150.  
  151. #!/bin/bash
  152.  
  153. parse_url() {
  154. eval $(echo "$1" | sed -e "s#^((.*)://)?(([^:@]*)(:(.*))?@)?([^/?]*)(/(.*))?#${PREFIX:-URL_}SCHEME='2' ${PREFIX:-URL_}USER='4' ${PREFIX:-URL_}PASSWORD='6' ${PREFIX:-URL_}HOST='7' ${PREFIX:-URL_}PATH='9'#")
  155. }
  156.  
  157. URL=${1:-"http://user:pass@example.com/path/somewhere"}
  158. PREFIX="URL_" parse_url "$URL"
  159. echo "$URL_SCHEME://$URL_USER:$URL_PASSWORD@$URL_HOST/$URL_PATH"
  160.  
  161. #!/bin/bash
  162.  
  163. PROJECT_URL="git@github.com:heremaps/here-aaa-java-sdk.git"
  164.  
  165. # Extract the protocol (includes trailing "://").
  166. PARSED_PROTO="$(echo $PROJECT_URL | sed -nr 's,^(.*://).*,1,p')"
  167.  
  168. # Remove the protocol from the URL.
  169. PARSED_URL="$(echo ${PROJECT_URL/$PARSED_PROTO/})"
  170.  
  171. # Extract the user (includes trailing "@").
  172. PARSED_USER="$(echo $PARSED_URL | sed -nr 's,^(.*@).*,1,p')"
  173.  
  174. # Remove the user from the URL.
  175. PARSED_URL="$(echo ${PARSED_URL/$PARSED_USER/})"
  176.  
  177. # Extract the port (includes leading ":").
  178. PARSED_PORT="$(echo $PARSED_URL | sed -nr 's,.*(:[0-9]+).*,1,p')"
  179.  
  180. # Remove the port from the URL.
  181. PARSED_URL="$(echo ${PARSED_URL/$PARSED_PORT/})"
  182.  
  183. # Extract the path (includes leading "/" or ":").
  184. PARSED_PATH="$(echo $PARSED_URL | sed -nr 's,[^/:]*([/:].*),1,p')"
  185.  
  186. # Remove the path from the URL.
  187. PARSED_HOST="$(echo ${PARSED_URL/$PARSED_PATH/})"
  188.  
  189. echo "proto: $PARSED_PROTO"
  190. echo "user: $PARSED_USER"
  191. echo "host: $PARSED_HOST"
  192. echo "port: $PARSED_PORT"
  193. echo "path: $PARSED_PATH"
  194.  
  195. proto:
  196. user: git@
  197. host: github.com
  198. port:
  199. path: :heremaps/here-aaa-java-sdk.git
  200.  
  201. proto: ssh://
  202. user: sschuberth@
  203. host: git.eclipse.org
  204. port: :29418
  205. path: /jgit/jgit
  206.  
  207. #!/bin/bash
  208.  
  209. parse_url() {
  210. local query1 query2 path1 path2
  211.  
  212. # extract the protocol
  213. proto="$(echo $1 | grep :// | sed -e's,^(.*://).*,1,g')"
  214.  
  215. if [[ ! -z $proto ]] ; then
  216. # remove the protocol
  217. url="$(echo ${1/$proto/})"
  218.  
  219. # extract the user (if any)
  220. login="$(echo $url | grep @ | cut -d@ -f1)"
  221.  
  222. # extract the host
  223. host="$(echo ${url/$login@/} | cut -d/ -f1)"
  224.  
  225. # by request - try to extract the port
  226. port="$(echo $host | sed -e 's,^.*:,:,g' -e 's,.*:([0-9]*).*,1,g' -e 's,[^0-9],,g')"
  227.  
  228. # extract the uri (if any)
  229. resource="/$(echo $url | grep / | cut -d/ -f2-)"
  230. else
  231. url=""
  232. login=""
  233. host=""
  234. port=""
  235. resource=$1
  236. fi
  237.  
  238. # extract the path (if any)
  239. path1="$(echo $resource | grep ? | cut -d? -f1 )"
  240. path2="$(echo $resource | grep # | cut -d# -f1 )"
  241. path=$path1
  242. if [[ -z $path ]] ; then path=$path2 ; fi
  243. if [[ -z $path ]] ; then path=$resource ; fi
  244.  
  245. # extract the query (if any)
  246. query1="$(echo $resource | grep ? | cut -d? -f2-)"
  247. query2="$(echo $query1 | grep # | cut -d# -f1 )"
  248. query=$query2
  249. if [[ -z $query ]] ; then query=$query1 ; fi
  250.  
  251. # extract the fragment (if any)
  252. fragment="$(echo $resource | grep # | cut -d# -f2 )"
  253.  
  254. echo "url: $url"
  255. echo " proto: $proto"
  256. echo " login: $login"
  257. echo " host: $host"
  258. echo " port: $port"
  259. echo "resource: $resource"
  260. echo " path: $path"
  261. echo " query: $query"
  262. echo "fragment: $fragment"
  263. echo ""
  264. }
  265.  
  266. parse_url "http://login:password@example.com:8080/one/more/dir/file.exe?a=sth&b=sth#anchor_fragment"
  267. parse_url "https://example.com/one/more/dir/file.exe#anchor_fragment"
  268. parse_url "http://login:password@example.com:8080/one/more/dir/file.exe#anchor_fragment"
  269. parse_url "ftp://user@example.com:8080/one/more/dir/file.exe?a=sth&b=sth"
  270. parse_url "/one/more/dir/file.exe"
  271. parse_url "file.exe"
  272. parse_url "file.exe#anchor"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement