gpt4 book ai didi

php - 从PHP阅读登录前ssh横幅

转载 作者:行者123 更新时间:2023-12-02 14:39:15 24 4
gpt4 key购买 nike

我正在尝试使用libssh2-php库函数从PHP连接到ssh服务器。太好了,但是我不知道如何在PHP中读取登录前的旗帜。 “登录前”横幅是在SSH提示登录之前显示的文本,据我所知我无法从连接对象获取流。

因此,鉴于此代码,是否有任何方法可以在登录之前进行读取?

<?php
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 {

//####### Would really like to get a stream right here to read pre-login banner

// 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);
}
}
}
?>

最佳答案

您可以使用phpseclib, a pure PHP SSH implementation做到这一点。标语应通过执行$ssh->getLastError()(返回字符串)或$ssh->getErrors()(返回字符串数组)来获得。

引用其来源:

// see http://tools.ietf.org/html/rfc4252#section-5.4; only called when the encryption has been activated and when we haven't already logged in
if (($this->bitmap & NET_SSH2_MASK_CONSTRUCTOR) && !($this->bitmap & NET_SSH2_MASK_LOGIN) && ord($payload[0]) == NET_SSH2_MSG_USERAUTH_BANNER) {
$this->_string_shift($payload, 1);
extract(unpack('Nlength', $this->_string_shift($payload, 4)));
$this->errors[] = 'SSH_MSG_USERAUTH_BANNER: ' . utf8_decode($this->_string_shift($payload, $length));
$payload = $this->_get_binary_packet();
}

关于php - 从PHP阅读登录前ssh横幅,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16093790/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com