首先第三方给我了我们 .key 和.pub 两个文件,一个是公钥一个是私钥, 1.在我们连接的时候需要处理下.key 文件,我们需要在liunx上执行命令* ssh-keygen -y -f /root/sftp_jfy_sc.key > /root/sftp_jfy.pub** 2.代码
public function connection(){ //连接 $datatxt=date('Ymd'); $methods = array( 'hostkey'=>'ssh-rsa,ssh-dss', 'client_to_server' => array( 'crypt' => 'aes256-ctr,aes192-ctr,aes128-ctr,aes256-cbc,aes192-cbc,aes128-cbc,3des-cbc,blowfish-cbc', 'comp' => 'none'), 'server_to_client' => array( 'crypt' => 'aes256-ctr,aes192-ctr,aes128-ctr,aes256-cbc,aes192-cbc,aes128-cbc,3des-cbc,blowfish-cbc', 'comp' => 'none') ); $connection = \ssh2_connect('219.141.111.111',988,$methods);// print_r($connection); $re= ssh2_auth_pubkey_file($connection, 'jinfeiying',__DIR__.'/sftp_jfy_sc.pub', __DIR__.'sftp_jfy.key');var_dump($re); //证书认证 if ($re) { //sftp初始化 $sftp = ssh2_sftp($connection); //创建流 //$stream = @fopen("ssh2.sftp://".intval($sftp)."/cbm/out/JFYMerItemData-".$datatxt.".txt",'w'); $stream = @fopen("ssh2.sftp://".intval($sftp)."/cbm/out/JFYMerItemData-".$datatxt.".txt.pgp",'w'); if (!$stream) { echo "Could not open file\n";die; } $data = file_get_contents("Public/pgpfile/JFYMerItemData-".$datatxt.'.txt.pgp');//var_dump($data);print_r("JFYMerItemData-'.$datatxt.'.txt'");die; if ($data == false) { echo "Could not open local file\n";die; } if (fwrite($stream,$data) === false) { echo 'Could not send data from file\n';die; } fclose($stream); echo "Public Key Authentication Successful\n"; }else{ die('Public Key Authentication Failed'); } }3.liunx 命令连接 sftp -oIdentityFile=/root/sftp_jfy.key -oPort=988 jinfeiying@101.231.256.218 (sftp -oIdentityFile=/root/私钥.key -oPort=端口 用户名@ip)
