欢迎进入Linux社区论坛,与200万技术人员互动交流 >>进入 下面我们分别详述之: 第一种方法:执行 你最好为下面的代码创建函数或者是一个类,不过本文仅仅起到一个为您提供基本观念的作用,所以说你可以如此开始: if (!function_exists("ssh2_connect")) di
欢迎进入Linux社区论坛,与200万技术人员互动交流 >>进入
下面我们分别详述之:
第一种方法:执行
你最好为下面的代码创建函数或者是一个类,不过本文仅仅起到一个为您提供基本观念的作用,所以说你可以如此开始:
if (!function_exists("ssh2_connect")) die("function ssh2_connect doesn't exist")<br> // log in at server1.example.com on port 22<br> if(!($con = ssh2_connect("server1.example.com", 22))){<br> echo "fail: unable to establish connection\n";<br> } else {<br> // try to authenticate with username root, password secretpassword<br> if(!ssh2_auth_password($con, "root", "secretpassword")) {<br> echo "fail: unable to authenticate\n";<br> } else {<br> // allright, we're in!<br> echo "okay: logged in...\n";<br> // execute a command<br> if(!($stream = ssh2_exec($con, "ls -al" )) ){<br> echo "fail: unable to execute command\n";<br> } else{<br> // collect returning data from command<br> stream_set_blocking( $stream, true );<br> $data = "";<br> while( $buf = fread($stream,4096) ){<br> $data .= $buf;<br> }<br> fclose($stream);<br> }<br> } |
第二种方法:外壳
同样道理,你也可以为如下的代码编写函数或者一个类。不过,本文仅仅提供基本观念:
if (!function_exists("ssh2_connect")) die("function ssh2_connect doesn't exist")<br> // log in at server1.example.com on port 22<br> if(!($con = ssh2_connect("server1.example.com", 22))){<br> echo "fail: unable to establish connection\n";<br> } else {<br> // try to authenticate with username root, password secretpassword<br> if(!ssh2_auth_password($con, "root", "secretpassword")) {<br> echo "fail: unable to authenticate\n";<br> } else {<br> // allright, we're in!<br> echo "okay: logged in...\n";<br> // create a shell<br> if(!($shell = ssh2_shell($con, 'vt102', null, 80, 40, SSH2_TERM_UNIT_CHARS))){<br> echo "fail: unable to establish shell\n";<br> } else{<br> stream_set_blocking( $shell, true );<br> // send a command<br> fwrite($shell,"ls -al\n");<br> sleep(1);<br> // & collect returning data<br> $data = "";<br> while( $buf = fread($shell,,4096) ){<br> $data .= $buf;<br> }<br> fclose($shell);<br> }<br> }<br> } |
[1] [2] [3]