Hostname: Difference between revisions

9,876 bytes added ,  5 months ago
m
m (→‎{{header|Wren}}: Minor tidy)
 
(22 intermediate revisions by 18 users not shown)
Line 8:
=={{header|Ada}}==
Works with GCC/GNAT
<langsyntaxhighlight lang="ada">with Ada.Text_IO; use Ada.Text_IO;
with GNAT.Sockets;
 
Line 14:
begin
Put_Line (GNAT.Sockets.Host_Name);
end Demo;</langsyntaxhighlight>
 
=={{header|Aikido}}==
<syntaxhighlight lang="aikido">
println (System.hostname)
</syntaxhighlight>
 
=={{header|ALGOL 68}}==
Line 22 ⟶ 27:
<!-- {{does not works with|ELLA ALGOL 68|Any (with appropriate job cards) - tested with release 1.8.8d.fc9.i386 - No such library function.}} -->
{{works with|POSIX|.1}}
<langsyntaxhighlight lang="algol68">STRING hostname;
get(read OF execve child pipe("/bin/hostname","hostname",""), hostname);
print(("hostname: ", hostname, new line))</langsyntaxhighlight>
 
=={{header|Aikido}}==
<lang aikido>
println (System.hostname)
</lang>
 
=={{header|AppleScript}}==
<langsyntaxhighlight lang="applescript">
host name of (system info)
</syntaxhighlight>
</lang>
 
=={{header|Arc}}==
<syntaxhighlight lang="arc">(system "hostname -f")</syntaxhighlight>
 
=={{header|Arturo}}==
 
<syntaxhighlight lang="arturo">print sys\hostname</syntaxhighlight>
 
{{out}}
 
<pre>drkameleons-Mac.home</pre>
 
=={{header|AutoHotkey}}==
<syntaxhighlight lang AutoHotkey="autohotkey">MsgBox % A_ComputerName</langsyntaxhighlight>
 
via Windows Management Instrumentation (WMI)
<langsyntaxhighlight AutoHotkeylang="autohotkey">for objItem in ComObjGet("winmgmts:\\.\root\CIMV2").ExecQuery("SELECT * FROM Win32_ComputerSystem")
MsgBox, % "Hostname:`t" objItem.Name</langsyntaxhighlight>
 
=={{header|Arc}}==
<lang Arc>(system "hostname -f")</lang>
 
=={{header|AWK}}==
 
{{noticebox||WARNING: the following purported solution makes an assumption about environment variables that may not be applicable in all circumstances.}}
<langsyntaxhighlight lang="awk">$ awk 'BEGIN{print ENVIRON["HOST"]}'
E51A08ZD</langsyntaxhighlight>
 
=={{header|BaCon}}==
<langsyntaxhighlight lang="freebasic">PRINT "Hostname: ", HOSTNAME$</langsyntaxhighlight>
 
=={{header|Batch File}}==
Since Windows 2000 :
<syntaxhighlight lang ="dos">Hostname</langsyntaxhighlight>
 
=={{header|BBC BASIC}}==
{{works with|BBC BASIC for Windows}}
<langsyntaxhighlight lang="bbcbasic"> INSTALL @lib$+"SOCKLIB"
PROC_initsockets
PRINT "hostname: " FN_gethostname
PROC_exitsockets</langsyntaxhighlight>
 
=={{header|C}}/{{header|C++}}==
Line 70 ⟶ 78:
 
{{works with|POSIX|.1}}
<langsyntaxhighlight lang="c">#include <stdlib.h>
#include <stdio.h>
#include <limits.h>
Line 79 ⟶ 87:
char name[_POSIX_HOST_NAME_MAX + 1];
return gethostname(name, sizeof name) == -1 || printf("%s\n", name) < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
}</langsyntaxhighlight>
 
=={{header|C sharp|C#}}==
<syntaxhighlight lang ="csharp">System.Net.Dns.GetHostName();</langsyntaxhighlight>
 
=={{header|Caché ObjectScript}}==
Line 89 ⟶ 97:
=={{header|Clojure}}==
 
<langsyntaxhighlight lang="clojure">
(.. java.net.InetAddress getLocalHost getHostName)
</syntaxhighlight>
</lang>
 
<langsyntaxhighlight lang="shell">
java -cp clojure.jar clojure.main -e "(.. java.net.InetAddress getLocalHost getHostName)"
</syntaxhighlight>
</lang>
 
=={{header|COBOL}}==
<langsyntaxhighlight lang="cobol"> identification division.
program-id. hostname.
 
Line 113 ⟶ 121:
goback.
end program hostname.
</syntaxhighlight>
</lang>
 
=={{header|CoffeeScript}}==
<langsyntaxhighlight lang="coffeescript">
os = require 'os'
console.log os.hostname()
</syntaxhighlight>
</lang>
 
=={{header|Common Lisp}}==
Another operating system feature that is implemented differently across lisp implementations. Here we show how to create a function that obtains the required result portably by working differently for each supported implementation. This technique is heavily used to make portable lisp libraries.
<langsyntaxhighlight lang="lisp">(defun get-host-name ()
#+(or sbcl ccl) (machine-instance)
#+clisp (let ((s (machine-instance))) (subseq s 0 (position #\Space s)))
#-(or sbcl ccl clisp) (error "get-host-name not implemented"))</langsyntaxhighlight>
 
{{libheader|CFFI}}
Line 132 ⟶ 140:
Another way is to use the [[FFI]] to access POSIX' <code>gethostname(2)</code>:
 
<langsyntaxhighlight lang="lisp">(cffi:defcfun ("gethostname" c-gethostname) :int
(buf :pointer) (len :unsigned-long))
 
Line 139 ⟶ 147:
(unless (zerop (c-gethostname buf 256))
(error "Can't get hostname"))
(values (cffi:foreign-string-to-lisp buf))))</langsyntaxhighlight>
 
<langsyntaxhighlight lang="lisp">BOA> (get-hostname)
"aurora"</langsyntaxhighlight>
 
=={{header|Crystal}}==
<syntaxhighlight lang="crystal">hostname = System.hostname</syntaxhighlight>
 
=={{header|D}}==
<langsyntaxhighlight lang="d">import std.stdio, std.socket;
 
void main() {
writeln(Socket.hostName());
}</langsyntaxhighlight>
 
=={{header|Delphi}}==
<langsyntaxhighlight Delphilang="delphi">program ShowHostName;
 
{$APPTYPE CONSOLE}
Line 167 ⟶ 178:
else
Writeln('error getting host name');
end.</langsyntaxhighlight>
 
=={{header|E}}==
 
<langsyntaxhighlight lang="e">makeCommand("hostname")()[0].trim()</langsyntaxhighlight>
 
Not exactly a good way to do it. A better way ought to be introduced along with a proper socket interface. [[Category:E examples needing attention]]
 
=={{header|Emacs Lisp}}==
<syntaxhighlight lang Lisp="lisp">(system-name)</langsyntaxhighlight>
 
=={{header|Erlang}}==
<syntaxhighlight lang="erlang">Host = net_adm:localhost().</syntaxhighlight>
 
=={{header|F_Sharp|F#}}==
<langsyntaxhighlight lang="fsharp">printfn "%s" (System.Net.Dns.GetHostName())</langsyntaxhighlight>
 
=={{header|Factor}}==
<langsyntaxhighlight lang="factor">USE: io.sockets
host-name</langsyntaxhighlight>
 
=={{header|Forth}}==
{{works with|GNU Forth|0.7.0}}
<langsyntaxhighlight lang="forth">include unix/socket.fs
 
hostname type</langsyntaxhighlight>
 
=={{header|Erlang}}==
<lang Erlang>Host = net_adm:localhost().</lang>
 
=={{header|friendly interactive shell}}==
{{trans|UNIX Shell}}
 
<lang fishshell>hostname</lang>
or
<lang fishshell>uname -n</lang>
 
=={{header|Fortran}}==
Line 205 ⟶ 209:
 
The function/subroutine <tt>HOSTNM</tt> is a GNU extension.
<langsyntaxhighlight lang="fortran">program HostTest
character(len=128) :: name
call hostnm(name)
print *, name
end program HostTest</langsyntaxhighlight>
 
Using fortran 2003 C-interoperability we can call posix C function gethostname (unix system call) directly
<langsyntaxhighlight lang="fortran">
program test_hostname
use, intrinsic :: iso_c_binding
Line 252 ⟶ 256:
 
end program test_hostname
</syntaxhighlight>
</lang>
 
=={{header|FreeBASIC}}==
<langsyntaxhighlight lang="freebasic">' FB 1.05.0 Win64
 
' On Windows 10, the command line utility HOSTNAME.EXE prints the 'hostname' to the console.
Line 267 ⟶ 271:
Print
Print "Press any key to quit"
Sleep</langsyntaxhighlight>
 
=={{header|friendly interactive shell}}==
{{trans|UNIX Shell}}
 
<syntaxhighlight lang="fishshell">hostname</syntaxhighlight>
or
<syntaxhighlight lang="fishshell">uname -n</syntaxhighlight>
 
=={{header|Frink}}==
<syntaxhighlight lang="frink">callJava["java.net.InetAddress", "getLocalHost"].getHostName[]</syntaxhighlight>
 
 
=={{header|FutureBasic}}==
<syntaxhighlight lang="futurebasic">
include "NSLog.incl"
NSLog( @"%@", fn ProcessInfoHostName )
HandleEvents
</syntaxhighlight>
 
 
=={{header|Gambas}}==
'''[https://gambas-playground.proko.eu/?gist=11d7f489117d7909da509050103b7891 Click this link to run this code]'''
<langsyntaxhighlight lang="gambas">Public Sub Main()
 
Print System.Host
 
End</langsyntaxhighlight>
Output:
<pre>
Line 283 ⟶ 306:
=={{header|Go}}==
Use [https://golang.org/pkg/os/#Hostname <code>os.Hostname</code>].
<langsyntaxhighlight lang="go">package main
 
import (
Line 292 ⟶ 315:
func main() {
fmt.Println(os.Hostname())
}</langsyntaxhighlight>
 
=={{header|Groovy}}==
 
<syntaxhighlight lang ="groovy">println InetAddress.localHost.hostName</langsyntaxhighlight>
 
=={{header|Harbour}}==
<syntaxhighlight lang ="visualfoxpro">? NetName()</langsyntaxhighlight>
 
=={{header|Haskell}}==
{{libheader|network}}
<langsyntaxhighlight lang="haskell">import Network.BSD
main = do hostName <- getHostName
putStrLn hostName</langsyntaxhighlight>
 
Or if you don't want to depend on the network package being installed, you can implement it on your own (this implementation is based on the implementation in the network package).
 
 
<langsyntaxhighlight lang="haskell">module GetHostName where
 
import Foreign.Marshal.Array ( allocaArray0, peekArray0 )
Line 328 ⟶ 351:
 
main = do hostName <- getHostName
putStrLn hostName</langsyntaxhighlight>
 
=={{header|Icon}} and {{header|Unicon}}==
<langsyntaxhighlight lang="icon">procedure main()
write(&host)
end</langsyntaxhighlight>
 
=={{header|IDL}}==
<langsyntaxhighlight lang="idl">hostname = GETENV('computername')</langsyntaxhighlight>
 
=={{header|J}}==
<langsyntaxhighlight lang="j">NB. Load the socket libraries
 
load 'socket'
Line 351 ⟶ 374:
NB. and the hostname is fetched only once, then use a 'one-liner' to accomplish it:
 
> {: sdgethostname coinsert 'jsocket' [ load 'socket'</langsyntaxhighlight>
 
=={{header|Java}}==
<syntaxhighlight lang ="java">import java.net.*;
import java.net.InetAddress;
class DiscoverHostName {
import java.net.UnknownHostException;
public static void main(final String[] args) {
</syntaxhighlight>
try {
<syntaxhighlight lang="java">
System.out.println(InetAddress.getLocalHost().getHostName());
void printHostname() }throws catch (UnknownHostException e) { // Doesn't actually happen, but Java requires it be handled.
InetAddress localhost = InetAddress.getLocalHost();
}
System.out.println(localhost.getHostName());
}
}
}</lang>
</syntaxhighlight>
<pre>
penguin
</pre>
 
=={{header|JavaScript}}==
{{works with|JScript}}
<langsyntaxhighlight lang="javascript">var network = new ActiveXObject('WScript.Network');
var hostname = network.computerName;
WScript.echo(hostname);</langsyntaxhighlight>
 
=={{header|jq}}==
Line 379 ⟶ 406:
]</pre>
 
=={{header|Jsish}}==
<syntaxhighlight lang="javascript">var hn = exec("hostname", {retAll:true}).data.trim();</syntaxhighlight>
 
=={{header|Julia}}==
<syntaxhighlight lang="julia">
<lang Julia>
println(gethostname())
</syntaxhighlight>
</lang>
 
{{out}}
<pre>
harlan
</pre>
 
=={{header|K}}==
<syntaxhighlight lang="k">
_h
</syntaxhighlight>
 
{{out}}
<pre>
`"narasimman-pc"
</pre>
 
=={{header|Kotlin}}==
<langsyntaxhighlight lang="scala">// version 1.1.4
 
import java.net.InetAddress
Line 397 ⟶ 436:
fun main(args: Array<String>) {
println(InetAddress.getLocalHost().hostName)
}</langsyntaxhighlight>
 
=={{header|Lasso}}==
This will ge the hostname as reported by the web server
<syntaxhighlight lang Lasso="lasso">[web_request->httpHost]</langsyntaxhighlight>
-> www.myserver.com
 
This will ge the hostname as reported by the system OS
<langsyntaxhighlight Lassolang="lasso">define host_name => thread {
 
data
Line 439 ⟶ 478:
}
 
host_name</langsyntaxhighlight>
-> mymachine.local
 
=={{header|LFE}}==
 
<langsyntaxhighlight lang="lisp">
(net_adm:localhost)
</syntaxhighlight>
</lang>
 
=={{header|Liberty BASIC}}==
<langsyntaxhighlight lang="lb">lpBuffer$=Space$(128) + Chr$(0)
struct SIZE,sz As Long
SIZE.sz.struct=Len(lpBuffer$)
Line 456 ⟶ 495:
CurrentComputerName$=Trim$(Left$(lpBuffer$, SIZE.sz.struct))
 
print CurrentComputerName$</langsyntaxhighlight>
 
=={{header|Limbo}}==
As with nearly anything in Inferno, it boils down to reading a file:
 
<langsyntaxhighlight Limbolang="limbo">implement Hostname;
 
include "sys.m"; sys: Sys;
Line 492 ⟶ 531:
raise "fail:errors";
}
</syntaxhighlight>
</lang>
 
Sys->ATOMICIO is usually 8 kilobytes; this version truncates if you have a ridiculously long hostname.
Line 498 ⟶ 537:
=={{header|Lingo}}==
{{libheader|Shell Xtra}}
<langsyntaxhighlight lang="lingo">
sx = xtra("Shell").new()
if the platform contains "win" then
Line 504 ⟶ 543:
else
hostname = sx.shell_cmd("hostname", RETURN).line[1]
end if</langsyntaxhighlight>
 
=={{header|LiveCode}}==
<syntaxhighlight lang LiveCode="livecode">answer the hostName</langsyntaxhighlight>
 
=={{header|Lua}}==
Requires: LuaSocket
<langsyntaxhighlight lang="lua">socket = require "socket"
print( socket.dns.gethostname() )</langsyntaxhighlight>
 
=={{header|M2000 Interpreter}}==
<syntaxhighlight lang="m2000 interpreter">
Module Host {
\\ one way
Print computer$
\\ second way
Declare objNetwork "WScript.Network"
With objNetwork, "ComputerName" as cName$
Print cName$, cName$=Computer$
Declare objNetwork Nothing
}
Host
</syntaxhighlight>
 
=={{header|Maple}}==
<syntaxhighlight lang Maple="maple">Sockets:-GetHostName()</langsyntaxhighlight>
 
=={{header|Mathematica}} / {{header|Wolfram Language}}==
<syntaxhighlight lang Mathematica="mathematica">$MachineName</langsyntaxhighlight>
 
=={{header|MATLAB}}==
This is a built-in MATLAB function. "failed" is a Boolean which will be false if the command sent to the OS succeeds. "hostname" is a string containing the system's hostname, provided that the external command <tt>hostname</tt> exists.
 
<langsyntaxhighlight Matlablang="matlab">[failed,hostname] = system('hostname')</langsyntaxhighlight>
 
=={{header|mIRC Scripting Language}}==
<syntaxhighlight lang ="mirc">echo -ag $host</langsyntaxhighlight>
 
=={{header|Modula-3}}==
<langsyntaxhighlight lang="modula3">MODULE Hostname EXPORTS Main;
 
IMPORT IO, OSConfig;
Line 535 ⟶ 588:
BEGIN
IO.Put(OSConfig.HostName() & "\n");
END Hostname.</langsyntaxhighlight>
 
=={{header|MUMPS}}==
<langsyntaxhighlight MUMPSlang="mumps">Write $Piece($System,":")</langsyntaxhighlight>
 
=={{header|NetRexx}}==
<langsyntaxhighlight NetRexxlang="netrexx">/* NetRexx */
options replace format comments java crossref savelog symbols binary
 
say InetAddress.getLocalHost.getHostName
</syntaxhighlight>
</lang>
 
=={{header|NewLISP}}==
<langsyntaxhighlight NewLISPlang="newlisp">(! "hostname")</langsyntaxhighlight>
 
=={{header|Nim}}==
There are several ways to get the host name, for instance reading the environment variable HOSTNAME or calling the low level Posix function “gethostname”. The simplest way consists to use the function “getHostName” from module “nativeSockets”:
<lang Nim>import posix
<syntaxhighlight lang="nim">import nativesockets
const size = 64
echo getHostName()</syntaxhighlight>
var s = cstring(newString(size))
discard s.getHostname(size)
echo s</lang>
 
=={{header|Oberon-2}}==
Works with oo2c version 2
<langsyntaxhighlight lang="oberon2">
MODULE HostName;
IMPORT
Line 567 ⟶ 618:
Out.Object("Host: " + ProcessParameters.GetEnv("HOSTNAME"));Out.Ln
END HostName.
</syntaxhighlight>
</lang>
Output:
<pre>
Host: localhost.localdomain
</pre>
=={{header|Objective-C}}==
 
Cocoa / Cocoa Touch / GNUstep:
 
<lang objc>
NSLog(@"%@", [[NSProcessInfo processInfo] hostName]);
</lang>
 
Example Output:
 
<lang objc>
2010-09-16 16:20:00.000 Playground[1319:a0f] sierra117.local // Hostname is sierra117.local.
</lang>
 
=={{header|Objeck}}==
<langsyntaxhighlight lang="objeck">
use Net;
 
Line 597 ⟶ 635:
}
}
</syntaxhighlight>
</lang>
 
=={{header|Objective-C}}==
 
Cocoa / Cocoa Touch / GNUstep:
 
<syntaxhighlight lang="objc">
NSLog(@"%@", [[NSProcessInfo processInfo] hostName]);
</syntaxhighlight>
 
Example Output:
 
<syntaxhighlight lang="objc">
2010-09-16 16:20:00.000 Playground[1319:a0f] sierra117.local // Hostname is sierra117.local.
</syntaxhighlight>
 
=={{header|OCaml}}==
<syntaxhighlight lang ="ocaml">Unix.gethostname()</langsyntaxhighlight>
 
=={{header|Octave}}==
Similarly to [[Discover the Hostname#MATLAB|MATLAB]], we could call system command <tt>hostname</tt> to know the hostname. But we can also call the internal function <tt>uname()</tt> which returns a structure holding several informations, among these the hostname (nodename):
 
<syntaxhighlight lang ="octave">uname().nodename</langsyntaxhighlight>
 
=={{header|ooRexx}}==
Line 612 ⟶ 664:
A solution using ActiveX/OLE on Windows
 
<langsyntaxhighlight ooRexxlang="oorexx">say .oleObject~new('WScript.Network')~computerName</langsyntaxhighlight>
 
and one using the Windows environment variables
 
<langsyntaxhighlight ooRexxlang="oorexx">say value('COMPUTERNAME',,'environment')</langsyntaxhighlight>
 
===UNIX Platform===
Line 626 ⟶ 678:
:'''Note:''' The '''<tt>address command</tt>''' clause causes the contents of the literal string that follows it to be sent to the command shell.
 
<syntaxhighlight lang ooRexx="oorexx">address command 'hostname -f'</langsyntaxhighlight>
 
<langsyntaxhighlight ooRexxlang="oorexx">address command "echo $HOSTNAME"</langsyntaxhighlight>
 
Command output can also be captured by the program to allow further processing.
Line 634 ⟶ 686:
In the following examples output written to STDOUT/STDERR is piped into '''<tt>rxqueue</tt>''' which sends it in turn to a Rexx queue for further processing by the program:
 
<langsyntaxhighlight ooRexxlang="oorexx">/* Rexx */
address command "echo $HOSTNAME | rxqueue"
address command "hostname -f | rxqueue"
Line 641 ⟶ 693:
say q_~right(2)':' hn
end q_
</syntaxhighlight>
</lang>
 
A utility class is also provided as a wrapper around the external data queue:
 
<langsyntaxhighlight ooRexxlang="oorexx">/* Rexx */
qq = .rexxqueue~new()
address command "echo $HOSTNAME | rxqueue"
Line 653 ⟶ 705:
say q_~right(2)':' hn
end q_
</syntaxhighlight>
</lang>
 
=={{header|Oz}}==
<langsyntaxhighlight lang="oz">{System.showInfo {OS.getHostByName 'localhost'}.name}</langsyntaxhighlight>
 
=={{header|PARI/GP}}==
Running the <code>hostname</code> or <code>uname</code> program and capturing its output (the first line of output) in a string.
 
<langsyntaxhighlight lang="parigp">str = externstr("hostname")[1];
str = externstr("uname -n")[1];</langsyntaxhighlight>
 
=={{header|Pascal}}==
For Windows systems see the [[Hostname#Delphi | Delphi]] example.
On Unix systems, FreePascal has the function GetHostName:
<langsyntaxhighlight lang="pascal">Program HostName;
 
uses
Line 674 ⟶ 726:
begin
writeln('The name of this computer is: ', GetHostName);
end.</langsyntaxhighlight>
Output example on Mac OS X:
<pre>
Line 684 ⟶ 736:
 
{{libheader|Sys::Hostname}}
<langsyntaxhighlight lang="perl">use Sys::Hostname;
 
$name = hostname;</langsyntaxhighlight>
 
=={{header|Perl 6}}==
=={{header|Phix}}==
<lang perl6>my $host = qx[hostname];</lang>
<!--<syntaxhighlight lang="phix">(notonline)-->
<span style="color: #008080;">without</span> <span style="color: #008080;">js</span> <span style="color: #000080;font-style:italic;">-- (system_exec, file i/o)</span>
<span style="color: #008080;">constant</span> <span style="color: #000000;">tmp</span> <span style="color: #0000FF;">=</span> <span style="color: #008000;">"hostname.txt"</span><span style="color: #0000FF;">,</span>
<span style="color: #000000;">cmd</span> <span style="color: #0000FF;">=</span> <span style="color: #008080;">iff</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">platform</span><span style="color: #0000FF;">()=</span><span style="color: #004600;">WINDOWS</span><span style="color: #0000FF;">?</span><span style="color: #008000;">"hostname"</span><span style="color: #0000FF;">:</span><span style="color: #008000;">"uname -n"</span><span style="color: #0000FF;">)</span>
<span style="color: #0000FF;">{}</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">system_exec</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">sprintf</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"%s &gt; %s"</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">cmd</span><span style="color: #0000FF;">,</span><span style="color: #000000;">tmp</span><span style="color: #0000FF;">}),</span><span style="color: #000000;">4</span><span style="color: #0000FF;">)</span>
<span style="color: #004080;">string</span> <span style="color: #000000;">host</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">trim</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">get_text</span><span style="color: #0000FF;">(</span><span style="color: #000000;">tmp</span><span style="color: #0000FF;">))</span>
<span style="color: #0000FF;">{}</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">delete_file</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"hostname.txt"</span><span style="color: #0000FF;">)</span>
<span style="color: #0000FF;">?</span><span style="color: #000000;">host</span>
<!--</syntaxhighlight>-->
{{out}}
<pre>
"Pete-PC"
</pre>
 
=={{header|PHP}}==
<syntaxhighlight lang ="php">echo $_SERVER['HTTP_HOST'];</langsyntaxhighlight>
 
<syntaxhighlight lang ="php">echo php_uname('n');</langsyntaxhighlight>
 
{{works with|PHP|5.3+}}
<syntaxhighlight lang ="php">echo gethostname();</langsyntaxhighlight>
 
=={{header|PicoLisp}}==
This will just print the hostname:
<syntaxhighlight lang PicoLisp="picolisp">(call 'hostname)</langsyntaxhighlight>
To use it as a string in a program:
<langsyntaxhighlight PicoLisplang="picolisp">(in '(hostname) (line T))</langsyntaxhighlight>
 
=={{header|Pike}}==
<langsyntaxhighlight lang="pike">import System;
 
int main(){
write(gethostname() + "\n");
}</langsyntaxhighlight>
 
=={{header|PL/SQL}}==
<langsyntaxhighlight lang="plsql">SET serveroutput on
BEGIN
DBMS_OUTPUT.PUT_LINE(UTL_INADDR.GET_HOST_NAME);
END;</langsyntaxhighlight>
 
=={{header|Pop11}}==
<langsyntaxhighlight lang="pop11">lvars host = sys_host_name();</langsyntaxhighlight>
 
=={{header|PowerBASIC}}==
This retreives the localhost's name:
 
<syntaxhighlight lang ="powerbasic">HOST NAME TO hostname$</langsyntaxhighlight>
 
This attempts to retreive the name of an arbitrary machine on the network (assuming ipAddress& is valid):
 
<langsyntaxhighlight lang="powerbasic">HOST NAME ipAddress& TO hostname$</langsyntaxhighlight>
 
=={{header|PowerShell}}==
Windows systems have the <code>ComputerName</code> environment variable which can be used:
<syntaxhighlight lang ="powershell">$Env:COMPUTERNAME</langsyntaxhighlight>
Also PowerShell can use .NET classes and methods:
<syntaxhighlight lang ="powershell">[Net.Dns]::GetHostName()</langsyntaxhighlight>
 
=={{header|PureBasic}}==
{{works with|PureBasic|4.41}}
<langsyntaxhighlight PureBasiclang="purebasic">InitNetwork()
answer$=Hostname()</langsyntaxhighlight>
 
=={{header|Python}}==
{{works with|Python|2.5}}
<langsyntaxhighlight lang="python">import socket
host = socket.gethostname()</langsyntaxhighlight>
 
=={{header|R}}==
Sys.info provides information about the platform that R is running on. The following code returns the hostname as a string.
<langsyntaxhighlight Rlang="r">Sys.info()[["nodename"]]</langsyntaxhighlight>
Note that Sys.info isn't guaranteed to be available on all platforms. As an alternative, you can call an OS command.
<langsyntaxhighlight Rlang="r">system("hostname", intern = TRUE)</langsyntaxhighlight>
... or retrieve an environment variable
<syntaxhighlight lang="r">
<lang R>
env_var <- ifelse(.Platform$OS.type == "windows", "COMPUTERNAME", "HOSTNAME")
Sys.getenv(env_var)
</syntaxhighlight>
</lang>
 
=={{header|Racket}}==
<syntaxhighlight lang="racket">
<lang Racket>
#lang racket/base
(require mzlibracket/os)
(gethostname)
</syntaxhighlight>
</lang>
 
=={{header|Raku}}==
(formerly Perl 6)
<syntaxhighlight lang="raku" line>my $host = qx[hostname];</syntaxhighlight>
 
=={{header|REBOL}}==
<syntaxhighlight lang REBOL="rebol">print system/network/host</langsyntaxhighlight>
 
=={{header|REXX}}==
Line 771 ⟶ 840:
<br>Other names could be used for the 3rd argument.
<br><br>The &nbsp; ''computername'' &nbsp; is the same as the output for the &nbsp; '''hostname.exe''' &nbsp; program.
<langsyntaxhighlight REXXlang="rexx">say value('COMPUTERNAME',,"ENVIRONMENT")
say value('OS',,"ENVIRONMENT")</langsyntaxhighlight>
'''output''' (using Windows/XP)
<pre>
Line 781 ⟶ 850:
This REXX solution is for R4 and ROO under the Microsoft NT family of Windows (XP, Vista, 7, etc).
<br>Other names could be used for the 3rd argument.
<langsyntaxhighlight REXXlang="rexx">say value('COMPUTERNAME',,"SYSTEM")
say value('OS',,"SYSTEM")</langsyntaxhighlight>
 
===MS DOS (without Windows), userid===
Under Microsoft DOS (with no Windows), the closest thing to a name of a host would be the userid.
<syntaxhighlight lang ="rexx">say userid()</langsyntaxhighlight>
 
===MS DOS (without Windows), version of DOS===
But perhaps the name or version of the MS DOS system would be more appropriate than the userid.
<langsyntaxhighlight REXXlang="rexx">'VER' /*this passes the VER command to the MS DOS system. */</langsyntaxhighlight>
Each REXX interpreter has their own name (some have multiple names) for the environmental variables.
<br>Different operating systems may call their hostnames by different identifiers.
Line 800 ⟶ 869:
This solution is platform specific and uses features that are available to the Regina implementation of Rexx.
:Tested with Regina on Mac OS X. Should work on other UNIX/Linux distros.
<langsyntaxhighlight REXXlang="rexx">/* Rexx */
address command "hostname -f" with output stem hn.
do q_ = 1 to hn.0
say hn.q_
end q_
exit</langsyntaxhighlight>
 
=={{header|Ruby}}==
<langsyntaxhighlight lang="ruby">require 'socket'
host = Socket.gethostname</langsyntaxhighlight>
 
=={{header|Run BASIC}}==
<langsyntaxhighlight lang="runbasic">print Platform$ ' OS where Run BASIC is being hosted
print UserInfo$ ' Information about the user's web browser
print UserAddress$ ' IP address of the user</langsyntaxhighlight>
 
=={{header|Rust}}==
Works on windows and linux with crate <code>hostname</code> version 0.1.5
<syntaxhighlight lang="rust">fn main() {
match hostname::get_hostname() {
Some(host) => println!("hostname: {}", host),
None => eprintln!("Could not get hostname!"),
}
}</syntaxhighlight>
 
=={{header|Scala}}==
<langsyntaxhighlight lang="scala">println(java.net.InetAddress.getLocalHost.getHostName)</langsyntaxhighlight>
 
=={{header|Scheme}}==
{{works with|Chicken Scheme}}
<langsyntaxhighlight lang="scheme">(use posix)
(get-host-name)</langsyntaxhighlight>
{{works with|Guile}}
<syntaxhighlight lang ="scheme">(gethostname)</langsyntaxhighlight>
 
=={{header|Seed7}}==
Line 831 ⟶ 909:
which returns the hostname.
 
<langsyntaxhighlight lang="seed7">$ include "seed7_05.s7i";
include "socket.s7i";
 
Line 837 ⟶ 915:
begin
writeln(getHostname);
end func;</langsyntaxhighlight>
 
=={{header|Sidef}}==
<langsyntaxhighlight lang="ruby">var sys = frequire('Sys::Hostname');
var host = sys.hostname;</langsyntaxhighlight>
Or:
<langsyntaxhighlight lang="ruby">var host = `hostname`.chomp;</langsyntaxhighlight>
 
=={{header|Slate}}==
<syntaxhighlight lang ="slate">Platform current nodeName</langsyntaxhighlight>
 
=={{header|SNOBOL4Slope}}==
<syntaxhighlight lang="slope">(hostname)</syntaxhighlight>
<lang snobol4>
output = host(4,"HOSTNAME")
end</lang>
 
=={{header|Standard ML}}==
<lang sml>NetHostDB.getHostName ()</lang>
 
=={{header|Smalltalk}}==
{{works with|Smalltalk/X}}
<syntaxhighlight lang Smalltalk="smalltalk">OperatingSystem getHostName</langsyntaxhighlight>
 
=={{header|SNOBOL4}}==
<syntaxhighlight lang="snobol4">
output = host(4,"HOSTNAME")
end</syntaxhighlight>
 
=={{header|SQL}}==
{{works with|Oracle}}
<langsyntaxhighlight lang="sql">
select host_name from v$instance;
</syntaxhighlight>
</lang>
 
=={{header|SQL PL}}==
{{works with|Db2 LUW}}
<langsyntaxhighlight lang="sql pl">
SELECT HOST_NAME FROM SYSIBMADM.ENV_SYS_INFO
</syntaxhighlight>
</lang>
Output:
<pre>
Line 880 ⟶ 958:
 
</pre>
 
=={{header|Standard ML}}==
<syntaxhighlight lang="sml">NetHostDB.getHostName ()</syntaxhighlight>
 
=={{header|Swift}}==
Swift 3
<syntaxhighlight lang Swift="swift">print(ProcessInfo.processInfo.hostName)</langsyntaxhighlight>
 
=={{header|Tcl}}==
The basic introspection tool in TCL is the <tt>info</tt> command. It can be used to find out about the version of the current Tcl or Tk, the available commands and libraries, variables, functions, the level of recursive interpreter invocation, and, amongst a myriad other things, the name of the current machine:
 
<syntaxhighlight lang Tcl="tcl">set hname [info hostname]</langsyntaxhighlight>
 
=={{header|Toka}}==
<langsyntaxhighlight lang="toka">2 import gethostname
1024 chars is-array foo
foo 1024 gethostname
foo type</langsyntaxhighlight>
 
=={{header|TUSCRIPT}}==
<langsyntaxhighlight lang="tuscript">
$$ MODE TUSCRIPT
host=HOST ()
</syntaxhighlight>
</lang>
 
=={{header|UNIX Shell}}==
<syntaxhighlight lang ="bash">hostname</langsyntaxhighlight>
or
<syntaxhighlight lang ="bash">uname -n</langsyntaxhighlight>
 
=={{header|Ursa}}==
<langsyntaxhighlight lang="ursa">out (ursa.net.localhost.name) endl console</langsyntaxhighlight>
 
=={{header|Ursala}}==
The user-defined hostname function ignores its argument and returns a string.
<langsyntaxhighlight Ursalalang="ursala">#import cli
 
hostname = ~&hmh+ (ask bash)/<>+ <'hostname'>!</langsyntaxhighlight>
For example, the following function returns the square root of its argument
if it's running on host kremvax, but otherwise returns the square.
<langsyntaxhighlight Ursalalang="ursala">#import flo
 
creative_accounting = (hostname== 'kremvax')?(sqrt,sqr)</langsyntaxhighlight>
 
=={{header|VBScript}}==
<syntaxhighlight lang="vb">
<lang vb>
Set objNetwork = CreateObject("WScript.Network")
WScript.Echo objNetwork.ComputerName
</syntaxhighlight>
</lang>
 
=={{header|Vim Script}}==
<syntaxhighlight lang ="vim">echo hostname()</langsyntaxhighlight>
 
=={{header|Visual Basic}}==
{{works with|Visual Basic|5}}
{{works with|Visual Basic|6}}
{{works with|VBA|Access 97}}
{{works with|VBA|6.5}}
{{works with|VBA|7.1}}
<syntaxhighlight lang="vb">Option Explicit
 
Private Declare Function GetComputerName Lib "kernel32.dll" Alias "GetComputerNameW" _
(ByVal lpBuffer As Long, ByRef nSize As Long) As Long
Private Const MAX_COMPUTERNAME_LENGTH As Long = 31
Private Const NO_ERR As Long = 0
 
Private Function Hostname() As String
Dim i As Long, l As Long, s As String
s = Space$(MAX_COMPUTERNAME_LENGTH)
l = Len(s) + 1
i = GetComputerName(StrPtr(s), l)
Debug.Assert i <> 0
Debug.Assert l <> 0
Hostname = Left$(s, l)
End Function
 
Sub Main()
Debug.Assert Hostname() = Environ$("COMPUTERNAME")
End Sub</syntaxhighlight>
 
=={{header|V (Vlang)}}==
In Vlang, the "main()" entry point and declaration can be skipped in one file programs and when used like a script.
<syntaxhighlight lang="v (vlang)">import os
 
println(os.hostname())</syntaxhighlight>
 
=={{header|Wren}}==
Wren CLI doesn't currently expose a way to find the host name.
 
However, if Wren is embedded in (say) a suitable Go program, then we can ask the latter to get it for us.
 
<syntaxhighlight lang="wren">/* Hostname.wren */
class Host {
foreign static name() // the code for this is provided by Go
}
 
System.print(Host.name())</syntaxhighlight>
 
which we embed in the following Go program and run it.
{{libheader|WrenGo}}
<syntaxhighlight lang="go">/* Hostname.go */
package main
 
import (
wren "github.com/crazyinfin8/WrenGo"
"os"
)
 
type any = interface{}
 
func hostname(vm *wren.VM, parameters []any) (any, error) {
name, _ := os.Hostname()
return name, nil
}
 
func main() {
vm := wren.NewVM()
fileName := "Hostname.wren"
methodMap := wren.MethodMap{"static name()": hostname}
classMap := wren.ClassMap{"Host": wren.NewClass(nil, nil, methodMap)}
module := wren.NewModule(classMap)
vm.SetModule(fileName, module)
vm.InterpretFile(fileName)
vm.Free()
}</syntaxhighlight>
 
=={{header|zkl}}==
<syntaxhighlight lang ="zkl">System.hostname</langsyntaxhighlight>
Or open a server socket, which contains the hostname.
<syntaxhighlight lang ="zkl">Network.TCPServerSocket.open(8080).hostname</langsyntaxhighlight>
 
 
9,476

edits