1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162
|
@c -*-texinfo-*-
@c This file is part of Guile-SSH Reference Manual.
@c Copyright (C) 2014-2021 Artyom V. Poptsov
@c See the file guile-ssh.texi for copying conditions.
@node Examples
@chapter Examples
There are working examples that come with Guile-SSH. These examples are
normally installed in @file{$prefix/share/guile-ssh/examples} directory:
@table @samp
@item sssh.scm
@itemx ssshd.scm
Guile-SSH client and server example.
@item echo/client.scm
@itemx echo/server.scm
Echo client and server example.
@end table
In addition, the following sections will provide an overview of programming
with Guile-SSH.
@c -----------------------------------------------------------------------------
@section Client
In this example we will connect to a server, open a channel and execute a
command. Then we will read output from the command and close connection to
the server.
@lisp
(use-modules (ssh channel)
(ssh session)
(ssh auth)
(ssh key))
(let ((session (make-session #:user "bob"
#:host "example.com"
#:port 22
#:log-verbosity 'nolog))) ; Be quiet
;; Connect to the server
(connect! session)
;; Perform server authentication
(case (authenticate-server session)
...) ; Check the result
;; Try to authenticate on the server with one of the `userauth-*'
;; procedures. Let's use `userauth-agent!'.
(case (userauth-agent! session)
...) ; Check the result
;; Suppose the authentication went successfully.
;; Now we need to open a channel.
(let ((channel (make-channel session)))
(if (not channel)
...) ; Handle an error
;; Open a session so we will be able to execute a command
;; on the server
(catch 'guile-ssh-error
(lambda () (channel-open-session channel))
(lambda (key . args)
...)) ; Handle an error
;; Execute a command
(channel-request-exec channel "uname")
;; Check the exit status of the command
(or (zero? (channel-get-exit-status channel))
...) ; Handle error
;; Poll the channel for data
(let poll ((ready? #f))
(if ready?
(begin
...) ; Read the output from the command
(poll (char-ready? channel))))
;; Close the channel
(close channel)
;; Disconnect from the server
(disconnect! session)))
@end lisp
@c -----------------------------------------------------------------------------
@section Server
In this example we will create a new server and start the server loop.
@lisp
(use-modules (ssh server)
(ssh message)
(ssh session)
(ssh channel)
(ssh key)
(ssh auth))
(let ((server (make-server #:bindport 22
#:rsakey "/home/alice/.ssh/host_rsa_key"
#:dsakey "/home/alice/.ssh/host_dsa_key"
#:log-verbosity 'nolog))) ; Be quiet
;; Start listen to incoming connections.
(server-listen server)
;; Start the main loop of the server
(while #t
;; Accept new connections from clients. Every connection is
;; handled in its own SSH session.
(let ((session (catch 'guile-ssh-error
(lambda () (server-accept server))
(lambda (key . args)
;; Handle an error
#f))))
(if (not session)
(begin
(sleep 1)
(continue)))
;; Handle server authentication request from a client
(server-handle-key-exchange session)
;; Start the session loop. Handle incoming messages from
;; the client
(let session-loop ((msg (server-message-get session)))
(if (not msg)
...) ; Handle an error
;; Get type of the received message
(let ((msg-type (message-get-type msg)))
;; Handle the message according to the type. Type is a list of
;; symbols where the car is the type and cadr is subtype.
(case (car msg-type)
((request-service)
...) ; Handle service request
((request-auth)
...) ; Handle authentication request
((request-channel-open)
...) ; Handle request
((request-channel)
...))) ; Handle request
(if (connected? session)
(session-loop (server-message-get session))))
(disconnect! session))))
@end lisp
@c Local Variables:
@c TeX-master: "guile-ssh.texi"
@c End:
|