os.exec
```golang
package main
import (
"fmt"
"os/exec"
)
func Subprocess(command string) {
cmd := exec.Command("bash","-c",command)
output,err := cmd.Output()
if err != nil {
fmt.Printf("Execute Shell:%s failed with error:%s", command, err.Error())
return
}
fmt.Printf("Execute Shell:%s finished with output:\n%s", command, string(output))
}
func main(){
fmt.Println(`*************************
1.d查看磁盘信息
2.f查看内存信息
3.ip查看IP信息
4.h查看主机名信息
*************************`)
var cmd string
fmt.Print("请输入你的选项:")
fmt.Scanln(&cmd)
switch cmd{
case "1" , "d":
Subprocess("df -h")
case "2" , "f":
Subprocess("free -h")
case "3" , "ip":
Subprocess("cip.cc")
case "4" , "h":
Subprocess("hostname")
default:
fmt.Println("请输入【1|2|3|4】")
}
}
```
```bash
#!/bin/bash
function ssh_cmd(){
expect -c "
#$1 = "HOST" , $2 = "USER", $3 = "PASSWD", $4 = cmd
set timeout 5;
spawn ssh $2@$1 $4 ;
expect {
\"password\" { send \"$3\r\" }
\"(yes/no)\" { send \"yes\r\"; exp_continue }
} ;
expect eof
"
}
ssh_cmd 192.168.111.128 root 211314 ls
```
```
#!/bin/bash
cmd="ALTER USER 'root'@'localhost' IDENTIFIED BY 'MHSQ@2022mysql';"
function mysqluse(){
expect << EOF
spawn mysql -uroot -p$(cat /mysql/mysqld/mysqld.log|grep password|awk '{print $13}')
expect "> " {send "$cmd\r"}
expect "> " {send "exit\r"}
EOF
echo ''
mysql -u root -pMHSQ@2022mysql -e "create user 'commute'@'%' identified by 'MHSQ@2022commute';"
mysql -u root -pMHSQ@2022mysql -e "grant all on *.* to ' commut'@'%';"
}
mysqluse
```