简体   繁体   English

如何将PHP值传递给bash脚本?

[英]How do I pass PHP value to a bash script?

I am trying to get the php session_id with my bash script. 我正在尝试使用bash脚本获取php session_id。

The echo $1 in my bash script works after running the bash script with exec in php. 我的bash脚本中的echo $ 1在php中使用exec运行bash脚本后有效。 But the $1 in my same bash script {echo "delete $1";sleep1;}|telnet localhost 12345 is not getting the value. 但是我的bash脚本{echo“ delete $ 1”; sleep1;} | telnet localhost 12345中的$ 1没有得到值。

.php code .php代码

$output = shell_exec('./test.sh '.escapeshellarg(session_id()));

.sh code .sh代码

!/bin/sh
echo $1
{ echo "delete $1"; sleep 1; } | telnet localhost 12345

Result from $output $ output的结果

fa123a3b435c0534345f453554355dasds2a3dfsf7 NOT_FOUND

If you're trying to pass a command through telnet, you'll want to look at expect instead of sh , doing something like this: 如果您试图通过telnet传递命令,则需要查看expect而不是sh ,执行以下操作:

#!/usr/bin/expect
set sid [lindex $argv 1]
spawn telnet localhost 12345
expect "#"
send "delete $sid"

Or just have PHP send the command. 或者只是让PHP发送命令。

$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
$result = socket_connect($socket, "localhost", 12345);
$cmd = "delete " . session_id();
socket_write($socket, $cmd, strlen($cmd));
while ($out = socket_read($socket, 2048)) {
    echo $out;
}
socket_close($socket);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM