2

I need your help. ssh connection was successful. I have checked this command -> Pass : pc1.RunCommand("echo '!123123' | sudo -S reboot")

So I tried other moves.

I need run linux shell script file.

remove : pc1.RunCommand("echo '!123123' | sudo -S reboot") add : pc1.RunCommand("cd ~/ryu/123.sh"); <--- not working

but this command not working. (in putty pass) Does anyone know how?

Thank you.

        PasswordConnectionInfo info1 = new PasswordConnectionInfo("server ip", 22, "user", "password");
        info1.Timeout = TimeSpan.FromSeconds(5);
        SshClient pc1 = new SshClient(info1);
        try
        {
            if (checkBox1.Checked == true)
            {
                pc1.Connect();
                if (pc1.IsConnected)
                {
                    pc1.RunCommand("echo '!123123' | sudo -S reboot"); <--- this command pass
                    pc1.RunCommand("cd ~/ryu/123.sh"); <--- this command not working
                }
Martin Prikryl
  • 188,800
  • 56
  • 490
  • 992

1 Answers1

0

Assuming the permission of the .sh file is already executable (chmod a+x). I think the 2nd command works just fine. You just have to "read" the "output" if there is. This is how I implemented Renci.SSHNet: I hope it helps.

$sshSession = New-Object Renci.SshNet.SshClient($serverIp, 22, $username, $password)
$sshSession.Connect()  # check if connected first if you want - 
$stream = $sshSession.CreateShellStream($serverIp, 500, 100, 1024, 600, 1024)
$reader = New-Object System.IO.StreamReader($stream)
$writer = New-Object System.IO.StreamWriter($stream)
$writer.AutoFlush =$true
$lineMatch = "[root@server1]#"  # sample only; change this to the text you are seeing on your terminal
$writer.WriteLine("pwd") # print working directory
Start-Sleep -Seconds 1
# this will loop until the the output is empty
do{
    $line = $reader.ReadLine()
    if(!([string]::IsNullOrEmpty($line))){
        Write-Host $line  # print the current line
    }        
}while($line -ne $lineMatch)
melcdn
  • 31
  • 6