parsed Posted May 17, 2009 Share Posted May 17, 2009 Hello everyone! Nice to see such a community dedicated to helping out with PHP. I hope someone can help me out with my issue. For some time now I have been trying to re-create this bash script into a pure PHP script and by searching online I found out that I needed libssh2, which I have installed now and confirmed is working. Unfortunately I have still not been able to re-create the script in pure PHP. I suspect that the server in question does not accept any commands once a shell has opened as a security measure and that what the bash script does is send the commands without opening a shell, which the server then accepts. Since I am new with both bash scripts and PHP I might be wrong in my suspicion and that something else might be wrong, but so far I have not managed to make any PHP script that worked. Hopefully someone here might help me out with this, I would appreciate it. This is the bash script: #!/bin/sh init=STATUS docool=YES doheat=YES id=AS52-356 key=/.ssh/id_rsa dir=/etc/slog/bak/ echo "mode='$init' cooldown='$docool' heatup='$doheat' device='$id'" | ssh -T -i $key root@server > /etc/slog/$init prevfile="$(ls $dir | sort -n | tail -n1)" docopy=0 if [ -z "$prevfile" ]; then docopy=1 else prevsum="$(md5sum $dir/$prevfile | cut -d\ -f1)" currsum="$(md5sum /etc/slog/$init | cut -d\ -f1)" echo "$prevsum $currsum" if [ "$prevsum" != "$currsum" ]; then docopy=1 else echo "No changes" fi fi if [ "$docopy" -eq 1 ]; then file="$dir/$init-$(date '+%Y%m%d-%H%M')" echo "Creating new $file" cp /etc/slog/$init $file else echo "No changes" fi Any ideas on how I can re-create the script as PHP? Link to comment https://forums.phpfreaks.com/topic/158487-converting-bash-to-php/ Share on other sites More sharing options...
BK87 Posted May 17, 2009 Share Posted May 17, 2009 try... php.net/system also php.net/exec for more details on how to run bash form php.... if you need this converted to php, email me we can talk on price. Link to comment https://forums.phpfreaks.com/topic/158487-converting-bash-to-php/#findComment-835963 Share on other sites More sharing options...
parsed Posted May 18, 2009 Author Share Posted May 18, 2009 Thank you and I appreciate your offer, but I would rather do it myself so I can learn something about bash and PHP in the process. So I am looking for some help and some suggestions in the first place. Using php.net/system or php.net/exec is not an option because I would be back at the beginning using a bash script which is what I am trying avoid. I have already re-created this script a couple of times unsuccessfully and that is the reason I have decided to start from the beginning again, but this time I intend to ask about the things I am unsure about so that I won't yet again end in a dead end. So far on this line I hit my first problem: echo "mode='$init' cooldown='$docool' heatup='$doheat' device='$id'" | ssh -T -i $key root@server > /etc/slog/$init I tried to re-create the ssh login part with this code: $connection = ssh2_connect("server", 22); if (ssh2_auth_pubkey_file($connection, "root", "/.ssh/id_rsa")) { echo "Authenticated"; } else { die("Failed"); } Which failed with this message "Warning: ssh2_auth_pubkey_file() expects at least 4 parameters, 3 given". I tried many different combinations, but all fail the same way since it seems that ssh2_auth_pubkey_file() needs both a private key and a public one. Although I have both keys the original bash script works with only the private key. Is this possible to do with PHP? Now if I add both the private and public key it authenticates and I hit on my second problem. As mentioned I suspect the bash script does not request a shell, but instead somehow sends the command directly (if I have interpreted the bash script wrong please tell me). This is because when I manually use ssh and log into the server I get the shell and banner but if I try to do anything the connection terminates instead of outputting data as it does when the bash script runs. The same thing happens when sending the command using PHP. Here is the code: if(!($stream = ssh2_exec($connection, "mode='$init' cooldown='$docool' heatup='$doheat' device='$id'")) ){ echo "Failed"; } else{ stream_set_blocking($stream, true); $output = ""; while($buffer = fread($stream,4096)){ $output .= $buffer; } fclose($stream); } echo "Received\n"; echo $output; This code does not seem to be working, the last thing I get is the text "Received" and nothing else. Although ssh2_exec() doesn't seem to fail sending the command, but since nothing is returned ($output is empty) I suspect the server did not accept the command, which I can confirm by looking at the server (the status on the server did not changing). I am truly lost here and would really appreciate any help sorting this out. Thanks. Link to comment https://forums.phpfreaks.com/topic/158487-converting-bash-to-php/#findComment-836359 Share on other sites More sharing options...
parsed Posted June 7, 2009 Author Share Posted June 7, 2009 So is there really nobody that can offer some advice on accomplishing a task such as this one? Link to comment https://forums.phpfreaks.com/topic/158487-converting-bash-to-php/#findComment-851227 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.