Absrtact: In the work often need to log on to the server to do a series of operations, each input ssh xxx is always very troublesome. Why not consider writing a generic small script at this time?
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 (numintFunc Main() {IfLen (OS. Args) = =1{fmt. Println ("Please enter a filename parameter")Return}list: = ListNode (OS. args[1]) fmt. Println ("Please select executed statement") FMT. SCANLN (&num)If Num <= list. Len () {fmt. Println ("You selected", num) Ssh_to_do (list,num)}else {fmt. Println ("You entered the wrong!" Num: ", num)}}Func Ssh_to_do(List *list. List, numInt) {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(FileNameString)*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_portStringCmdString{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 Batchnodestruct {UserStringpasswordStringip_portStringcmdStringFunc (Batchnode *batchnode) String() string {Return"SSH" + Batchnode.user +"@" + Batchnode.ip_port +"With Password:" + Batchnode.password +"and run:" + batchnode.cmd}Func ReadNode(FileNameString)*List.List {inputfile, err: = OS. Open (FileName)If Err! =Nil {fmt. Printf ("There was an error while opening the file \ n Does the file exist? \ 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:
Gavinxxxx,192.168.1.128: 22,EchoOk1 >>A. DataGavinxxxx,192.168.1.128: 22,EchoOk2 >>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 [email protected]:22 with password:root and Run:echo Ok1 >>a.data
2 ssh [email protected]:22 with password:root and Run:echo Ok2 >>a.data
3 ssh [email protected]:22 with password:root and Run:echo ok3 >>a.data
4 SSH [email protected]: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.
https://my.oschina.net/jiangmitiao/blog/753883
Use the Go language to perform operations in a flexible bulk SSH login server