3

Windows does have a concept for null devices as detailed here, here or here for example. All examples that I can find are about redirecting streams.

While porting a script from Linux to Windows, I came across something odd: If nul is opened like a file, I an write to it, close it, re-open it and read the data back. Sometimes. Checking the working directory, I can see a file named nul being created and growing as I write data to it. Ironically, I can not delete it from the explorer, only from the command line.

Windows 10 comes with its a native implementation of OpenSSH nowadays. I wanted to use an old trick: When it is looking for the known_hosts file, I want to point it to a null device instead. On Linux, this looks as follows: ssh -o UserKnownHostsFile=/dev/null user@host. On Windows, I tested it as follows: ssh -o UserKnownHostsFile=nul user@host. I got a file named nul containing the hosts. I also tried NUL, null and NULL with the same result. Trying to point to \Device\Null or /Device/Null causes ssh to complain that it can not find/open those files.

How do I correctly use and/or point to the null device on Windows (10)?

s-m-e
  • 3,433
  • 2
  • 34
  • 71
  • are you using powershell or cmd – KetZoomer May 03 '21 at 15:59
  • Both cmd and Python scripts (with sub-processes). Result is always the same. Have not tried Powershell. – s-m-e May 03 '21 at 16:00
  • https://stackoverflow.com/a/45391321/449722 – g01d May 03 '21 at 16:01
  • try powershell, `ssh -o UserKnownHostsFile=$null user@host` – KetZoomer May 03 '21 at 16:01
  • @g01d The NUL followed by a colon (`NUL:`) does not work: I get a file with some unicode character instead of a colon. This is bizarre. – s-m-e May 03 '21 at 16:12
  • @KetZoomer While I use cmd for testing, for production I am starting my processes independent of any shell, i.e. [subprocess.Popen](https://docs.python.org/3/library/subprocess.html#subprocess.Popen) with `shell = False`. – s-m-e May 03 '21 at 16:14

2 Answers2

7

The JS crowd has a solution: I need to point to \\.\NUL. This is the actual name or path of the null device, apparently.

s-m-e
  • 3,433
  • 2
  • 34
  • 71
  • That actually makes sense, being a full UNC path. Good find. https://www.pcmag.com/encyclopedia/term/unc – g01d May 03 '21 at 16:23
1

What I'm doing is create a file C:\Users\myself\.ssh\config (just like on Linux), with this:

# Don't save local computers to known_hosts
Host localhost 127.?*.?*.?* *.local 10.?*.?*.?* 192.168.?*.?* 172.16.?*.?* 172.17.?*.?* 172.18.?*.?* 172.19.?*.?* 172.2?.?* 172.30.?*.?* 172.31.?*.?*
    StrictHostKeyChecking no
    UserKnownHostsFile=\\.\NUL

This matches most IPv4 thingies/whatever that are considered private networks. Do this at home, but not in your company network though - because of reasons.

dersimn
  • 805
  • 9
  • 15