Chinaunix

标题: ssh和scp命令在php执行环境下失效? [打印本页]

作者: bikkuri    时间: 2014-10-18 19:04
标题: ssh和scp命令在php执行环境下失效?
大家好!
我有一个问题向大家请教。
我写了一个shell脚本,含有大量的ssh和scp命令。
该脚本在Linux命令行下执行正常。
然后我给这个脚本用PHP做了一个界面。
在这个界面上可以选择脚本的调用参数,然后php脚本用这些参数来执行shell脚本。
但是我发现php执行shell脚本的时候,所有的ssh和scp命令都失效了,而其他命令包括连接远程mysql数据库的命令执行都正常。
比如这样的命令:
ssh -i $key root@$mysql_ip "ls -l /tmp"
如果在命令行下执行可以得到正确的输出。
但是通过php脚本执行shell脚本就没有任何输出。
我知道php脚本执行shell脚本的时候所用的帐号不是root而是apache,但是ssh -i $key root@$mysql_ip "ls -l /tmp"这条命令已经指定了远程连接的用户名,按道理来说应该没有问题啊?
到底是哪里出了问题呢?
请大家帮我出出主意。
谢谢。
作者: reb00t    时间: 2014-10-18 19:58
在linux命令行php执行调用shell执行有输出吗?
作者: bikkuri    时间: 2014-10-18 20:19
有输出啊,其他命令都执行正常,就ssh和scp命令失效了一样,没有任何输出。
  1. -rw-rw-rw- 1 test test 3259 Oct 18 18:55 auto.log
  2. -rw-r--r-- 1 test test 2919 Oct 18 17:26 auto.php
  3. -rwxr-xr-x 1 test test 5186 Oct 18 19:05 auto_sql.sh
  4. -rw-r--r-- 1 test test 2173 Oct 18 17:55 do.php
  5. drwxrwxrwx 2 test test 4096 Oct 18 18:25 download
复制代码
原来是auto_sql.sh可以正常执行的,现在加了一个界面auto.php,在界面上可以选择参数,然后用这些参数来调用auto_sql.sh。
例如,我在auto_sql.sh中加入以下代码段:
  1. #-DEBUG
  2. ls -l $key
  3. echo "mysql_ip=$mysql_ip"
  4. echo "ssh -i $key root@$mysql_ip "
  5. ssh -i $key root@$mysql_ip "ls -l /tmp"
  6. echo "<br>"
  7. #-DEBUG
复制代码
php调用执行后得到的输出为:
  1. -rw------- 1 apache apache 1671 Oct 18 06:55 /tmp/yokohama.key
  2. mysql_ip=192.168.122.100
  3. ssh -i /tmp/yokohama.key root@192.168.122.100
复制代码
就是没有ssh命令的输出。
如果直接在命令行输入这条ssh命令是可以得到正确输出的。
  1. [root@hp8 public_html]# ssh -i /tmp/yokohama.key root@192.168.122.100 "ls -l /tmp"
  2. total 516
  3. drwxr-xr-x. 2 root root     17 Oct 14 21:44 hsperfdata_root
  4. -rwx------. 1 root root    663 Oct 14 18:53 ks-script-XFqyVx
  5. -rw-r--r--. 1 root root  13532 Oct 18 14:11 list
  6. -rw-r--r--. 1 root root 505163 Oct 18 18:54 mysql.csv
  7. drwx------. 3 root root     16 Oct 15 01:24 systemd-private-pPEJxZ
  8. -rw-------. 1 root root      0 Oct 14 18:41 yum.log
  9. [root@hp8 public_html]#
复制代码
reb00t 发表于 2014-10-18 19:58
在linux命令行php执行调用shell执行有输出吗?

作者: bikkuri    时间: 2014-10-18 20:41
本帖最后由 bikkuri 于 2014-10-18 23:57 编辑

我做了以下一个实验:

  1. [root@hp8 public_html]# ls -l *test*
  2. -rw-r--r-- 1 root root 88 Oct 18 20:37 php_test.php
  3. -rwxr-xr-x 1 root root 69 Oct 18 20:29 test_ssh.sh
  4. [root@hp8 public_html]# cat test_ssh.sh
  5. #!/bin/sh
  6. echo "Getting list of /tmp on remote server:<br>\n";
  7. ssh -i /tmp/yokohama.key root@192.168.122.100 "ls -l /tmp"
  8. [root@hp8 public_html]# cat php_test.php
  9. <?php
  10. system ("./test_ssh.sh");
  11. ?>
  12. [root@hp8 public_html]# php php_test.php
  13. Getting list of /tmp on remote server:<br>
  14. total 516
  15. drwxr-xr-x. 2 root root     17 Oct 14 21:44 hsperfdata_root
  16. -rwx------. 1 root root    663 Oct 14 18:53 ks-script-XFqyVx
  17. -rw-r--r--. 1 root root  13532 Oct 18 14:11 list
  18. -rw-r--r--. 1 root root 505163 Oct 18 18:54 mysql.csv
  19. drwx------. 3 root root     16 Oct 15 01:24 systemd-private-pPEJxZ
  20. -rw-------. 1 root root      0 Oct 14 18:41 yum.log
  21. [root@hp8 public_html]#
复制代码
先写了这样两个简单的测试脚本,php脚本调用shell脚本用ssh命令远程连接到服务器上列出/tmp目录里的文件清单。
在Linux命令行是可以正确执行并得到输出的。
但是如果通过网页访问php_test.php,就能得到以下一行输出:
  1. [root@hp8 public_html]# lynx -dump http://localhost/~test/php_test.php
  2.    Getting list of /tmp on remote server:


  3. [root@hp8 public_html]#
复制代码
key的权限虽然是600,但是文件的属主是apache,所以apache应该是有权限访问的。何况这个文件本身就是apache创建的。
  1. -rw------- 1 apache apache 1671 Oct 18 18:55 yokohama.key
复制代码
怀疑是不是因为apache用户的shell是/sbin/nologin导致无法登录,但是把apache的shell改成/bin/bash以后再测试结果还是一样。
  1. [root@hp8 public_html]# finger apache
  2. Login: apache                           Name: Apache
  3. Directory: /var/www                     Shell: /sbin/nologin
  4. Never logged in.
  5. No mail.
  6. No Plan.
  7. [root@hp8 public_html]# chsh apache
  8. Changing shell for apache.
  9. New shell [/sbin/nologin]: /bin/bash
  10. Shell changed.
  11. [root@hp8 public_html]# finger apache
  12. Login: apache                           Name: Apache
  13. Directory: /var/www                     Shell: /bin/bash
  14. Never logged in.
  15. No mail.
  16. No Plan.
  17. [root@hp8 public_html]# lynx -dump http://localhost/~test/php_test.php
  18.    Getting list of /tmp on remote server:



  19. [root@hp8 public_html]#
复制代码

作者: bikkuri    时间: 2014-10-19 00:26
终于找到原因了!
原来apache第一次连接远端服务器的时候会被提示接受服务器的公钥,而通过网页访问的情况下无法接受,所以连接失败了。
  1. [root@hp8 public_html]# su - apache
  2. This fortune is encrypted -- get your decoder rings ready!
  3. -bash-4.1$ ssh -i /tmp/yokohama.key root@192.168.122.100 "ls -l /tmp"
  4. The authenticity of host '192.168.122.100 (192.168.122.100)' can't be established.
  5. RSA key fingerprint is 9f:64:7e:b2:13:73:59:c6:60:62:f4:69:75:92:14:0e.
  6. Are you sure you want to continue connecting (yes/no)? yes
  7. Warning: Permanently added '192.168.122.100' (RSA) to the list of known hosts.
  8. total 516
  9. drwxr-xr-x. 2 root root     17 Oct 14 21:44 hsperfdata_root
  10. -rwx------. 1 root root    663 Oct 14 18:53 ks-script-XFqyVx
  11. -rw-r--r--. 1 root root  13532 Oct 18 14:11 list
  12. -rw-r--r--. 1 root root 505163 Oct 18 18:54 mysql.csv
  13. drwx------. 3 root root     16 Oct 15 01:24 systemd-private-pPEJxZ
  14. -rw-------. 1 root root      0 Oct 14 18:41 yum.log
  15. -bash-4.1$ lynx -dump http://localhost/~test/php_test.php
  16.    Getting list of /tmp on remote server:
  17.    total 516 -rw-r--r--. 1 root root 505163 Oct 18 18:54
  18.    mysql.csv drwxr-xr-x. 2 root root 17 Oct 14 21:44
  19.    hsperfdata_root -rwx------. 1 root root 663 Oct 14 18:53
  20.    ks-script-XFqyVx -rw-r--r--. 1 root root 13532 Oct 18 14:11 list
  21.    drwx------. 3 root root 16 Oct 15 01:24 systemd-private-pPEJxZ
  22.    -rw-------. 1 root root 0 Oct 14 18:41 yum.log
  23. -bash-4.1$ exit
  24. -bash-4.1$ exit
  25. logout
  26. [root@hp8 public_html]# chsh apache
  27. Changing shell for apache.
  28. New shell [/bin/bash]: /sbin/nologin
  29. Shell changed.
  30. [root@hp8 public_html]# lynx -dump http://localhost/~test/php_test.php
  31.    Getting list of /tmp on remote server:
  32.    total 516 -rw-r--r--. 1 root root 505163 Oct 18 18:54
  33.    mysql.csv drwxr-xr-x. 2 root root 17 Oct 14 21:44
  34.    hsperfdata_root -rwx------. 1 root root 663 Oct 14 18:53
  35.    ks-script-XFqyVx -rw-r--r--. 1 root root 13532 Oct 18 14:11 list
  36.    drwx------. 3 root root 16 Oct 15 01:24 systemd-private-pPEJxZ
  37.    -rw-------. 1 root root 0 Oct 14 18:41 yum.log
  38. [root@hp8 public_html]#
复制代码
手工接受公钥以后,问题解决。
作者: 李满满    时间: 2014-10-19 11:58
学生党:飘过~,再来一个公钥分发的脚本

  1. $ cat trans_authorized_key.sh
  2. #!/bin/bash
  3. . /etc/init.d/functions
  4. #usage: expect trans_authorized_key.exp <pbkey_name> <rusername> <rpassword> <remoteip>
  5. for remoteip in 172.24.10.{2..5}
  6. do
  7.         >.res
  8.         expect trans_authorized_key.exp ~/.ssh/id_dsa.pub root yinhe@123 ${remoteip} >/dev/null 2>&1
  9.         if [[ $(cat .res) == 0 ]];then
  10.                 action "${remoteip}" /bin/true
  11.         else
  12.                 action "${remoteip}" /bin/false
  13.         fi
  14. done
复制代码

  1. $ cat trans_authorized_key.exp
  2. #!/usr/bin/expect
  3. if { $argc != 4 } {
  4.         send_user "usage expect trans_authorized_key.exp pbkey_name rusername rpassword remoteip\n"
  5.         exit
  6. }
  7. #define var
  8. set pb_key [lindex $argv 0]
  9. set rm_usr [lindex $argv 1]
  10. set rm_pas [lindex $argv 2]
  11. set rm_ips [lindex $argv 3]

  12. #spawn ssh-copy-id -i id_rsa.pub yhsafe@172.24.10.2
  13. spawn ssh-copy-id -i $pb_key $rm_usr@$rm_ips
  14. expect {
  15.          "*expecting."                  {send_user [exec echo "0" >> .res]}
  16.          "yes/no"                       {exp_send "yes\n"                 }
  17.          "*password:"                   {exp_send "$rm_pas\n"             }
  18. }
  19. wait
  20. expect   eof
  21. exit
复制代码
如果要php->apache越权访问任何节点服务器信息那就顺便visodu一下Defaults~即安全又方便~当然节点服务器那就禁止root登录用低权限用户sudo~安全又方便测试




欢迎光临 Chinaunix (http://bbs.chinaunix.net/) Powered by Discuz! X3.2