dongyong3223 2019-07-07 10:53
浏览 228
已采纳

在php .sh文件中执行

i am trying to execute in php one .sh file

this file is located in /home/update.sh

#!/bin/bash

/usr/bin/rsync -zaP '-e ssh -i /home/id_rsa_ws -v -p 6633' --del /var/www root@xxx.xxx.xxx:/var/

execute this file working well:

cd /home/; ./update.sh;

the Problem is: i need to execute this file in php

What i already tried in php:

1 - nothing happens.

    $contents = file_get_contents('/home/distribute.sh');
    echo '<pre>';
    echo shell_exec($contents);

i tried also :

2.

    exec("cd /home/;./update.sh 2>&1",$o,$return);
    print_r($o);
    print_r($return);

    Array
    (
            [0] => OpenSSH_7.6p1 Ubuntu-4ubuntu0.3, OpenSSL 1.0.2n  7 Dec 2017
            [1] => debug1: Reading configuration data /etc/ssh/ssh_config
            [2] => debug1: /etc/ssh/ssh_config line 19: Applying options for *
            [3] => debug1: Connecting to xxx.xxx.xxx.xxx [xxx.xxx.xxx.xxx] port 6633.
            [4] => debug1: Connection established.
            [5] => debug1: identity file /var/www/.ssh/id_rsa_ws type 0
            [6] => debug1: key_load_public: No such file or directory
            [7] => debug1: identity file /var/www/.ssh/id_rsa_ws-cert type -1
            [8] => debug1: Local version string SSH-2.0-OpenSSH_7.6p1 Ubuntu-4ubuntu0.3
            [9] => debug1: Remote protocol version 2.0, remote software version OpenSSH_7.6p1 Ubuntu-4ubuntu0.3
            [10] => debug1: match: OpenSSH_7.6p1 Ubuntu-4ubuntu0.3 pat OpenSSH* compat 0x04000000
            [11] => debug1: Authenticating to xxx.xxx.xxx.xxx:6633 as 'root'
            [12] => debug1: SSH2_MSG_KEXINIT sent
            [13] => debug1: SSH2_MSG_KEXINIT received
            [14] => debug1: kex: algorithm: curve25519-sha256
            [15] => debug1: kex: host key algorithm: ecdsa-sha2-nistp256
            [16] => debug1: kex: server->client cipher: chacha20-poly1305@openssh.com MAC:  compression: none
            [17] => debug1: kex: client->server cipher: chacha20-poly1305@openssh.com MAC:  compression: none
            [18] => debug1: expecting SSH2_MSG_KEX_ECDH_REPLY
            [19] => debug1: Server host key: ecdsa-sha2-nistp256 SHA256:VYnLDKxjyAr9Peqx/Ef03k+99009S6y3OZvRL5euGE8
            [20] => debug1: checking without port identifier
            [21] => debug1: read_passphrase: can't open /dev/tty: No such device or address
            [22] => Host key verification failed.
            [23] => rsync: connection unexpectedly closed (0 bytes received so far) [sender]
            [24] => rsync error: unexplained error (code 255) at io.c(235) [sender=3.1.2]
    )
    255

here are the file in linux:

    cd /home/
    total 12K
    -rwxrwxrwx 1 www-data root  111 Jul  7 12:20 update.sh
    -rw------- 1 www-data root 1.7K Jul  6 02:26 id_rsa_ws
    -rw------- 1 www-data root  393 Jul  6 02:26 id_rsa_ws.pub

Thank you all in advance

  • 写回答

1条回答 默认 最新

  • doufusi2013 2019-07-07 13:31
    关注

    Many thanks to [https://stackoverflow.com/users/1491895/barmar]

    after Barmar recommendation i changed following:

    A. I moved ssh key to /var/www/.ssh

    B. I changed command in .sh file, me add two parameters

        -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null
    
    
        /usr/bin/rsync -zaP '-e ssh -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null -i /var/www/.ssh/id_rsa_ws -v -p 6633' --del /var/www root@xxx.xxx.xxx.xxx:/var/
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 ROS系统搭建请教(跨境电商用途)
  • ¥15 AIC3204的示例代码有吗,想用AIC3204测量血氧,找不到相关的代码。
  • ¥20 CST怎么把天线放在座椅环境中并仿真
  • ¥15 任务A:大数据平台搭建(容器环境)怎么做呢?
  • ¥15 YOLOv8obb获取边框坐标时报错AttributeError: 'NoneType' object has no attribute 'xywhr'
  • ¥15 r语言神经网络自变量重要性分析
  • ¥15 基于双目测规则物体尺寸
  • ¥15 wegame打不开英雄联盟
  • ¥15 公司的电脑,win10系统自带远程协助,访问家里个人电脑,提示出现内部错误,各种常规的设置都已经尝试,感觉公司对此功能进行了限制(我们是集团公司)
  • ¥15 救!ENVI5.6深度学习初始化模型报错怎么办?