This is a creation in Article, where the information may have evolved or changed.
The go language is a new language that can be used in many places. The beginner go language, has done such a small tool, also is the practice practiced hand.
The function of this applet is to read the relevant configuration from the user-specified file and then perform the related operation according to the user's instruction.
The code is as follows:
Package Mainimport ("FMT" "Golang.org/x/crypto/ssh" "OS" "IO" "Bufio" "Encoding/csv" "Strings" "Container/list") var (num int) Func Main () {if Len (OS. Args) = = 1{fmt. Println ("Please enter filename parameter") Return}list: = ListNode (OS. ARGS[1]) fmt. PRINTLN ("Choose Statement to execute") fmt. SCANLN (&num) if Num <= list. Len () {fmt. PRINTLN ("You have selected", num) Ssh_to_do (list,num)}else {fmt. PRINTLN ("You entered the wrong!") Num: ", num)}}func ssh_to_do (list *list. list, num int) {if num! = 0 {i: = 1for node: = list. Front (); Node! = Nil; node = node. Next () {if i = = num {switch value: = node. Value. (type) {Case Batchnode:ssh_do (value. User, value. Password, value. Ip_port, value. CMD)}}i++}} else {for node: = list. Front (); Node! = Nil; node = node. Next () {switch Value: = node. Value. (type) {Case Batchnode:ssh_do (value. User, value. Password, value. Ip_port, value. CMD)}}}}func ListNode (FileName string) *list. List {list: = ReadNode (fileName) fmt. Printf ("Total%d data \ n", list.) Len ()) I: = 1for node: = list. Front (); Node! = Nil; node = node. Next () {switch Value: = node. Value. (type) {CAsE batchnode:fmt. Println (i, "", value.) String ())}i++}return list}func ssh_do (user, password, ip_port string, cmd string) {PassWd: = []ssh]. Authmethod{ssh. Password (Password)}conf: = ssh. Clientconfig{user:user, Auth:passwd}client, _: = ssh. Dial ("TCP", Ip_port, &conf) defer client.close () for {command: = cmdif session, Err: = Client.newsession (); err = = Nil { Defer session. Close () session. Stdout = OS. Stdoutsession.stderr = OS. Stderrsession.run (command) break}}}type batchnode struct {User stringpassword stringip_port stringcmd string}func (Batchnode *batchnode) String () string {return "ssh" + batchnode.user + "@" + batchnode.ip_port + "with password:" + Batchnode.password + " and run: "+ Batchnode.cmd}func ReadNode (fileName string) *list. List {inputfile, err: = OS. Open (fileName) if err! = Nil {fmt. Printf ("An error occurred while opening the file \ nthe file exists? \ n" \ nthe permission? \ n ") return list. New ()}defer inputfile.close () batchnodelist: = list. New () Inputreader: = Bufio. Newreader (Inputfile) for {inputstring, err: = InputreAder. ReadString (' \ n ') r: = csv. Newreader (Strings. Newreader (String (inputstring)))) for {record, err: = R.read () If err = = Io. EOF {break}if Err! = Nil {fmt. PRINTLN ("Error!!!", err) Continue}batchnode: = Batchnode{record[0], record[1], record[2], record[3]} Batchnodelist.pushback (batchnode)}if err = = Io. EOF {Break}}return Batchnodelist}
The contents of my file are:
Gavin,xxxx,192.168.1.128:22,echo Ok1 >>a.datagavin,xxxx,192.168.1.128:22,echo Ok2 >>a.datagavin,xxxx, 192.168.1.128:22,echo ok3 >>a.datagavin,xxxx,192.168.1.128:22,echo Ok4 >>a.data
Applet restricts the use of file content in CSV format, which is also easily handled by Excel
The results of the operation are as follows:
Total 4 Data
1 ssh gavin@192.168.1.128:22 with password:root and Run:echo Ok1 >>a.data
2 ssh gavin@192.168.1.128:22 with Password:root and Run:echo Ok2 >>a.data
3 ssh gavin@192.168.1.128:22 with Password:root and Run:echo ok3 >>a.data
4 ssh gavin@192.168.1.128:22 with Password:root and Run:echo Ok4 >>a.data
Please select the executed statement
1
You have chosen to be 1
Go to the online view:
If you enter 0, all configuration items are executed. In other words, if there is a fixed task, it can be easily controlled in bulk.