Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- sftp://user@host.net/some/random/path
- #!/usr/bin/env python
- import os
- from urlparse import urlparse
- uri = os.environ['NAUTILUS_SCRIPT_CURRENT_URI']
- result = urlparse(uri)
- user, host = result.netloc.split('@')
- path = result.path
- print('user=', user)
- print('host=', host)
- print('path=', path)
- #!/bin/bash
- # extract the protocol
- proto="$(echo $1 | grep :// | sed -e's,^(.*://).*,1,g')"
- # remove the protocol
- url="$(echo ${1/$proto/})"
- # extract the user (if any)
- user="$(echo $url | grep @ | cut -d@ -f1)"
- # extract the host
- host="$(echo ${url/$user@/} | cut -d/ -f1)"
- # by request - try to extract the port
- port="$(echo $host | sed -e 's,^.*:,:,g' -e 's,.*:([0-9]*).*,1,g' -e 's,[^0-9],,g')"
- # extract the path (if any)
- path="$(echo $url | grep / | cut -d/ -f2-)"
- echo "url: $url"
- echo " proto: $proto"
- echo " user: $user"
- echo " host: $host"
- echo " port: $port"
- echo " path: $path"
- url: user@host.net/some/random/path
- proto: sftp://
- user: user
- host: host.net
- port:
- path: some/random/path
- #!/bin/bash
- # extract the protocol
- proto="$(echo $1 | grep :// | sed -e's,^(.*://).*,1,g')"
- # remove the protocol -- updated
- url=$(echo $1 | sed -e s,$proto,,g)
- # extract the user (if any)
- user="$(echo $url | grep @ | cut -d@ -f1)"
- # extract the host -- updated
- host=$(echo $url | sed -e s,$user@,,g | cut -d/ -f1)
- # by request - try to extract the port
- port="$(echo $host | sed -e 's,^.*:,:,g' -e 's,.*:([0-9]*).*,1,g' -e 's,[^0-9],,g')"
- # extract the path (if any)
- path="$(echo $url | grep / | cut -d/ -f2-)"
- # extract the protocol
- proto="`echo $DATABASE_URL | grep '://' | sed -e's,^(.*://).*,1,g'`"
- # remove the protocol
- url=`echo $DATABASE_URL | sed -e s,$proto,,g`
- # extract the user and password (if any)
- userpass="`echo $url | grep @ | cut -d@ -f1`"
- pass=`echo $userpass | grep : | cut -d: -f2`
- if [ -n "$pass" ]; then
- user=`echo $userpass | grep : | cut -d: -f1`
- else
- user=$userpass
- fi
- # extract the host -- updated
- hostport=`echo $url | sed -e s,$userpass@,,g | cut -d/ -f1`
- port=`echo $hostport | grep : | cut -d: -f2`
- if [ -n "$port" ]; then
- host=`echo $hostport | grep : | cut -d: -f1`
- else
- host=$hostport
- fi
- # extract the path (if any)
- path="`echo $url | grep / | cut -d/ -f2-`"
- if ! echo "$url" | grep -q '^[[:blank:]]*ftp://[[:alnum:]]+:[[:alnum:]]+@[[:alnum:].]+/.*[[:blank:]]*$'; then return 1; fi
- login=$( echo "$url" | sed 's|[[:blank:]]*ftp://([^:]+):([^@]+)@([^/]+)(/.*)[[:blank:]]*|1|' )
- pass=$( echo "$url" | sed 's|[[:blank:]]*ftp://([^:]+):([^@]+)@([^/]+)(/.*)[[:blank:]]*|2|' )
- host=$( echo "$url" | sed 's|[[:blank:]]*ftp://([^:]+):([^@]+)@([^/]+)(/.*)[[:blank:]]*|3|' )
- dir=$( echo "$url" | sed 's|[[:blank:]]*ftp://([^:]+):([^@]+)@([^/]+)(/.*)[[:blank:]]*|4|' )
- #!/bin/bash
- test_ftp_url()
- {
- local url="$1"
- if ! echo "$url" | grep -q '^[[:blank:]]*ftp://[[:alnum:]]+:[[:alnum:]]+@[[:alnum:].]+/.*[[:blank:]]*$'; then return 1; fi
- local login=$( echo "$url" | sed 's|[[:blank:]]*ftp://([^:]+):([^@]+)@([^/]+)(/.*)[[:blank:]]*|1|' )
- local pass=$( echo "$url" | sed 's|[[:blank:]]*ftp://([^:]+):([^@]+)@([^/]+)(/.*)[[:blank:]]*|2|' )
- local host=$( echo "$url" | sed 's|[[:blank:]]*ftp://([^:]+):([^@]+)@([^/]+)(/.*)[[:blank:]]*|3|' )
- local dir=$( echo "$url" | sed 's|[[:blank:]]*ftp://([^:]+):([^@]+)@([^/]+)(/.*)[[:blank:]]*|4|' )
- exec 3>&2 2>/dev/null
- exec 6<>"/dev/tcp/$host/21" || ( exec 2>&3 3>&-; return 2 )
- echo -e "USER $loginn" >&6; read <&6
- if ! echo "$REPLY" | grep -q '^220'; then exec 2>&3 3>&- 6>&-; return 3; fi # 220 vsFTPd 3.0.2+ (ext.1) ready...
- echo -e "PASS $passn" >&6; read <&6
- if ! echo "$REPLY" | grep -q '^331'; then exec 2>&3 3>&- 6>&-; return 4; fi # 331 Please specify the password.
- echo -e "CWD $dirn" >&6; read <&6
- if ! echo "$REPLY" | grep -q '^230'; then exec 2>&3 3>&- 6>&-; return 5; fi # 230 Login successful.
- echo -e "QUITn" >&6; read <&6
- if ! echo "$REPLY" | grep -q '^250'; then exec 2>&3 3>&- 6>&-; return 6; fi # 250 Directory successfully changed.
- exec 2>&3 3>&- 6>&-
- return 0
- }
- test_ftp_url 'ftp://fz223free:fz223free@ftp.zakupki.gov.ru/out/nsi/nsiProtocol/daily'
- echo "$?"
- #!/bin/bash
- FIELDS=($(echo "sftp://user@host.net/some/random/path"
- | awk '{split($0, arr, /[/@:]*/); for (x in arr) { print arr[x] }}'))
- proto=${FIELDS[1]}
- user=${FIELDS[2]}
- host=${FIELDS[3]}
- path=$(echo ${FIELDS[@]:3} | sed 's/ ///g')
- #!/bin/bash
- FIELDS=($(echo "sftp://user@host.net/some/random/path"
- | grep -o "[a-z0-9.-][a-z0-9.-]*" | tr 'n' ' '))
- proto=${FIELDS[1]}
- user=${FIELDS[2]}
- host=${FIELDS[3]}
- path=$(echo ${FIELDS[@]:3} | sed 's/ ///g')
- #!/bin/bash
- parse_url() {
- 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'#")
- }
- URL=${1:-"http://user:pass@example.com/path/somewhere"}
- PREFIX="URL_" parse_url "$URL"
- echo "$URL_SCHEME://$URL_USER:$URL_PASSWORD@$URL_HOST/$URL_PATH"
- #!/bin/bash
- PROJECT_URL="git@github.com:heremaps/here-aaa-java-sdk.git"
- # Extract the protocol (includes trailing "://").
- PARSED_PROTO="$(echo $PROJECT_URL | sed -nr 's,^(.*://).*,1,p')"
- # Remove the protocol from the URL.
- PARSED_URL="$(echo ${PROJECT_URL/$PARSED_PROTO/})"
- # Extract the user (includes trailing "@").
- PARSED_USER="$(echo $PARSED_URL | sed -nr 's,^(.*@).*,1,p')"
- # Remove the user from the URL.
- PARSED_URL="$(echo ${PARSED_URL/$PARSED_USER/})"
- # Extract the port (includes leading ":").
- PARSED_PORT="$(echo $PARSED_URL | sed -nr 's,.*(:[0-9]+).*,1,p')"
- # Remove the port from the URL.
- PARSED_URL="$(echo ${PARSED_URL/$PARSED_PORT/})"
- # Extract the path (includes leading "/" or ":").
- PARSED_PATH="$(echo $PARSED_URL | sed -nr 's,[^/:]*([/:].*),1,p')"
- # Remove the path from the URL.
- PARSED_HOST="$(echo ${PARSED_URL/$PARSED_PATH/})"
- echo "proto: $PARSED_PROTO"
- echo "user: $PARSED_USER"
- echo "host: $PARSED_HOST"
- echo "port: $PARSED_PORT"
- echo "path: $PARSED_PATH"
- proto:
- user: git@
- host: github.com
- port:
- path: :heremaps/here-aaa-java-sdk.git
- proto: ssh://
- user: sschuberth@
- host: git.eclipse.org
- port: :29418
- path: /jgit/jgit
- #!/bin/bash
- parse_url() {
- local query1 query2 path1 path2
- # extract the protocol
- proto="$(echo $1 | grep :// | sed -e's,^(.*://).*,1,g')"
- if [[ ! -z $proto ]] ; then
- # remove the protocol
- url="$(echo ${1/$proto/})"
- # extract the user (if any)
- login="$(echo $url | grep @ | cut -d@ -f1)"
- # extract the host
- host="$(echo ${url/$login@/} | cut -d/ -f1)"
- # by request - try to extract the port
- port="$(echo $host | sed -e 's,^.*:,:,g' -e 's,.*:([0-9]*).*,1,g' -e 's,[^0-9],,g')"
- # extract the uri (if any)
- resource="/$(echo $url | grep / | cut -d/ -f2-)"
- else
- url=""
- login=""
- host=""
- port=""
- resource=$1
- fi
- # extract the path (if any)
- path1="$(echo $resource | grep ? | cut -d? -f1 )"
- path2="$(echo $resource | grep # | cut -d# -f1 )"
- path=$path1
- if [[ -z $path ]] ; then path=$path2 ; fi
- if [[ -z $path ]] ; then path=$resource ; fi
- # extract the query (if any)
- query1="$(echo $resource | grep ? | cut -d? -f2-)"
- query2="$(echo $query1 | grep # | cut -d# -f1 )"
- query=$query2
- if [[ -z $query ]] ; then query=$query1 ; fi
- # extract the fragment (if any)
- fragment="$(echo $resource | grep # | cut -d# -f2 )"
- echo "url: $url"
- echo " proto: $proto"
- echo " login: $login"
- echo " host: $host"
- echo " port: $port"
- echo "resource: $resource"
- echo " path: $path"
- echo " query: $query"
- echo "fragment: $fragment"
- echo ""
- }
- parse_url "http://login:password@example.com:8080/one/more/dir/file.exe?a=sth&b=sth#anchor_fragment"
- parse_url "https://example.com/one/more/dir/file.exe#anchor_fragment"
- parse_url "http://login:password@example.com:8080/one/more/dir/file.exe#anchor_fragment"
- parse_url "ftp://user@example.com:8080/one/more/dir/file.exe?a=sth&b=sth"
- parse_url "/one/more/dir/file.exe"
- parse_url "file.exe"
- parse_url "file.exe#anchor"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement