Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
318 views
in Technique[技术] by (71.8m points)

linux - php command works by running it static but not with a var between

So my problem is this. When I try to run the following command nothing works, I get no error message and no output and and the file will not be transferred to the other device (here with the local host address as placeholder).

shell_exec('sshpass -p "'.$password.'" scp -o StrictHostKeyChecking=no /file.txt user@127.0.0.1:/home/user/ 2>&1');

But if I pass the password in plain text in the command instead of using a variable (As shown below), it works as it should???

shell_exec('sshpass -p "Password" scp -o StrictHostKeyChecking=no /file.txt user@127.0.0.1:/home/user/ 2>&1');

What I have already tried:

  • I have tried to enter the password many times and it was always correct because I only had a simple one for testing purposes.
  • And I have already tried first composing the command first, then saving it into another variable and then executing it

I have now tried a few things but somehow none of them works and I don't understand why because in the end these are 1:1 the same commands... Hopefully I will get smarter here

(The website runs on a Linux apache Server and I try to run this with php v. 7.3.19)

question from:https://stackoverflow.com/questions/65897549/php-command-works-by-running-it-static-but-not-with-a-var-between

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

So just look at the output of this code, it really will help you to debug

<?php

$password = "Password";

$commandOne = 'sshpass -p "'.$password.'" scp -o StrictHostKeyChecking=no /file.txt user@127.0.0.1:/home/user/ 2>&1';
$commandTwo = 'sshpass -p "Password" scp -o StrictHostKeyChecking=no /file.txt user@127.0.0.1:/home/user/ 2>&1';

echo $commandOne . PHP_EOL;
echo $commandTwo . PHP_EOL;

output

sshpass -p "Password" scp -o StrictHostKeyChecking=no /file.txt user@127.0.0.1:/home/user/ 2>&1
sshpass -p "Password" scp -o StrictHostKeyChecking=no /file.txt user@127.0.0.1:/home/user/ 2>&1

They are completly same when I use put "Pasword" in the password variable, so now just try to put your real pass and compare them, it will show you the problem
If you get the same result just try this in your code

<?php

$password = "Password";
$command = 'sshpass -p "'.$password.'" scp -o StrictHostKeyChecking=no /file.txt user@127.0.0.1:/home/user/ 2>&1';
shell_exec($command);

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...