如何使用SshClient将内容写入Linux SFTP服务器上的文件? [英] How to write content to file on linux SFTP server using SshClient?
问题描述
我正在使用.Net(SshClient)代码处理Linux SFTP服务器,因此我正在SFTP服务器上创建文件夹并也创建文件.为此,我正在使用以下代码,
I am dealing with Linux SFTP server using .Net (SshClient) code, so I am creating folder on SFTP server and creating files also. For this I am using following code,
using (var client = new SshClient(WebConfigurationManager.AppSettings["SftpServer"], WebConfigurationManager.AppSettings["SftpUserName"], WebConfigurationManager.AppSettings["SftpPassword"]))
{
client.Connect();
if (client.IsConnected)
{
// Check if the Folder Already Exists
var ifExists = client.RunCommand("cat /etc/passwd | grep -w " + ftpUserName);
// If the Folder and User Does Not exists , create both
if (string.IsNullOrEmpty(ifExists.Result))
{
client.RunCommand("echo Asdf@123 | sudo -S mkdir /sftpusers/" + ftpUserName);
client.RunCommand("echo Asdf@123 | sudo -S mkdir /sftpusers/" + ftpUserName + "/import"); // this is for creating folder
client.RunCommand("echo Asdf@123 | sudo -S mkdir /sftpusers/" + ftpUserName + "/schema");
client.RunCommand("echo Asdf@123 | sudo -S touch /sftpusers/" + ftpUserName + "/schema/fileName.txt");
sftpCreated = true;
}
}
client.Disconnect();
}
此代码是在SFTP上创建文件夹和文本文件的工作文件,但是现在我试图将数据写入该文件,为此,我正在使用以下代码,
This code is working file to create a folder and text files on SFTP but Now I am trying to write data to that file, and for that I am using following code,
client.RunCommand("echo Asdf@123 | sudo -S echo hi user >> /sftpusers/asb/schema/filename.txt");
但是它不起作用. Error = "bash: /sftpusers/asb/schema/filename.txt: Permission denied\n
.
如果使用相同的命令,即echo pwd | sudo -s cmd
在SFTP服务器上创建文件夹,文件,然后为什么在将内容写入文件时失败.
I am little confused that, if same command i.e. echo pwd | sudo -s cmd
is creating folder, files on SFTP server then why it failing while writing content to file.
在此方面需要尽快的帮助.
Need help on this as early as possible.
推荐答案
sudo touch ./blabla
将创建具有根(!)权限的root用户的blabla
文件.该文件的所有者将是root,并且很可能只允许root用户写入.
Will create a blabla
file as root with root(!) permissions. The owner of this file will be root and most probably the write will be only allowed for root.
sudo echo 123 > ./blabla
这将以root用户身份执行echo
,并以shell(!)用户的身份将输出重定向到blabla
. sudo
使命令echo
以root身份运行.但是重定向>
是由外壳程序完成的,而不是由sudo
完成.因此,重定向是通过打开外壳程序来完成的,对于您而言,不是由非root用户(无法访问该文件)来完成的.
This will execute echo
as root, and redirect the output to blabla
as the shell(!) user. sudo
makes the command echo
run as root. But the redirection >
is done by the shell, not by sudo
. So the redirection is done by the however opened the shell, in your case by a user that isn't root, who can't access the file.
您必须以root用户身份写入文件才能正常工作.您可以以root用户身份运行子shell:
You have to write to the file as root for it to work. You can run a subshell as root:
echo 123 | sudo sh -c 'cat > ./blabla'
// or better for scripting, cause no double quoting is needed
echo 123 | sudo sh -c 'cat > "$1"' -- ./blabla
这将echo 123
并以root用户身份执行sh
,并使用命令cat
重定向到脚本的第一个参数,即blabla
.请注意,由于Shell以root身份运行,因此重定向也以root身份完成,因此Shell能够打开并写入文件.
This will echo 123
and execute sh
as root with the command cat
redirected to the first argument of the script, that is blabla
. Note that as the shell is run as root, the redirection is also done as root, so the shell is able to open and write to the file.
或者,更干净的方法是制作tee
.
Alternatively a more clean approach is to make a tee
.
echo 123 | sudo tee blabla >/dev/null
请记住将tee
的标准输出重定向为null,这样它就不会打印任何内容.
Remember to redirect tee
's stdout to null, so it doesn't print anything.
做:
client.RunCommand("echo Asdf@123 | sudo -S sh -c 'echo hi user >> "$1"' -- /sftpusers/asb/schema/filename.txt");
请注意,因为您使用sudo -S
来读取密码(这是不安全的并且很糟糕),所以不能使用stdin将写入的字符串传递给tee
.
Note that because you use sudo -S
to read the password (which is unsafe and kind of bad), you can't use stdin to pass written string to tee
.
这篇关于如何使用SshClient将内容写入Linux SFTP服务器上的文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!