I'm trying to execute sh
script from Windows on Remote SUSE Linux using WinSCP .NET assembly.
I've created a session as follow:
$sessionOptions = New-Object WinSCP.SessionOptions -Property @
{
Protocol = [WinSCP.Protocol]::Sftp
HostName = "RemoteIp"
UserName = "username"
Password = "password"
}
$session = New-Object WinSCP.Session
I run the sh
script
$session.ExecuteCommand("bash /home/script.sh" )
and I get permissions errors, for example:
rm: cannot remove '/somefolder': Permission denied.
Simple command like uname
works fine.
Any idea how can I log in as root?
CodePudding user response:
You have to run your command through sudo
:
$session.ExecuteCommand("sudo bash /home/script.sh")
Though WinSCP does not support providing input to commands. So your remote system must be configured not to require sudo
password (at all, or for that specific command).
Some references:
- How do I change user after login (e.g. su root)?
- Allowing automatic command execution as root on Linux using SSH.
CodePudding user response:
Go to your WinSCP profile (Session > Sites > Site Manager)
Click on Edit > Advanced... > Environment > SFTP
Insert sudo su -c /usr/lib/sftp-server in "SFTP Server" (note this path might be different in your system)
Save and connect