dongye1934 2019-01-04 11:03
浏览 60

如何通过PHP中的ssh连接到imap

I am trying to communicate with imap via ssh in PHP. The following code demonstrates how I connect to imap via ssh:

$ssh_conn = ssh2_connect($sshIP, $sshPORT);
ssh2_auth_password($ssh_conn, $sshUSERNAME, $sshPASSWORD);
$stream = ssh2_tunnel($c, $imapSERVER, $imapPORT);

And seems everything is ok, I get connected to imap via ssh. No errors, and I have resource in $stream.

Then I try to write LOGIN command to stream, like this:

fwrite($stream, "A1 LOGIN username password
");

Problem is that server doesn't response anything.

I try to get response like this:

while (true) {
     echo fread($stream, 4096);
}

Don't worry about codes for writing and reading data to imap server, this I have written just for example. This is not important here. The problem is that I don't get any response from imap. Always empty string.

I am sure, writing and reading methods work well, because I use big HORDE library https://dev.horde.org/imap_client/, and when I use stream of SOCKS5 or HTTPS - it work well!

Also I tried different SSH servers - not success.

So, where might be a problem ? Also the next information may be useful: I get different information when using stream_get_meta_data() for ssh and socks5 stream.

SSH:

array(8) {
  ["exit_status"]=>
  int(0)
  ["timed_out"]=>
  bool(false)
  ["blocked"]=>
  bool(true)
  ["eof"]=>
  bool(false)
  ["stream_type"]=>
  string(12) "SSH2 Channel"
  ["mode"]=>
  string(2) "r+"
  ["unread_bytes"]=>
  int(0)
  ["seekable"]=>
  bool(false)
}

SOCKS5:

array(8) {
  ["crypto"]=>
  array(4) {
    ["protocol"]=>
    string(7) "TLSv1.2"
    ["cipher_name"]=>
    string(27) "ECDHE-RSA-AES128-GCM-SHA256"
    ["cipher_bits"]=>
    int(128)
    ["cipher_version"]=>
    string(7) "TLSv1.2"
  }
  ["timed_out"]=>
  bool(false)
  ["blocked"]=>
  bool(true)
  ["eof"]=>
  bool(false)
  ["stream_type"]=>
  string(14) "tcp_socket/ssl"
  ["mode"]=>
  string(2) "r+"
  ["unread_bytes"]=>
  int(0)
  ["seekable"]=>
  bool(false)
}

Also I notified important thing, it is working if I use port 143 for imap server. And doesn't work if I use port 993. But 143 is not ssl. Therefore I can't even use LOGIN command. I think cause touches with ssl. But how to set ssl for ssh tunnel..... ?

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥30 这是哪个作者做的宝宝起名网站
    • ¥60 版本过低apk如何修改可以兼容新的安卓系统
    • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
    • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
    • ¥50 有数据,怎么用matlab求全要素生产率
    • ¥15 TI的insta-spin例程
    • ¥15 完成下列问题完成下列问题
    • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
    • ¥15 YoloV5 第三方库的版本对照问题
    • ¥15 请完成下列相关问题!