📜 ⬆️ ⬇️

Go for system administrators. Practical examples. Part 0

Hello, my name is Vitaly and I am a monkey practitioner - it's better for me to see and copy once, than read abstract manuals a hundred times. For a long time I was an ordinary system administrator - I wrote scripts on CMD / BAT, and even on sh (using busybox for Windows). But one day I was missing the usual shell, and I decided to write my own RPC server for myself, but so that it works with a minimum of system components, and was understandable, and was multi-threaded and contained a minimum of lines of code. I dismissed Java and other OOP, because for professionals it’s too abstract, and I need to install the environment for execution on the target computer, and I, as an admin, should update it. I've been looking at perl for a long time, but I'm afraid of dynamic typing. In the article, I will tell you how a person who is not familiar with programming can solve some system administration problems with Go .

I assume that you have mastered "Quick start - we program on Go under Windows - setting up the Environment" , you have experience writing simple scripts. And I lied. The target machine may require Microsoft Visual C ++ .

To begin, we will try to turn a simple script into an application on Go. For example, take test.bat :

@echo off set URL1="ftp://user:pass@88.88.88.88/test.zip" set URL2="ftp://user1:pass1@99.99.99.99/exchange/test.zip" set SAVE_PATH=".\test\test.zip" echo   curl %URL1% -o %SAVE_PATH% if %errorlevel% NEQ 0 ( echo       : %errorlevel% curl %URL2% -o %SAVE_PATH% ) 

A moment of love for Microsoft. If I want to check if the file was downloaded from the second source, then I have to use GOTO , because inside IF and FOR % errorlevel% and % time% remain the same as before calling IF and FOR .
')
This is how our Go script will look like:

 package main import ( "fmt" "os" "os/exec" ) func main() { url1 := "ftp://user:pass@88.88.88.88/test.zip" url2 := "ftp://user1:pass1@99.99.99.99/exchange/test.zip" out_file := ".\\test\\test.zip" // Go    ,     windows   . err := start_curl(url1, out_file) if err != nil { fmt.Printf("      : %v\r\n", err) err = start_curl(url2, out_file) if err != nil { fmt.Printf("      : %v\r\n", err) os.Exit(1) } } fmt.Printf("  \r\n") } func start_curl(url string, out_file string) error { cmd := exec.Command("curl", url, "-o", out_file) err := cmd.Run() return err } 

We started the run, but for debugging it would be nice to see what the curl issues in stderr / stdout. By the way, curl gives everything to stderr:

 func start_curl(url string, out_file string) error { cmd := exec.Command("curl", url, "-o", out_file) //cmd.Stdout = os.Stdout //     . cmd.Stderr = os.Stderr err := cmd.Run() return err } 

On error, the start_curl () function returns something like “exit code 7”. And we would like to get the return code as a number. We can cut the string “exit_code” and convert the string “7” to number 7. To do this, you will have to import the “strings” and “strconv” packages. But there is a simpler and less understandable way:

 package main import ( "fmt" "os" "os/exec" "syscall" ) func main() { url1 := "ftp://user:pass@88.88.88.88/test.zip" url2 := "ftp://user1:pass1@99.99.99.99/exchange/test.zip" out_file := ".\\test\\test.zip" int_err := start_curl(url1, out_file) if int_err != 0 { fmt.Printf("      : %d\r\n", int_err) int_err = start_curl(url2, out_file) if int_err != 0 { fmt.Printf("      : %d\r\n", int_err) os.Exit(int_err) } } fmt.Printf("  \r\n") } func start_curl(url string, out_file string) int { var exit_code int cmd := exec.Command("curl", url, "-o", out_file) //cmd.Stdout = os.Stdout //cmd.Stderr = os.Stderr err := cmd.Run() if err != nil { exit_code = int(err.(*exec.ExitError).Sys().(syscall.WaitStatus).ExitCode) } else { exit_code = 0 } return exit_code } 

That's all for today. The compiler will collect us a ready-made exe-file. It’s not necessary to be afraid of big size (several megabytes). This file will contain the minimum runtime and all the necessary packages. The memory consumption of an application on Go is two times less than that of perl or python (unless of course we are talking about small applications). If the article is of interest to someone, in the comments indicate which of the topics you would like to consider:

Source: https://habr.com/ru/post/253039/


All Articles