I am using a handlerfunc k8sHandler(w http.ResponseWriter, r *http.Request)
, which has a for loop inside it, basically it loops over a list of hosts in a file and runs a script on each host. So, I wanted to print the output in the browser when it connects to each host like.. currently I using fmt.Fprintf(w, "Connecting to host", host)
Connecting to host host1
Connecting to host host2
func k8sHandler(w http.ResponseWriter, r *http.Request) {
user := "root"
key_path := "/home/mrawat/.ssh/id_rsa"
f, err := os.Open("/home/mrawat/golang/k8s-main/files/servers_list.txt")
if err != nil {
log.Fatal(err)
}
// remember to close the file at the end of the program
defer f.Close()
// read the file line by line using scanner
scanner := bufio.NewScanner(f)
for scanner.Scan() {
// do something with a line
host := fmt.Sprintf("%s\n", scanner.Text())
host = removeSpace(host)
fmt.Fprintf(w, "Connecting to host", host)
w.(http.Flusher).Flush()
display(w, "upload", "Upload successful.")
privPEM, err := ioutil.ReadFile("/home/mrawat/.ssh/id_rsa")
sshConf, err := scp.NewSSHConfigFromPrivateKey("root", privPEM)
port := ":22"
hostname := host + port
fmt.Println(hostname)
scpClient, err := scp.NewClient(hostname, sshConf, &scp.ClientOption{})
defer scpClient.Close()
// Do the file transfer without timeout/context
err = scpClient.CopyFileToRemote("/home/mrawat/golang/k8s-main/k8s_bash.sh", "/tmp/k8s_bash.sh", &scp.FileTransferOption{})
// Do the file copy with timeout, context and file properties preserved.
// Note that the context and timeout will both take effect.
fo := &scp.FileTransferOption{
Timeout: 30 * time.Second,
PreserveProp: true,
}
err = scpClient.CopyFileToRemote("/path/to/local/file", "/path/at/remote", fo)
name, err := os.Hostname()
if err != nil {
panic(err)
}
key_path = "/home/mrawat/.ssh/id_rsa"
client, session, err := connectToHost(user, host, key_path)
if err != nil {
panic(err)
}
out, err := session.CombinedOutput("/bin/bash /tmp/k8s_bash.sh > /tmp/k8s-config.yaml")
if err != nil {
panic(err)
}
fmt.Println(string(out))
//display_k8sup()
client.Close()
}
if err := scanner.Err(); err != nil {
log.Fatal(err)
}
}
Likewise.. it is printing the above output but only once the complete loop is completed. It is not printing it in runtime one after another.. Is there a way to print the output in runtime..??