examples.texi 4.3 KB

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