1.首先需要有php_ssh2扩展

安装方法参考我的另一篇文章

2.代码及注释



<?php

$ssh_user='root';//用户名
$ssh_pwd = '12312313';//密码
$ssh_port='22';//端口号
$ssh_host='192.168.31.100';//ip地址

//判断是否成功安装ssh2扩展
if(!function_exists("ssh2_connect")){
            exit('SSH扩展没有安装或者没有安装成功');
        }

//建立ssh2连接
$ssh2 = ssh2_connect($ssh_host, $ssh_port);

if(!$ssh2){
    exit('连接服务器失败');
}else{
    echo '成功连接上了服务器';
}

//连接成功后进行密码验证,没验证无法进行其他操作。
if(!ssh2_auth_password( $ssh2, $ssh_user,  $ssh_pwd )){
    return false;
}
//执行linux命令
ssh2_exec($ssh2,'cd /www;./test.sh nihao');

最后修改:2022 年 07 月 12 日
如果觉得我的文章对你有用,请随意赞赏