Convert .ncftp/bookmarks to .netrc

When using the netrw plugin for vim it is recommended to use the .netrc file for managing all ftp logins. Because I used ncftp before this I wanted to easily convert all my stored ftp bookmarks (which are many) into the .netrw format. Here I post my script for this task.

.netrc format

The format is very simple:

    # comment
    machine my_hostname
        login my_username
        password my_password

Also you can define macros, but which aren't related to bookmarks. As you can see, this format doesn't allow much. In theory you couldn't even define an alias, because the hostname must point to some valid host. To help with this you can define your ip to alias mapping inside /etc/hosts. Man Page for netrc

My Program

It reads the .ncftp/bookmarks file, and output the machine with alias, username and password and also outputs the /etc/hosts data. To run it you either redirect the output in a file and copy the content separately into the config files where they are needed.. or you could use this ununderstandable oneliner as root;)

    ./ .ncftp/bookmarks |tee -a $out >(grep -v ".netrc orig host" >> .netrc) | grep ".netrc orig host" >> /etc/hosts

The Code

Because I'm to lazy to upload it I hope it's ok, if I just post it here..


import sys
import csv

if len(sys.argv) < 2:
    print """Usage:
./ bookmarks
bookmarks is the the .ncftp/bookmarks file

bmarks = open(sys.argv[1])
netrcData = ''
hostData = ''
i = 0
for line in bmarks:
    if i<=2:
        # skip first two lines as they contain only metadata
    reader = csv.reader([line], skipinitialspace=True)
    for r in reader:
        alias = r[0]
        host = r[1]
        login = r[2]
        password = r[3][9:].decode("base64").rstrip("")
        directory = r[5]
        port = r[7]
        lastchange = r[8]
        lastip = r[13]
machine %s
    login %s
    password %s""" % (alias, login, password)
        newHostString = "n%s %s" %(lastip, alias)
        newHostString+=" "*(40-len(newHostString))
        newHostString+=" # for .netrc orig host: %s" % (host)

print netrcData
print "   "
print hostData

Vim usage

Because I mentioned vim I also want to show you shortly how to use it then: First you need the netrw plugin. Then you can open a connection just using the alias.. so it is:

vim ftp://my_alias/