você está aqui: Home  → Arquivo de Mensagens

Conexões FTP sobre SSH

Colaboração: Marcos Aguinaldo Forquesato

Data de Publicação: 26 de Abril de 1998

Estou anexando a esta mensagem um shell script, usando expect, para viabilizar conexões FTP sobre sessões com SSH.


  Hello SSH users,
  
  In the FAQ of SSH it is mentioned that FTP over SSH can be used.  Sure, but it
  is a little bit unfriendly to use. First you have to establish an SSH
  connection, then the FTP connection over it, etc. To automate this process
  I've written a short Expect-based script named "sshftp". 
  Its usage is simple and looks like this:
  
      :> sshftp taz.apache.org
      This is FTP-over-SSH, 1.0
      Username: rse
      Password:
      Connecting to rse@taz.apache.org:
       + opening SSH background session
       + opening FTP foreground session over SSH
       + switching SSH background session into hangup mode
       + login to FTP foreground session
       + switching to FTP data sessions to passive mode
      FTP over SSH login to rse@taz.apache.org successful.
      ftp> ls
      227 Entering Passive Mode (204,62,130,149,9,249)
      150 Opening ASCII mode data connection for /bin/ls.
      total 206
      -rw-r--r--   1 rse  wheel    165 Aug  8  1997 README
      drwxr-xr-x   3 rse  wheel    512 Jul 11  1997 arc
      drwxr-xr-x   2 rse  wheel    512 Jul 25  1997 bin
      drwxr-xr-x   2 rse  wheel    512 Jul 11  1997 etc
      drwxrwxr-x   2 rse  wheel    512 Dec 22 00:33 mirror
      drwxr-xr-x   4 rse  wheel    512 Nov 22 01:25 public_html
      drwxr-xr-x  12 rse  wheel    512 Aug  9  1997 sw
      drwxr-xr-x   3 rse  wheel    512 Apr 11 05:08 tmp
      226 Transfer complete.
      ftp> bye
      221 Goodbye.
      FTP over SSH connection closed
      rse@en1:/u/rse
      :>
  
  Feel free to use it, because for this I post it here.  I'm not subscribed to
  this list, so please use an additional carbon-copy for me when replying.
  
  Greetings,
                                         Ralf S. Engelschall
                                         rse@engelschall.com
                                         www.engelschall.com
  
  #!/usr/local/bin/expect
  ##
  ##  sshftp -- FTP over SSH
  ##  Copyright (c) 1998 Ralf S. Engelschall, <rse@engelschall.com>
  ##
  ##  Usage: sshftp [<hostname> [<port>]]
  ##
  
  set myname    "FTP-over-SSH"
  set myversion "1.0"
  
  #   configuration
  set shell_prompt "(%|#|\\\$|:>) "
  set ftp_prompt   "(ftp)?>"
  set localport    21021
  
  #   program message
  send_user "This is $myname, $myversion\n"
  
  #   determine parameters
  if $argc<1 {
      send_user "Hostname: "
      expect_user -re "(.*)\n"
      set hostname $expect_out(1,string)
  } else {
      set hostname [lindex $argv 0]
  }
  if $argc<2 {
      set hostport 21
  } else {
      set hostport [lindex $argv 1]
  }
  send_user Üsername: "
  expect_user -re "(.*)\n"
  set username $expect_out(1,string)
  send_user "Password: "
  stty -echo
  expect_user -re "(.*)\n"
  send_user "\n"
  stty echo
  set password $expect_out(1,string)
  
  #   global parameters
  set timeout 60
  log_user 0
  
  send_user "Connecting to $username@$hostname:\n"
  
  #   create SSH layer
  send_user " + opening SSH background session\n"
  eval spawn -noecho ssh -x -l $username -L $localport:$hostname:$hostport $hostname
  set ssh_id $spawn_id
  expect -i $ssh_id -re $shell_prompt
  
  #   create FTP layer
  send_user " + opening FTP foreground session over SSH\n"
  eval spawn -noecho ftp localhost $localport
  set ftp_id $spawn_id
  expect -i $ftp_id -re "Name.*:"
  
  #   logout from interactive SSH terminal session
  #   (SSH automatically waits until the FTP session is closed)
  send_user " + switching SSH background session into hangup mode\n"
  send -i $ssh_id "logout\r"
  
  #   login to FTP session
  send_user " + login to FTP foreground session\n"
  send   -i $ftp_id "$username\r"
  expect -i $ftp_id "sword:"
  send   -i $ftp_id "$password\r"
  
  #   switch to passive connect type
  send_user " + switching to FTP data sessions to passive mode\n"
  expect -i $ftp_id -re $ftp_prompt
  send   -i $ftp_id "passive\r"
  
  #   switch to user interaction
  send_user "FTP over SSH login to $username@$hostname successful."
  expect -i $ftp_id -re $ftp_prompt
  log_user 1
  send -i $ftp_id "\r"
  interact
  
  #   finally exit gracefully
  wait
  send_user "FTP over SSH connection closed\n"


Veja a relação completa dos artigos de Marcos Aguinaldo Forquesato

 

 

Opinião dos Leitores

Seja o primeiro a comentar este artigo
*Nome:
Email:
Me notifique sobre novos comentários nessa página
Oculte meu email
*Texto:
 
  Para publicar seu comentário, digite o código contido na imagem acima
 


Powered by Scriptsmill Comments Script