home *** CD-ROM | disk | FTP | other *** search
/ Columbia Kermit / kermit.zip / ckscripts / autossh < prev    next >
Text File  |  2020-01-01  |  6KB  |  155 lines

  1. #!/opt/local/bin/kermit +
  2. ;
  3. ; a u t o s s h
  4. ;
  5. ; Illustration of scripted SSH session using C-Kermit 8.0 or K95 2.0.
  6. ; Also illustrates client/server features such as client query of server
  7. ; variables and sending e-mail containing a script-composed text body
  8. ; through the server.
  9. ;
  10. ; In Unix, adjust the top line to point to your C-Kermit 8.0 executable.
  11. ;
  12. ; F. da Cruz, Columbia University Kermit Project, July 2002.
  13. ;
  14. ; Command-line parameters:
  15. ;  1. Hostname or address
  16. ;  2. Username on host
  17. ;  3. Name of file to send
  18. ;  4. Directory on host in which to store the file
  19. ;  5. Email address for status report.
  20. ;
  21. ; Parameters not supplied are prompted for.
  22. ;
  23. ; The following version-check works with older Kermit versions.
  24. ;
  25. define badversion {
  26.     echo Sorry - C-Kermit 8.0 or K95 2.0 or later required.
  27.     exit 1
  28. }
  29. if not equal "\v(program)" "C-Kermit" badversion
  30. if LLT \v(version) 800200 badversion
  31.  
  32. ; From here down we make free use of C-Kermit 8.0 / K95 2.0 features.
  33.  
  34. define ERRQUIT {            ; Macro to exit appropriately
  35.     if kerbang exit 1 \%1            ; If Kerbang script, exit all the way.
  36.     stop 1 \%1                ; Otherwise return to Kermit prompt.
  37. }
  38. while not def \%1 {            ; If hostname/address not supplied
  39.     ask \%1 { Host: }            ; prompt for one until we get it.
  40.     if > \fsplit(\%1,,,.) 1 {        ; Allow only one "word" here
  41.         echo Just the address please.    ; (no TCP port number).
  42.         undef \%1
  43.     }
  44. }
  45. if not def \%2 {            ; If username not supplied
  46.     ask \%2 { User [\v(user)]: }    ; Prompt for one, but default
  47.     if not def \%2 assign \%2 \v(user)    ; to local user ID.
  48. }                    ; 
  49. ; Note: if you have key files the following step can and should be skipped.
  50.  
  51. if not def \%3 {            ; If password not supplied
  52.     askq \%3 { Password: }        ; Prompt for one.
  53. }
  54.  
  55. ; As a sample, let's suppose we want to access a Unix host, upload a file
  56. ; to a certain directory, and then send mail to somebody when it's uploaded.
  57. ; Accept these items from the command line and prompt for any that are missing.
  58. ;
  59. while not def filename {        ; If filename not supplied
  60.     ask filename { File: }        ; Prompt for one.
  61.     if not def filename continue
  62.     if not readable \m(filename) {
  63.         echo \m(filename): not found or not readable    
  64.         continue
  65.     }
  66. }
  67. .filename := \fcontents(\%3)
  68.  
  69. ; Note: \fcontents() is used to avoid over-evaluating DOS pathnames that
  70. ; contain backslashes.
  71.  
  72. while not def \%4 {                     ; Target directory for uploading
  73.     ask \%4 { Target directory: }
  74. }
  75. .directory := \fcontents(\%4)
  76.  
  77. while not def \%5 {                     ; Email address for upload report
  78.     ask \%5 { Email address for report: }
  79. }
  80. .address := \fcontents(\%5)
  81.  
  82. ;;; log session session.log             ; Uncomment to log the host session
  83. set input echo off                      ; Change to ON to watch the session
  84.  
  85. echo Making SSH connection to \%1 as user \%2...
  86.  
  87. ; Bypass network directory lookup to avoid finding an entry of that name that
  88. ; specifies a different connection method.
  89. ;
  90. set network directory ""
  91.  
  92. ; In Unix, we make the connection through a pseudoterminal to the external SSH
  93. ; client, which prompts you for the host password unless you have the
  94. ; appropriate public and private keys installed for SSH.  The only way to
  95. ; avoid the password prompt is to install the needed keys.
  96. ;
  97. ; If you are running this script in Unix and it fails, it is most likely due
  98. ; to a peculiarity of the underlying PTY driver.  Try changing "set host /pty"
  99. ; to "set host /pipe" below.
  100. ;
  101. if k-95 {                                ; K-95 has SSH built in
  102.     set host /network:ssh /user:\%2 /password:\%3 \%1
  103. } else {                                 ; C-Kermit uses external SSH client
  104.     set host /pty ssh -e none -l \%2 \%1
  105. }
  106. if fail errquit {Can't open SSH connection to \%1.}
  107. echo Connected to \%1.
  108.  
  109. ; At this point we have a connection to the host.  Everything from here
  110. ; here down is specific to the host's prompts, commands, and applications,
  111. ; and of course, what you want to do once you have a connection.
  112. ;
  113. ; In this example we start the host's Kermit server and use it.  The method
  114. ; used to start the server depends on the host allowing typeahead and there
  115. ; being no interactive non-shell prompt prior to login (adjust as necessary,
  116. ; e.g. to respond to prompts and/or wait for a specific shell prompt).  It
  117. ; also assumes a relatively up-to-date C-Kermit version is installed on the
  118. ; host as "kermit" in your PATH.
  119. ;
  120. set xfer display brief                  ; No fancy screen stuff
  121. set exit warning off                    ; Exit without asking
  122. lineout kermit -x                       ; Start Kermit server
  123. input 20 "READY TO SERVE..."            ; Wait for server's ready message
  124. if fail stop 1 Server didn't start      ; Check
  125. remote cd \m(directory)                 ; CD to download directory on server
  126. if fail stop 1 REMOTE CD \m(directory) failure
  127.  
  128. query kermit directory                  ; Get full remote directory path
  129. if success .directory := \v(query)
  130.  
  131. set flag off                            ; For remembering transfer status
  132. send \m(filename)                       ; Upload the file
  133. if success set flag on                  ; Remember file-transfer status
  134.  
  135. ; Send a report by e-mail through the server.  This illustrates not only
  136. ; how to send email but also how to have the message body in memory rather
  137. ; than in a file by creating a small array containing the lines of the
  138. ; message.
  139. ;
  140. declare \&a[4]                          ; Declare a 4-element array
  141. .\&a[1] := File: \fpath(\m(filename))   ; Full pathname of source file
  142. .\&a[2] := Size: \fsize(\m(filename))   ; Size of source file
  143. .\&a[3] := To directory: \m(directory)  ; Target directory
  144. if flag {                               ; Status
  145.     .\&a[4] := Status: Success
  146. } else {
  147.     .\&a[4] := Status: Failure \v(xfermsg)
  148. }
  149. send /mail:\m(address) /array:a /text /subject:"Upload status"
  150.  
  151. ; Shut down the server and close the connection
  152.  
  153. bye
  154. exit \v(status)
  155.