Golang : Execute shell command
A programming language will not be complete if there is no way for the developer to execute shell command. In this tutorial, we will show you how to execute a shell command in Go language.
We will use the dig
command to retrieve the name servers of a website and output them.
Very often, dig
command should be installed by default on most *nix based operating system. In case there is no dig
command available, you can install it with the sudo yum install bind-utils
or search for equivalent installation instruction for installing the BIND package. For Windows BIND package please download from http://www.isc.org
exec.go
package main
import (
"fmt"
"os/exec"
)
func main() {
cmd := exec.Command("dig", "any", "google.com")
out, err := cmd.Output()
if err != nil {
fmt.Println(err.Error())
return
}
fmt.Print(string(out))
}
Execute > go run exec.go
will produce the following output
]# go run exec.go
;; Truncated, retrying in TCP mode.
; <<>> DiG 9.8.2rc1-RedHat-9.8.2-0.23.rc1.el6_5.1 <<>> any google.com
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 52756
;; flags: qr rd ra; QUERY: 1, ANSWER: 24, AUTHORITY: 0, ADDITIONAL: 0
;; QUESTION SECTION:
;google.com. IN ANY
;; ANSWER SECTION:
google.com. 213 IN A 74.125.226.67
google.com. 213 IN A 74.125.226.70
google.com. 213 IN A 74.125.226.68
google.com. 213 IN A 74.125.226.78
google.com. 213 IN A 74.125.226.69
google.com. 213 IN A 74.125.226.66
google.com. 213 IN A 74.125.226.65
google.com. 213 IN A 74.125.226.72
google.com. 213 IN A 74.125.226.71
google.com. 213 IN A 74.125.226.73
google.com. 213 IN A 74.125.226.64
google.com. 213 IN AAAA 2607:f8b0:4006:808::1007
google.com. 513 IN MX 50 alt4.aspmx.l.google.com.
google.com. 21513 IN SOA ns1.google.com. dns-admin.google.com. 2014021800 7200 1800 1209600 300
google.com. 3513 IN TXT "v=spf1 include:_spf.google.com ip4:216.73.93.70/31 ip4:216.73.93.72/31 ~all"
google.com. 513 IN MX 30 alt2.aspmx.l.google.com.
google.com. 21513 IN NS ns4.google.com.
google.com. 21513 IN NS ns2.google.com.
google.com. 21513 IN NS ns3.google.com.
google.com. 513 IN MX 10 aspmx.l.google.com.
google.com. 21513 IN TYPE257 \# 19 0005697373756573796D616E7465632E636F6D
google.com. 513 IN MX 20 alt1.aspmx.l.google.com.
google.com. 513 IN MX 40 alt3.aspmx.l.google.com.
google.com. 21513 IN NS ns1.google.com.
;; Query time: 21 msec
;; SERVER: 8.8.4.4#53(8.8.4.4)
;; WHEN: Tue May 13 04:43:46 2014
;; MSG SIZE rcvd: 577
You can change the following code
cmd := exec.Command("dig","any","google.com")
to
application_name = "dig"
arg_0 = "any"
arg_1 = "google.com"
cmd := exec.Command(application_name,arg_0,arg_1)
for readability purpose. Also, arguments must be separated in this manner instead of lumping everything into a string. Hope this tutorial will help you in learning more about Golang.
References:
By Adam Ng
IF you gain some knowledge or the information here solved your programming problem. Please consider donating to the less fortunate or some charities that you like. Apart from donation, planting trees, volunteering or reducing your carbon footprint will be great too.
Advertisement
Tutorials
+18.4k Golang : Read binary file into memory
+12.5k Elastic Search : Return all records (higher than default 10)
+24.6k Golang : Time slice or date sort and reverse sort example
+4.9k Golang : A program that contain another program and executes it during run-time
+10.7k Golang : How to unmarshal JSON inner/nested value and assign to specific struct?
+8.4k Golang : Implementing class(object-oriented programming style)
+14.3k Golang : Convert IP version 6 address to integer or decimal number
+29.5k Golang : Login(Authenticate) with Facebook example
+8.2k Golang : Tell color name with OpenCV example
+6k Golang : Extract unicode string from another unicode string example
+14.6k How to automatically restart your crashed Golang server
+20.8k Golang : Saving private and public key to files