Welcome to the Linux community forum and interact with the 2 million technical staff. Next we will detail the following: the first method is to execute a function or a class for the following code, however, this article only serves to provide you with basic concepts, so you can start with: if (! Function_exists (ssh2_connect) di
Welcome to the Linux community forum and interact with 2 million technical staff> here we will detail the first method: execute the following code to create a function or a class, however, this article only serves to provide you with basic concepts, so you can start with: if (! Function_exists ("ssh2_connect") di
Welcome to the Linux community forum and interact with 2 million technicians>
We will detail the following in detail:
Method 1: Execute
You 'd better create a function or a class for the following code, but this article only serves to provide you with a basic idea, so you can start like this:
if (!function_exists("ssh2_connect")) die("function ssh2_connect doesn't exist") // log in at server1.example.com on port 22 if(!($con = ssh2_connect("server1.example.com", 22))){ echo "fail: unable to establish connection\n"; } else { // try to authenticate with username root, password secretpassword if(!ssh2_auth_password($con, "root", "secretpassword")) { echo "fail: unable to authenticate\n"; } else { // allright, we're in! echo "okay: logged in...\n"; // execute a command if(!($stream = ssh2_exec($con, "ls -al" )) ){ echo "fail: unable to execute command\n"; } else{ // collect returning data from command stream_set_blocking( $stream, true ); $data = ""; while( $buf = fread($stream,4096) ){ $data .= $buf; } fclose($stream); } } |
Method 2: Housing
Similarly, you can write a function or a class for the following code. However, this article only provides the basic concepts:
if (!function_exists("ssh2_connect")) die("function ssh2_connect doesn't exist") // log in at server1.example.com on port 22 if(!($con = ssh2_connect("server1.example.com", 22))){ echo "fail: unable to establish connection\n"; } else { // try to authenticate with username root, password secretpassword if(!ssh2_auth_password($con, "root", "secretpassword")) { echo "fail: unable to authenticate\n"; } else { // allright, we're in! echo "okay: logged in...\n"; // create a shell if(!($shell = ssh2_shell($con, 'vt102', null, 80, 40, SSH2_TERM_UNIT_CHARS))){ echo "fail: unable to establish shell\n"; } else{ stream_set_blocking( $shell, true ); // send a command fwrite($shell,"ls -al\n"); sleep(1); // & collect returning data $data = ""; while( $buf = fread($shell,,4096) ){ $data .= $buf; } fclose($shell); } } } |
[1] [2] [3]