drxnfdx798517235 2017-05-25 11:25
浏览 84

如何使用我的ec2键scp与PHP

I use a command like this on my Mac to scp to my remote server (in a .sh file—it must be done automatically).

scp -i my_key.pem index.html ubuntu@<ip>:~/public/index.html;

Now I have to do the same thing from a Windows machine and figured I would just write it in php instead of using bash so that the code is the same on Windows and Mac (our app runs a local server too that already uses php scripts, it's complicated). I've looked at a few examples but can't find my exact solution. Also, to be honest I don't know much about private/public keys and I want to be careful.

I've got something like this. I don't know what to do for the key.

$hostname = '21.232.foo.bar'; 
$sourceFile = 'index.html';
$targetFile = '~/public/indexx.html';

// SSH Key File — I'm guessing this should be private, not public
    private $ssh_auth_priv = '~/.ec2/my_key.pem'; 

$connection = ssh2_connect($hostname, 22);

ssh2_scp_send($connection, $sourceFile, $targetFile, 0777);

Also, if I'm trying to do something stupid and there is a much easier way, please let me know. Thanks for your help.

~~ UPDATE

My code now looks as follows and I'm getting an error Warning: ssh2_scp_send(): Failure creating remote file: failed to send file

<html>
 <head>
  <title>PHP Test</title>
 </head>
 <body>
 <?php 

$hostname = '13.124.foo.bar'; 
$sourceFile = 'foo.txt';
$targetFile = '~/public2/foo.txt';

$connection = ssh2_connect($hostname, 22);

ssh2_auth_pubkey_file(
    $connection,
    'ubuntu',
    '~/.ec2/id_rsa.pub',
    '~/.ec2/bh.pem'
);


ssh2_scp_send($connection, $sourceFile, $targetFile, 0644);



 ?> 
 </body>
</html>

That's being hosted on my localhost:8000

  • 写回答

1条回答 默认 最新

  • dongpiaozhao6836 2017-05-25 11:36
    关注

    The general format will be

    1   <?php
    2   $conn = ssh2_connect('example.com', 22);
    3   ssh2_auth_pubkey_file(
    4       $conn,
    5       'username',
    6       '/home/username/.ssh/id_rsa.pub',
    7       '/home/username/.ssh/id_rsa'
    8   );
    9   ......
    

    Use ssh2_scp_send($conn, '/local/filename', '/remote/filename', 0644); to send the file where 0644 are file permissions.

    Check out http://www.patcup.com/php-ssh-authentication-using-a-public-key/ if you want to continue using ssh2 for this type of thing, but it's recommended you use https://github.com/phpseclib/phpseclib.

    评论

报告相同问题?

悬赏问题

  • ¥15 微信会员卡接入微信支付商户号收款
  • ¥15 如何获取烟草零售终端数据
  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?