Linux命令使用SharpSSH库时无法从.NET执行 [英] Linux Command Doesn't work from .NET execution when using SharpSSH library
问题描述
我试图在Linux中通过.NET运行一个非常简单的命令使用SharpSsh .NET库。代码不会抛出任何异常,它仍然不工作。当我从终端运行 sudo php /www/data/{directory}/scripts/create_file.php
命令时,它会创建该文件。当我从终端运行 sudo php /www/data/{directory}/scripts/delete_file.php
命令时,它会删除该文件。当从.NET调用此代码时,我没有看到正在创建/删除的文件。
查看此设置的答案:
< a href =http://stackoverflow.com/questions/9587821/sharpssh-net-library-unable-to-connect-to-linux-debian-from-net> SharpSSH .NET库:无法连接到Linux( Debian).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代码:
使用System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
使用Tamir.SharpSsh;
命名空间测试
{
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);
}
}
}
我假设这些玩具的php文件是一个较小的情况下。
如果你需要实际创建和删除一个文件,不要使用php
如果您想要关闭密码提示,请使用 / etc / sudoers
文件 - 请参阅NOPASSWD此处。
PHP应不以root身份运行,尤其是在命令行中的变量。如果你有权限问题 - 解决他们! PHP应该以具有最低权限的用户身份运行。
如果您需要一个维护帐户来访问ssh,并且不想让php用户拥有额外的功能,使其成为与php用户共享组的另一个用户 - 您可以使用组权限。 (您可能需要调整文件创建的默认权限。)
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");
}
}
}
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.)
这篇关于Linux命令使用SharpSSH库时无法从.NET执行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!