Simple implementation of input commands and executing switch commands in two steps.
- Enter the account number and password to perform the replacement. You can enter multiple accounts and passwords at once. For the convenience of operation, the input format is specified. Such as username; host IP; password | username; host IP; password. Example admin;192.168.56.10;h3csw1|admin;192.168.56.11;h3csw2
- Enter the command to be executed to; split. For example system-view;dis cu;
There are problems:
- Not flexible enough. The input method limit is too dead, and there may be errors in entering special characters.
- Too simple.
- Simple function.
However, my goal has been achieved, and I mainly understand the use of ssh.
package main import ( "bufio" "fmt" "/x/crypto/ssh" "log" "os" "strings" "sync" ) //Get the correspondence between account and passwordtype HostPassword struct { Host string Username string Password string } var ( a,b string //Temporary storage of variables commands = []string{} //Execute the command group hp []HostPassword //Save account and password wg //Execute goroutine ) func main() { //1. Select the switch //2. Enter the command to be executed //3. Establish a session connection //4. Create a new session and execute the command //1. Select Operation Switch // 1.1 Enter the switch to be executed ("Please enter the switch account and password for the planned execution command. The account password is used directly | splitting, and use it between multiple account passwords; splitting, such as admin;192.168.56.10;h3csw1|admin;192.168.56.11;h3csw2") _, err := (&a) if err != nil { ("Input error:",err) } ("Please enter the command line to be executed to; number interval") //1.1.1 Cutting switch command switchgroups := (a, "|") length := len(switchgroups) hp = make([]HostPassword,length) for i,singleswitch := range switchgroups{ hp[i]=HostPassword{} switchsplit := (singleswitch, ";") hp[i].Username=switchsplit[0] hp[i].Host=switchsplit[1] hp[i].Password=switchsplit[2] } // 1.2 Enter the command to be executed input := () b, err := ('\n') if err != nil { ("Input Error",err) } commands = (b, ";") //2. Perform switch operations err = SshSwitch(hp) if err != nil { (err) } // Synchronous waiting () } //Create an ssh connectionfunc SshSwitch(hostpasswords []HostPassword) (error){ //Change to get the account and password of hostpasswords for i,_ := range hp{ //Add a synchronization group, goroutin will be executed below (1) config := &{ Config: { Ciphers: []string{"aes128-ctr", "aes192-ctr", "aes256-ctr", "aes128-gcm@", "arcfour256", "arcfour128", "aes128-cbc", "3des-cbc", "aes192-cbc", "aes256-cbc"}, }, //A lot of encryption methods have been added to deal with different password rules User: hp[i].Username, Auth: []{ (hp[i].Password), }, HostKeyCallback: (), //This is equivalent to executing nil, but it is not safe } client, err := ("tcp",hp[i].Host+":22", config) if err != nil { ("Sh connection establishment error:",err) return err } //Execute goroutine, but no error was returned. go HandleSession(client, commands,&wg) } return nil } //Create session and execute commands.func HandleSession(client *,commands []string,wg *) error { //Create session session, err := () if err != nil { ("Error creating session",err) return err } //Delayed closing session defer () //How to set terminalmodes modes := { : 0, // disable echoing ssh.TTY_OP_ISPEED: 14400, // input speed = 14.4kbaud ssh.TTY_OP_OSPEED: 14400, // output speed = 14.4kbaud } //Create a pseudo-terminal err = ("xterm",80,40,modes) if err != nil { ("Error creating requestpty",err) return err } //The standard input to set session is stdin stdin, err := () if err != nil { ("Input Error",err) return err } //Set the standard output and error output of session are, os, stderr. It is output to the background = = err = () if err != nil { ("Error creating shell",err) return err } //Execute the commands in turn for _, cmd := range commands { (cmd) _, err = (stdin, "%s\n", cmd) if err != nil { ("Error writing to stdin",err) return err } } //Execute waiting err = () if err != nil { ("Waiting for session error",err) return err } //Reduce the number of synchronized groups () return nil }
This is the article about Golang's implementation of SSH execution switch operations. For more related content on Golang SSH execution switches, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!