Let's talk about the background of the application. Today, the company asked me to write a segment program and asked me to synchronize the configuration files of the master server to different servers. After studying for a while, the code written is as follows: Part of the configuration file config. inc. php: 'servergroup' = & gt; array ('192. 168.0.240 '= & gt;
Let's talk about the background of the application.
Today, the company asked me to write a segment program and asked me to synchronize the configuration files of the master server to different servers. After a while,
The code written is as follows:
Configuration file config. inc. php:
- 'Servergroup' =>Array(
- '1970. 168.0.240 '=>Array(
- 'User' => 'root ',
- 'Pass' => 'sjgj2508 ',
- 'Path' => '/var/www/html /',
- ),
- '1970. 168.0.244 '=>Array(
- 'User' => 'root ',
- 'Pass' => 'sjgj2508 ',
- 'Path' => '/var/www/html /',
- ),
- ),
The main code of the synchronization program is as follows:
- $ ConfigName = 'config. xml'; // name of the configuration file of the master server
- $ ConfPath = ROOT_PATH. '/_ cfg/config. xml'; // the configuration file path of the master server
- // Slave server host name and other information
- $ ServerGroup = $ this-> conf ['servergroup']; // $ this-> conf [] here; is a built-in array of the Framework. You can obtain the content in the preceding configuration file.
- Foreach($ ServerGroupAs$ K => $ v ){
- // Connect to the slave server
- $ ResConnection = ssh2_connect ($ k );
- // Determine whether the log on to the slave server is successful
- If(Ssh2_auth_password ($ resConnection, $ v ['user'], $ v ['pass']) {
- // Initialize the SFTP Subsystem
- $ ResSFTP = ssh2_sftp ($ resConnection );
- If(! Copy ($ confPath, "ssh2.sftp: // {$ resSFTP} {$ v ['path']} _ inc/class/Algorithm/{$ configName }")){
- Echo 'synchronization configuration file failed ';
- }
- }Else{
- Echo "unable to connect to the slave server {$ k }";
- }
- }
Before testing, make sure that the system has related extensions (such as libssh2 ).
Write a blog for the first time. Write down these items as a backup. Please kindly advise.
In addition, one of the programs is not very understandable.
If (! Copy ($ confPath, "ssh2.sftp: // {$ resSFTP} {$ v ['path']} _ inc/class/Algorithm/{$ configName }")){
Echo 'synchronization configuration file failed ';
}
In this section, ssh2.sftp: // {$ resSFTP} {$ v ['path']} _ inc/class/Algorithm/{$ configName }"
The point is that it is unclear why ssh2.sftp: // {$ resSFTP} should be added. Why? I hope the great gods who read this article can help me solve the problem. Thank you.