免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 6233 | 回复: 5
打印 上一主题 下一主题

[网络相关] ssh和scp命令在php执行环境下失效? [复制链接]

论坛徽章:
18
辰龙
日期:2014-05-21 21:01:4115-16赛季CBA联赛之深圳
日期:2016-12-23 13:51:3815-16赛季CBA联赛之北控
日期:2016-11-28 18:26:3815-16赛季CBA联赛之佛山
日期:2016-11-03 11:18:5815-16赛季CBA联赛之辽宁
日期:2016-07-10 16:09:4115-16赛季CBA联赛之江苏
日期:2016-02-20 23:09:202015亚冠之塔什干棉农
日期:2015-08-17 19:49:492015年亚洲杯之日本
日期:2015-04-30 01:24:342015年亚洲杯之约旦
日期:2015-04-01 00:37:182015年亚洲杯之沙特阿拉伯
日期:2015-03-02 15:55:40处女座
日期:2014-05-25 10:34:0020周年集字徽章-年
日期:2023-04-23 11:17:52
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2014-10-18 19:04 |只看该作者 |倒序浏览
大家好!
我有一个问题向大家请教。
我写了一个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"这条命令已经指定了远程连接的用户名,按道理来说应该没有问题啊?
到底是哪里出了问题呢?
请大家帮我出出主意。
谢谢。

论坛徽章:
14
15-16赛季CBA联赛之辽宁
日期:2019-06-16 15:47:3515-16赛季CBA联赛之广夏
日期:2016-08-13 21:24:352015亚冠之武里南联
日期:2015-07-07 17:37:372015亚冠之萨济拖拉机
日期:2015-07-06 17:07:482015亚冠之全北现代
日期:2015-06-04 13:54:272015亚冠之城南
日期:2015-05-21 15:43:212015年亚洲杯之伊朗
日期:2015-04-25 18:20:362015年亚洲杯之伊朗
日期:2015-04-20 16:06:052015年亚洲杯之科威特
日期:2015-03-07 12:51:26丑牛
日期:2014-12-30 10:26:38申猴
日期:2014-09-28 22:40:18金牛座
日期:2014-09-13 21:12:22
2 [报告]
发表于 2014-10-18 19:58 |只看该作者
在linux命令行php执行调用shell执行有输出吗?

论坛徽章:
18
辰龙
日期:2014-05-21 21:01:4115-16赛季CBA联赛之深圳
日期:2016-12-23 13:51:3815-16赛季CBA联赛之北控
日期:2016-11-28 18:26:3815-16赛季CBA联赛之佛山
日期:2016-11-03 11:18:5815-16赛季CBA联赛之辽宁
日期:2016-07-10 16:09:4115-16赛季CBA联赛之江苏
日期:2016-02-20 23:09:202015亚冠之塔什干棉农
日期:2015-08-17 19:49:492015年亚洲杯之日本
日期:2015-04-30 01:24:342015年亚洲杯之约旦
日期:2015-04-01 00:37:182015年亚洲杯之沙特阿拉伯
日期:2015-03-02 15:55:40处女座
日期:2014-05-25 10:34:0020周年集字徽章-年
日期:2023-04-23 11:17:52
3 [报告]
发表于 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执行有输出吗?

论坛徽章:
18
辰龙
日期:2014-05-21 21:01:4115-16赛季CBA联赛之深圳
日期:2016-12-23 13:51:3815-16赛季CBA联赛之北控
日期:2016-11-28 18:26:3815-16赛季CBA联赛之佛山
日期:2016-11-03 11:18:5815-16赛季CBA联赛之辽宁
日期:2016-07-10 16:09:4115-16赛季CBA联赛之江苏
日期:2016-02-20 23:09:202015亚冠之塔什干棉农
日期:2015-08-17 19:49:492015年亚洲杯之日本
日期:2015-04-30 01:24:342015年亚洲杯之约旦
日期:2015-04-01 00:37:182015年亚洲杯之沙特阿拉伯
日期:2015-03-02 15:55:40处女座
日期:2014-05-25 10:34:0020周年集字徽章-年
日期:2023-04-23 11:17:52
4 [报告]
发表于 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]#
复制代码

论坛徽章:
18
辰龙
日期:2014-05-21 21:01:4115-16赛季CBA联赛之深圳
日期:2016-12-23 13:51:3815-16赛季CBA联赛之北控
日期:2016-11-28 18:26:3815-16赛季CBA联赛之佛山
日期:2016-11-03 11:18:5815-16赛季CBA联赛之辽宁
日期:2016-07-10 16:09:4115-16赛季CBA联赛之江苏
日期:2016-02-20 23:09:202015亚冠之塔什干棉农
日期:2015-08-17 19:49:492015年亚洲杯之日本
日期:2015-04-30 01:24:342015年亚洲杯之约旦
日期:2015-04-01 00:37:182015年亚洲杯之沙特阿拉伯
日期:2015-03-02 15:55:40处女座
日期:2014-05-25 10:34:0020周年集字徽章-年
日期:2023-04-23 11:17:52
5 [报告]
发表于 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]#
复制代码
手工接受公钥以后,问题解决。

论坛徽章:
3
丑牛
日期:2014-09-13 18:19:22摩羯座
日期:2014-10-10 17:43:02水瓶座
日期:2014-10-16 01:00:22
6 [报告]
发表于 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~安全又方便测试
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP