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命令失效了一样,没有任何输出。
-rw-rw-rw- 1 test test 3259 Oct 18 18:55 auto.log
-rw-r--r-- 1 test test 2919 Oct 18 17:26 auto.php
-rwxr-xr-x 1 test test 5186 Oct 18 19:05 auto_sql.sh
-rw-r--r-- 1 test test 2173 Oct 18 17:55 do.php
drwxrwxrwx 2 test test 4096 Oct 18 18:25 download
复制代码
原来是auto_sql.sh可以正常执行的,现在加了一个界面auto.php,在界面上可以选择参数,然后用这些参数来调用auto_sql.sh。
例如,我在auto_sql.sh中加入以下代码段:
#-DEBUG
ls -l $key
echo "mysql_ip=$mysql_ip"
echo "ssh -i $key root@$mysql_ip "
ssh -i $key root@$mysql_ip "ls -l /tmp"
echo "<br>"
#-DEBUG
复制代码
php调用执行后得到的输出为:
-rw------- 1 apache apache 1671 Oct 18 06:55 /tmp/yokohama.key
mysql_ip=192.168.122.100
ssh -i /tmp/yokohama.key root@192.168.122.100
复制代码
就是没有ssh命令的输出。
如果直接在命令行输入这条ssh命令是可以得到正确输出的。
[root@hp8 public_html]# ssh -i /tmp/yokohama.key root@192.168.122.100 "ls -l /tmp"
total 516
drwxr-xr-x. 2 root root 17 Oct 14 21:44 hsperfdata_root
-rwx------. 1 root root 663 Oct 14 18:53 ks-script-XFqyVx
-rw-r--r--. 1 root root 13532 Oct 18 14:11 list
-rw-r--r--. 1 root root 505163 Oct 18 18:54 mysql.csv
drwx------. 3 root root 16 Oct 15 01:24 systemd-private-pPEJxZ
-rw-------. 1 root root 0 Oct 14 18:41 yum.log
[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 编辑
我做了以下一个实验:
[root@hp8 public_html]# ls -l *test*
-rw-r--r-- 1 root root 88 Oct 18 20:37 php_test.php
-rwxr-xr-x 1 root root 69 Oct 18 20:29 test_ssh.sh
[root@hp8 public_html]# cat test_ssh.sh
#!/bin/sh
echo "Getting list of /tmp on remote server:<br>\n";
ssh -i /tmp/yokohama.key root@192.168.122.100 "ls -l /tmp"
[root@hp8 public_html]# cat php_test.php
<?php
system ("./test_ssh.sh");
?>
[root@hp8 public_html]# php php_test.php
Getting list of /tmp on remote server:<br>
total 516
drwxr-xr-x. 2 root root 17 Oct 14 21:44 hsperfdata_root
-rwx------. 1 root root 663 Oct 14 18:53 ks-script-XFqyVx
-rw-r--r--. 1 root root 13532 Oct 18 14:11 list
-rw-r--r--. 1 root root 505163 Oct 18 18:54 mysql.csv
drwx------. 3 root root 16 Oct 15 01:24 systemd-private-pPEJxZ
-rw-------. 1 root root 0 Oct 14 18:41 yum.log
[root@hp8 public_html]#
复制代码
先写了这样两个简单的测试脚本,php脚本调用shell脚本用ssh命令远程连接到服务器上列出/tmp目录里的文件清单。
在Linux命令行是可以正确执行并得到输出的。
但是如果通过网页访问php_test.php,就能得到以下一行输出:
[root@hp8 public_html]# lynx -dump http://localhost/~test/php_test.php
Getting list of /tmp on remote server:
[root@hp8 public_html]#
复制代码
key的权限虽然是600,但是文件的属主是apache,所以apache应该是有权限访问的。何况这个文件本身就是apache创建的。
-rw------- 1 apache apache 1671 Oct 18 18:55 yokohama.key
复制代码
怀疑是不是因为apache用户的shell是/sbin/nologin导致无法登录,但是把apache的shell改成/bin/bash以后再测试结果还是一样。
[root@hp8 public_html]# finger apache
Login: apache Name: Apache
Directory: /var/www Shell: /sbin/nologin
Never logged in.
No mail.
No Plan.
[root@hp8 public_html]# chsh apache
Changing shell for apache.
New shell [/sbin/nologin]: /bin/bash
Shell changed.
[root@hp8 public_html]# finger apache
Login: apache Name: Apache
Directory: /var/www Shell: /bin/bash
Never logged in.
No mail.
No Plan.
[root@hp8 public_html]# lynx -dump http://localhost/~test/php_test.php
Getting list of /tmp on remote server:
[root@hp8 public_html]#
复制代码
作者:
bikkuri
时间:
2014-10-19 00:26
终于找到原因了!
原来apache第一次连接远端服务器的时候会被提示接受服务器的公钥,而通过网页访问的情况下无法接受,所以连接失败了。
[root@hp8 public_html]# su - apache
This fortune is encrypted -- get your decoder rings ready!
-bash-4.1$ ssh -i /tmp/yokohama.key root@192.168.122.100 "ls -l /tmp"
The authenticity of host '192.168.122.100 (192.168.122.100)' can't be established.
RSA key fingerprint is 9f:64:7e:b2:13:73:59:c6:60:62:f4:69:75:92:14:0e.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '192.168.122.100' (RSA) to the list of known hosts.
total 516
drwxr-xr-x. 2 root root 17 Oct 14 21:44 hsperfdata_root
-rwx------. 1 root root 663 Oct 14 18:53 ks-script-XFqyVx
-rw-r--r--. 1 root root 13532 Oct 18 14:11 list
-rw-r--r--. 1 root root 505163 Oct 18 18:54 mysql.csv
drwx------. 3 root root 16 Oct 15 01:24 systemd-private-pPEJxZ
-rw-------. 1 root root 0 Oct 14 18:41 yum.log
-bash-4.1$ lynx -dump http://localhost/~test/php_test.php
Getting list of /tmp on remote server:
total 516 -rw-r--r--. 1 root root 505163 Oct 18 18:54
mysql.csv drwxr-xr-x. 2 root root 17 Oct 14 21:44
hsperfdata_root -rwx------. 1 root root 663 Oct 14 18:53
ks-script-XFqyVx -rw-r--r--. 1 root root 13532 Oct 18 14:11 list
drwx------. 3 root root 16 Oct 15 01:24 systemd-private-pPEJxZ
-rw-------. 1 root root 0 Oct 14 18:41 yum.log
-bash-4.1$ exit
-bash-4.1$ exit
logout
[root@hp8 public_html]# chsh apache
Changing shell for apache.
New shell [/bin/bash]: /sbin/nologin
Shell changed.
[root@hp8 public_html]# lynx -dump http://localhost/~test/php_test.php
Getting list of /tmp on remote server:
total 516 -rw-r--r--. 1 root root 505163 Oct 18 18:54
mysql.csv drwxr-xr-x. 2 root root 17 Oct 14 21:44
hsperfdata_root -rwx------. 1 root root 663 Oct 14 18:53
ks-script-XFqyVx -rw-r--r--. 1 root root 13532 Oct 18 14:11 list
drwx------. 3 root root 16 Oct 15 01:24 systemd-private-pPEJxZ
-rw-------. 1 root root 0 Oct 14 18:41 yum.log
[root@hp8 public_html]#
复制代码
手工接受公钥以后,问题解决。
作者:
李满满
时间:
2014-10-19 11:58
学生党:飘过~,再来一个公钥分发的脚本
$ cat trans_authorized_key.sh
#!/bin/bash
. /etc/init.d/functions
#usage: expect trans_authorized_key.exp <pbkey_name> <rusername> <rpassword> <remoteip>
for remoteip in 172.24.10.{2..5}
do
>.res
expect trans_authorized_key.exp ~/.ssh/id_dsa.pub root yinhe@123 ${remoteip} >/dev/null 2>&1
if [[ $(cat .res) == 0 ]];then
action "${remoteip}" /bin/true
else
action "${remoteip}" /bin/false
fi
done
复制代码
$ cat trans_authorized_key.exp
#!/usr/bin/expect
if { $argc != 4 } {
send_user "usage expect trans_authorized_key.exp pbkey_name rusername rpassword remoteip\n"
exit
}
#define var
set pb_key [lindex $argv 0]
set rm_usr [lindex $argv 1]
set rm_pas [lindex $argv 2]
set rm_ips [lindex $argv 3]
#spawn ssh-copy-id -i id_rsa.pub yhsafe@172.24.10.2
spawn ssh-copy-id -i $pb_key $rm_usr@$rm_ips
expect {
"*expecting." {send_user [exec echo "0" >> .res]}
"yes/no" {exp_send "yes\n" }
"*password:" {exp_send "$rm_pas\n" }
}
wait
expect eof
exit
复制代码
如果要php->apache越权访问任何节点服务器信息那就顺便visodu一下Defaults~即安全又方便~当然节点服务器那就禁止root登录用低权限用户sudo~安全又方便测试
欢迎光临 Chinaunix (http://bbs.chinaunix.net/)
Powered by Discuz! X3.2