php写ssh命令行_php调用外部交互式命令,比如ssh,如何实现?

前提保证安装php的ssh扩展

class ssh {

public function __construct($host = "",$port = "",$key=array())

{

$this->sshHost = !empty($host) ? $host : $this->sshHost;

$this->sshPort = !empty($port) ? $port : $this->sshPort;

if(!empty($key)) $parm = $key;

$this->sshConn = ssh2_connect($this->sshHost,$this->sshPort,$parm);

if(! $this->sshConn)

$this->sshMsg = "Connection failed !";

}

/**

* 通过账号与密码登陆ssh

* @param $user 账号

* @param $pwd 密码

* @return NULL

*/

public function authUserPassport($user,$pwd){

$this->sshUser = !empty($user) ? $user : $this->sshUser ;

$this->sshPwd = !empty($pwd) ? $pwd : $this->sshPwd;

if(! ssh2_auth_password($this->sshConn,$this->sshUser,$this->sshPwd))

$this->sshMsg .= "Authorization failed ! ".PHP_EOL ;

}

/**

* 通过公有key和私有key登陆ssh

* @param $publicKey 公有key

* @param $privateKey 私有key

* @param $user  账号

* @param $pwd  密码

*/

public function authPublicKey($publicKey,$privateKey,$user,$pwd){

$this->sshUser = !empty($user) ? $user : $this->sshUser ;

$this->sshPwd = !empty($pwd) ? $pwd : $this->sshPwd;

if(!ssh2_auth_pubkey_file($this->sshConn,$this->sshUser,$publicKey,$privateKey,$this->sshPwd))

$this->sshMsg = "Public Key Authentication Failed";

}

//调用(通过账号密码登陆)

$ssh = new ssh('IP','端口号');

$ssh->authUserPassport("账号","密码");

//调用(通过账号密码跟key登陆)

//$ssh = new ssh('IP','端口号',array('hostkey'=>'ssh-dss'));

//$ssh->authPublicKey("/tmp/xshell.pub.pub","/tmp/xshell.pub.ppk","账号","密码");

}

我之前专门写了个php通过ssh登陆服务器,向服务器发送指令的类`以上是登陆部分供参考

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值