How can I identify a remote host OS (Unix/Windows) using python ? One solution I found is to check whether the port22 is open but came to know that some Windows hosts also having Port 22 open but connections refused. Please let me know the efficient way to do the same. Thanks in advance.
For security reasons, most operating systems do not advertise information over the network. While tools such as nmap can deduce the OS running on a remote system by scanning ports over the network the only way to reliably know the OS is to login to the system. In many cases the OS will be reported as part of the login process so establishing a connection over the network will suffice to determine the OS. Running "uname -a" on the remote system will also retrieve the OS type on linux systems.
This will retrieve the welcome string from HOST which usually includes the OS type. Substitute a valid user name for UNAME and host name for HOST.
#!/usr/bin/env python3
import sys
import subprocess
CMD="uname -a"
conn = subprocess.Popen(["ssh", "UNAME@HOST", CMD],
shell=False,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE)
res = conn.stdout.readlines()
print(res)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With