Tuesday, October 13, 2009

Remote CMD.exe Shell

I've been using SSH on both Windows and Linux for a long time to get remote access to various machines, and to tunnel TCP connections through firewalls.

On windows, this has generally meant installing Cygwin's SSHD server, which gives you the cygwin Bash shell.

While I'm comfortable in a unix environment, when I log into a windows box I generally prefer to see a shell that looks like a standard windows command line, e.g. I generally don't want the slashes going the wrong way, and I don't want the whole windows filesystem visible a few levels too deep (/cygdrive/c/ for example )

I knew about the http://sshwindows.sourceforge.net/ project, but it has not been active for a long time (although It does now look active again). And I dont want to have to install something new, when I already have a working SSHD server on many machines. There's also WinSSHD, but that is not free.

I just discovered, by editing /etc/passwd I can make my existing cygwin SSHD installations give me a CMD.exe prompt instead of a Bash shell.

All I had to do was find the line corresponding to my user, and edit the end of the line, changing '/bin/bash' to '/cygdrive/c/WINDOWS/system32/cmd.exe'

And thats it!

Unfortunately, this stops SCP working. I do know the above OpenSSH for Windows project does not have this problem by using the 'switch.exe' program, but I dont see that included with a standard cygwin installation.

To get around this issue, I just created another user, same as the my first, but with the original '/bin/bash' shell configured instead of cmd.exe. I log in with this user when I need SCP.

Update: It doesn't like tab file completion, Ctrl-C is killing putty instead of the remote application, and I forgot how important 'vim' is in these remote terminals... so I'm switching back to the default bash shell. I can always just start cmd.exe when inside bash...

6 comments:

  1. A great discovery.I am recently started using remote connections so do not have much knowledge on this.But as I read your blog I felt that no one can give such detailed insight on topic as you wrote.Thanks
    e-sign act

    ReplyDelete
  2. Thanks for sharing your experience. I'll save myself the trouble and just launch cmd from within bash.

    ReplyDelete
  3. Yeah, but I have a problem. The arrow keys and the TAB key don't work.

    ReplyDelete
  4. I have another option that works with SFTP as well, its a modified version of the /bin/sftponly script that CopSSH / Cygwin has, make up a text file /bin/wincmd (and set that as your shell)

    The shell script is as follows... SFTP / SCP works, and you get a Windows Command Prompt

    #!/bin/bash
    if [ "$*" != "-c /bin/sftp-server" ]; then
    cmd.exe
    fi
    exec /bin/sftp-server

    ReplyDelete
    Replies
    1. This comment has been removed by the author.

      Delete
    2. A slightly better version of the script, supports running scripted commands via SSH client, due to the behaviour of the old switch.exe shell (part of an old windows openssh setup that gave a windows command prompt) which required triple \\\ to produce a single \ in the output script, this also reduces the triple \\\ to single \ if found in the script passed.

      #!/bin/bash
      if [ "$*" != "-c /bin/sftp-server" ]; then
      if [ "$*" == "" ]; then
      cmd.exe
      else
      echo "Executing Script"
      commandstring=${*//\\\\\\/\\}
      echo "${commandstring:3}" > temp.bat
      cmd.exe /c temp.bat
      fi
      exit 1
      fi
      exec /bin/sftp-server

      Delete