<? Php /** Php sftp tutorial Telnet, FTP, SSH, SFTP, SSL (1) ftp protocol Introduction File Transfer Protocol (FTP) is one of the common protocols on the Internet. As with many other communication protocols, FTP communication protocols also adopt the Client/Server architecture. You can use different FTP client programs, The FTP protocol is used to connect to the FTP server. The above command transmission and data transmission for transferring or downloading file FTP are transmitted through different ports. FTP is a specific application of TCP/IP. It works on the Seventh Layer of the OSI model, the fourth layer of the TCP model, that is, the application layer. It uses TCP transmission instead of UDP, In this way, the FTP client will go through a well-known "three-way handshake" process before establishing a connection with the kimono. The significance of this process is that the connection between the client and the server is reliable, It is connection-oriented and provides reliable guarantee for data transmission. (2) ssh protocol Ssh is fully called SecureShell, which can report all transmission data to wake up encryption, so that the "man-in-the-middle" cannot obtain the data we transmit. My colleague, the transmitted data is compressed, which can accelerate the transmission speed. ssh has many functions. It can replace telnet or ftppop and provide a secure channel. The most important part of the SSH protocol framework is the three protocols: * The Transport Layer Protocol supports Server Authentication, data confidentiality, and information integrity; * The User Authentication Protocol provides The client identity Authentication for The server; * The Connection Protocol reuses encrypted information tunnels into several logical channels for higher-level application protocols; Various high-level application protocols can be relatively independent from the SSH basic system, and rely on this basic framework to use SSH security mechanisms through connection protocols. (3) sftp protocol The protocol used for FTP transmission over SSH is SFTP (Secure File Transfer). Both Sftp and Ftp are file transfer protocols. Difference: sftp is the Protocol included in ssh (ssh is the encrypted telnet protocol ), As long as the sshd server is started, it is available, and sftp is highly secure. It does not need to be started on the ftp server. Sftp = ssh + ftp (Secure File Transfer Protocol ). Because ftp is transmitted in plain text, No security. sftp is based on ssh, And the transmitted content is encrypted and secure. Currently, the network is not safe. Previously, all telnet users used ssh2 (SSH1 has been cracked ). Sftp tool and ftp Same method. However, its transmission file is encrypted through ssl, and cannot be cracked even if it is intercepted. Sftp has more functions than ftp, and more file attribute settings. */ // Note that ftp is not verified; Class ftp { // The initial configuration is NULL. Private $ config = NULL; // The connection is NULL. Private $ conn = NULL; Public function init ($ config ){ $ This-> config = $ config; } // Ftp connection Public function connect (){ Return $ this-> conn = ftp_connect ($ this-> config ['host'], $ this-> config ['Port']); } // Transmit the data transport layer protocol to obtain true or false data Public function download ($ remote, $ local, $ mode = 'auto '){ Return $ result = @ ftp_get ($ this-> conn, $ localpath, $ remotepath, $ mode ); } // Data transmission layer protocol, which uploads data true or false Public function upload ($ remote, $ local, $ mode = 'auto '){ Return $ result = @ ftp_put ($ this-> conn, $ localpath, $ remotepath, $ mode ); } // Delete an object Public function remove ($ remote ){ Return $ result = @ ftp_delete ($ this-> conn_id, $ file ); } } // Use $ Config = array ( 'Hostname' => 'localhost ', 'Username' => 'root ', 'Password' => 'root ', 'Port' => 21 ); $ Ftp = new Ftp (); $ Ftp-> connect ($ config ); $ Ftp-> upload ('ftp _ err. log', 'ftp _ upload. log '); $ Ftp-> download ('ftp _ upload. log', 'ftp _ download. log '); /* Write an ssh-based ftp class based on the above three Protocols We know that there are two authentication methods: public key and password; (1) login with a password (2) password-free login, that is, using the public key to log on */ Class sftp { // The initial configuration is NULL. Private $ config = NULL; // The connection is NULL. Private $ conn = NULL; // Whether to log on with the key Private $ use_pubkey_file = false; // Initialization Public function init ($ config ){ $ This-> config = $ config; } // Connect to ssh. There are two ways to connect (1) Use the password // (2) use the key Public function connect (){ $ Methods ['hostkey'] = $ use_pubkey_file? 'Ssh-rsa ': []; $ Con = ssh2_connect ($ this-> config ['host'], $ this-> config ['Port'], $ methods ); // (1) when using the key If ($ use_pubkey_file ){ // User Authentication Protocol $ Rc = ssh2_auth_pubkey_file ( $ Conn, $ This-> config ['user'], $ This-> config ['pubkey _ file'], $ This-> config ['privkey _ file'], $ This-> config ['passphrase']) ); // (2) use the login username and password } Else { $ Rc = ssh2_auth_password ($ conn, $ this-> conf _ ['user'], $ this-> conf _ ['passwd']); } Return $ rc; } // Transmit the data transport layer protocol to obtain data Public function download ($ remote, $ local ){ Return ssh2_scp_recv ($ this-> conn _, $ remote, $ local ); } // Data transmission layer protocol, which writes data to the ftp server Public function upload ($ remote, $ local, $ file_mode = 0664 ){ Return ssh2_scp_send ($ this-> conn _, $ local, $ remote, $ file_mode ); } // Delete an object Public function remove ($ remote ){ $ Sftp = ssh2_sftp ($ this-> conn _); $ Rc = false; If (is_dir ("ssh2.sftp: // {$ sftp}/{$ remote }")){ $ Rc = false; // Delete a folder through ssh $ Rc = ssh2_sftp_rmdir ($ sftp, $ remote ); } Else { // Delete an object $ Rc = ssh2_sftp_unlink ($ sftp, $ remote ); } Return $ rc; } } $ Config = [ "Host" => "192.168.1.1", // ftp address "User" => "***", "Port" => "22 ", "Pubkey_path" => "/root/. ssh/id_rsa.pub", // public key storage address "Privkey_path" => "/root/. ssh/id_rsa", // Private Key storage address ]; $ Handle = new SftpAccess (); $ Handle-> init ($ config ); $ Rc = $ handle-> connect (); $ Handle-> getData (remote, $ local ); |