Use powershell to run cygwin configuration file

0

I'm running Windows server 2012 R2. I have a cygwin_config file that I run inside cygwin with excpect cygwin_config command. The file configures my ssh_config and sshd_config. Is there any way to run this script from my powershell so I wouldn't need to go into cygwin to run it? I have tried a few things but without any success such as:

$file= Get-ChildItem  C:\Cygwin\home\Administrator\cygwin_config | Select-Object 
$cmd=Start-Process C:\Cygwin\bin\mintty.exe $file

It tried to open mintty.exe but it immideately closed the mintty.exe. If nothing else helps I guess I will need to create the users that ssh-host-config needs by powershell.

I also created a file that has expect /path/to/cygwin_config and saved it as .exe file. After double clicking on it I get an error popup: "Windows cannot access the specified device,path, or file. You may not have the appropriate permissions to access them." I have tried to run things as admin and changed permissions in directory but nothing works for that one.

My cygwin_config:

#!/bin/expect -f

spawn ssh-host-config

set timeout 20

expect {
     "Should StrictModes be used? (yes/no)" { send "no\r" }
}

set timeout 5

expect {
     "new local account 'sshd'? (yes/no)" { send "yes\r" }
}

expect {
     "Do you want to install sshd as a service?"  {send 
     "yes\r";exp_continue }
     "Enter the value of CYGWIN for the daemon:" { send " \r"; }
}

set timeout 5

expect {
     "Do you want to use a different name? (yes/no)" { send "no\r";
      exp_continue }
     "Create new privileged user account" { send "yes\r";
      exp_continue }
     "Please enter the password" { send "pwd\r";
      exp_continue }
     "Reenter:" {send "pwd\r";
      exp_continue }
}

[EDIT] Got over the windows popup error by changing permissions for the file from the properties(gave full rights to admin). But it still won't work because the app can't run on my pc: "This app can't run on your PC" So that solution goes to the trash bin.

Mira

Posted 2018-02-08T13:06:08.033

Reputation: 1

Answers

1

I don't use Powershell, presumably a similar setup to what's described here ought to work. Overall, there might be two issues with the listed approach above:

  1. The script is an expect script, so it ought to be run via C:\Cygwin\bin\expect.exe, not C:\Cygwin\bin\mintty.exe
  2. The script location is interpreted as an argument to a Cygwin command, so the path ought to use a notation Cygwin understands (which differs from Windows notation unless it's quoted).

You might want to try:

C:\Cygwin\bin\expect.exe 'C:\Cygwin\home\Administrator\cygwin_config'

Those single quotes are important so Cygwin recognizes a Windows-style path. Or you can try using Cygwin path notation in the first place:

C:\Cygwin\bin\expect.exe /home/Administrator/cygwin_config'

Here is a different example to play with. Create a file called run_myscript.bat with contents:

C:\Cygwin\bin\sh 'C:\Cygwin\home\Administrator\myscript.sh'

Create the script myscript.sh at the specified location with contents:

date
sleep 10

Make sure your batch script is executable (chmod 775 run_myscript.bat). Run that batch script.

Uwe Mayer

Posted 2018-02-08T13:06:08.033

Reputation: 36

Thank you, I was able to get a bit forward on my quest. Now instead when its trying to run it, it won't succeed because ssh-host-config is not existing according to the powershell. `C:\Cygwin\bin\expect.exe : couldn't execute "ssh-host-config": no such file or directory At C:\Users\Administrator\Desktop\cygwin.ps1:1 char:1

  • C:\Cygwin\bin\expect.exe '/home/Administrator/cygwin_config'´`
  • < – Mira – 2018-05-28T12:30:10.353

Mira, it is not the powershell that is reporting that ssh-host-config does not exist, this message originates from the expect command (and is only passed through by the powershell). You may want to create a shell script in Cygwin that sets the required environment variables (PATH in particular) and call ssh-host-config from within that shell script, and finally call that shell script with the spawn command from your expect script. – Uwe Mayer – 2018-05-29T13:44:58.830