0

I'm trying to run a very simple command in Linux via .NET using the SharpSsh .NET library. The code doesn't throw any exceptions, and it's still not working. When I run the sudo php /www/data/{directory}/scripts/create_file.php command from the terminal it creates the file. And when I run the sudo php /www/data/{directory}/scripts/delete_file.php command from the terminal, it deletes the file. When calling this code from .NET, I don't see the file being created/deleted.

See this answer for setup:
SharpSSH .NET Library: unable to connect to Linux (Debian) from .NET

create_file.php:

<?php
$handle = fopen( 'test.txt', 'w' );
fwrite( $handle, 'Test Contents' );
fclose( $handle );

delete_file.php:

<?php
if( file_exists( 'test.txt' ) )
        unlink( 'test.txt' );

.NET code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Tamir.SharpSsh;

namespace Testing
{
    public class Unix
    {
        public static void CreateFile()
        {
            SshExec ssh = new SshExec(Constants.LINUX_HOST, Constants.LINUX_USER, Constants.LINUX_PASSWORD);
            ssh.Connect();
            String result = ssh.RunCommand("sudo php " + Constants.PHP_SCRIPTS_DIR + "create_file.php");
        }

        public static void DeleteFile()
        {
            SshExec ssh = new SshExec(Constants.LINUX_HOST, Constants.LINUX_USER, Constants.LINUX_PASSWORD);
            ssh.Connect();
            String result = ssh.RunCommand("sudo php " + Constants.PHP_SCRIPTS_DIR + "delete_file.php");
        }
    }
}
Community
  • 1
  • 1
JustBeingHelpful
  • 18,332
  • 38
  • 160
  • 245
  • What ends up in the result string? – Broam Mar 12 '12 at 18:32
  • 1
    Does sudo require a password for this operation? If not, does it require it the first time? If that is true, the machine is caching the authentication credentials. Why do you need to run php as root? This should be run as the user that owns the file. – Broam Mar 12 '12 at 18:34
  • hmmmm good point. It does in the terminal. Let me try. – JustBeingHelpful Mar 12 '12 at 18:35
  • I don't think my issue is the command. I found you can read the input from stdin by placing echo "password" in front of the command. And this still didn't work: ssh.RunCommand("echo \"" + Constants.LINUX_PASSWORD + "\" | sudo php " + Constants.PHP_SCRIPTS_BUYEMP_DIR + "delete_file.php"); – JustBeingHelpful Mar 13 '12 at 07:28
  • Which version of the SharpSSH library are you using? It's a bit old and rough to use, overall. If you try the newest compilation at http://bitbucket.org/mattgwagner/sharpssh and it doesn't work the way you intend, you might want to check out SSH.net over at CodePlex. That library is well maintained. – MattGWagner Mar 13 '12 at 19:20
  • The one from the hyperlink below this text in my question, "See this answer for setup:" – JustBeingHelpful Mar 13 '12 at 20:16
  • Oh man, you're using the library from 2005... even Tamir updated it on SourceForge till like 2007. Give the newer version a shot. – MattGWagner Mar 14 '12 at 13:00
  • @MattGWagner can you send me the link to the latest? – JustBeingHelpful Apr 25 '12 at 00:49
  • http://bitbucket.org/mattgwagner/sharpssh has a semi-updated version that I've rolled some patches into. – MattGWagner Apr 25 '12 at 00:55

1 Answers1

0

I assume that these toy php files are a reduced case of something larger.

If you need to actually create and delete a file, don't use php - you can just use a shell command directly.

If you want ot turn off the password prompt, there's syntax in the /etc/sudoers file for that - see "NOPASSWD" here.

PHP should not be run as root, especially with variables in the command line like that. If you're having permissions issues -- solve them! PHP should be run as a user with least privilege.

If you need a 'maintenance' account to ssh in and don't want to make the php user have extra capability, make it another user that shares a group with the php user - and you can use group permissions. (You may need to fiddle with the default permissions on file create.)

Broam
  • 4,602
  • 1
  • 23
  • 38
  • I was able to avoid the password prompt another way--by putting echo in front of the script. The issue seems to be something with the SharpSsh library. – JustBeingHelpful Mar 13 '12 at 07:32