Skip to content

Instantly share code, notes, and snippets.

@artyom-poptsov
Last active October 28, 2015 07:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save artyom-poptsov/8cc1e913cc47d277d65c to your computer and use it in GitHub Desktop.
Save artyom-poptsov/8cc1e913cc47d277d65c to your computer and use it in GitHub Desktop.
rcat -- Remote 'cat'
#!/usr/bin/guile \
-e main -s
!#
;;; rcat.scm -- Remote 'cat'
;; Copyright (C) 2015 Artyom V. Poptsov <poptsov.artyom@gmail.com>
;;
;; This program is free software: you can redistribute it and/or
;; modify it under the terms of the GNU General Public License as
;; published by the Free Software Foundation, either version 3 of the
;; License, or (at your option) any later version.
;;
;; This program is distributed in the hope that it will be useful, but
;; WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
;; General Public License for more details.
;;
;; You should have received a copy of the GNU General Public License
;; along with this program. If not, see <http://www.gnu.org/licenses/>.
;;; Commentary:
;; A program that uses (ssh sftp) module from Guile-SSH to read a
;; remote file and print its content to stdout. See
;; <https://github.com/artyom-poptsov/guile-ssh>
;;
;; NOTE that (ssh sftp) module is not stable yet and the API may
;; change in the future. If you do want to test this program, you
;; should use 'wip-sftp' branch from the repository. 'rcat.scm' is
;; known to work with commit 70a1351 on the branch.
;;; Code:
(use-modules (ice-9 rdelim)
(ice-9 regex)
(ice-9 getopt-long)
;; Guile-SSH modules
(ssh session)
(ssh auth)
(ssh sftp))
(define %remote-regex
(make-regexp "(.*)@([0-9]+\\.[0-9]+\\.[0-9]\\.[0-9]+):(.*)"))
(define (print-help-and-exit)
(display "
Usage: rcat remote-file
Example:
rcat avp@127.0.0.1:/etc/profile
")
(exit 0))
(define (rcat user host filename)
"Print contents of a remote file on a HOST pointed by a FILENAME to
stdout."
(let ((session (make-session #:user user #:host host)))
;; Connect to an SSH server.
(connect! session)
;; Authenticate with an SSH server using a SSH agent.
(userauth-agent! session)
(let ((sftp-session (make-sftp-session session)))
(with-input-from-remote-file sftp-session filename
(lambda ()
(do ((line (read-line) (read-line)))
((eof-object? line))
(write-line line)))))))
(define (main args)
"Entry point."
(let* ((option-spec '((help (single-char #\h) (value #f))))
(options (getopt-long args option-spec))
(help-needed? (option-ref options 'help #f))
(args (option-ref options '() #f)))
(and help-needed?
(print-help-and-exit))
(cond
((regexp-exec %remote-regex (car args)) =>
(lambda (match)
(let ((user (match:substring match 1))
(host (match:substring match 2))
(path (match:substring match 3)))
(rcat user host path))))
(else
(error "Not supported yet. :-/" args)))))
;;; rcat.scm ends here.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment