martes, 6 de junio de 2017

scripting your .ssh/config with scheme

Generating config files via programs is an old trick which I'm not going to rediscover now. But this one is my first scheme script I wrote for that purpose.

It's about generating a config file you ought to put in your ~/.ssh/config , so that there are some sane defaults when you are ssh-ing to your servers.



It's using the amazon cli interface to fetch the instances of your aws infrastructure. Then, concatenating it with a list of custome servers, and that's basically it (I'm using chicken scheme here, with the regex module which you can get via chicken-install):

;; sudo chicken-install shell
;; csi -ss ~/bin/generate-ssh-config-file.scm >~/.ssh/config
(use shell)
(define servers
(string-split
(capture "aws ec2 describe-instances --output text | grep TAGS | grep Name | cut -f3")))
(define special-servers
'(("user1" "host" "hostname")))
(define (print-formatted server)
(let ((user (car server))
(host (cadr server))
(hostname (caddr server)))
(print "host " host)
(print "hostname " hostname)
(print "user " user)))
(define (main args)
(map print-formatted
(append
(map (lambda (server)
(list "rgrau" server server))
servers)
special-servers)))
With just this, you can already ssh to the servers and have your username filled in, or have your aliases in there.

Also, as I'm super super lazy, I also have a readily available command to prompt me for the server I want to ssh to, and make it ssh there. Quite simple stuff, but hey, it works :)

urxvt -e ssh $(grep 'host ' .ssh/config | awk '{print $2}' | dmenu -l 10)

No hay comentarios: