curlssh

ssh port issue:curl: (6) Couldn't resolve host '127.0.0.1:ssh'


On my Windows XP machine, I setup a WinSSHD server.

This command worked:

$ curl -T test.txt -u hdp:123 sftp://127.0.0.1:22

However, when I change the port 22 to ssh, it failed with the output:

curl: (6) Couldn't resolve host '127.0.0.1:ssh'

Why?


Solution

  • curl used RFC3986 for specification of URLs and there is no capacity in that standard for non-numeric port numbers:

    3.2.3. Port

    The port subcomponent of authority is designated by an optional port number in decimal following the host and delimited from it by a single colon (":") character.

    port = *DIGIT

    It allows for translation of a DNS name, so that you can use localhost rather than 127.0.0.1 for example, but not for the port identifier. It would probably be a relatively minor change to allow curl to look up services externally (such as in /etc/services) but it currently does not do that.


    Alternatively, you could find a separate way to convert ssh into 22 and use that. For example, the getent services ssh command on Linux returns something like:

    ssh     22/tcp
    

    so you could use a command like (split over lines for readability but should be on a single line):

    curl -T test.txt -u hdp:123 sftp://127.0.0.1:$(
        getent services ssh | awk '{gsub("/.*", "", $2); print $2}')
    

    to achieve what you want. The command within $() runs the afore-mentioned getent program to generate the information then awk is used to extract just the 22 bit.

    You could even make this a Bash function with something like:

    port_of() {
        if [[ -z "$1" ]] || [[ -z "$2" ]] ; then
            echo >&2 "Args were: '$1' '$2'"
            echo >&2 "    Usage: port_of DB KEY"
            echo >&2 "       eg: port_of services ssh"
            return 1
        fi
        getent "$1" "$2" | awk '{
            gsub("/.*", "", $2)
            print $2
        }'
    }
    
    # Testing code:
    #     port_of
    #     port_of services
    #     port_of services ssh
    

    Then your use becomes the much simpler:

    curl -T test.txt -u hdp:123 sftp://127.0.0.1:$(port_of services ssh)