magom001
magom001

Reputation: 660

Golang ssh interrupt signal

I am working on a simple ssh client that connects to multiple remote hosts and allows the reading of log files. I manage to connect to and run commands on my remote machine

stdin, err := session.StdinPipe()

if err != nil {
    log.Fatalf("Failed to establish stdin pipe: %v", err)
}

session.Stdout = os.Stdout
session.Stderr = os.Stderr

err = session.Shell()

if err != nil {
    log.Fatal(err)
}

reader := bufio.NewReader(os.Stdin)
for {
    text, _ := reader.ReadString('\n')

    if text == "exit\n" {
        break
    }

    if text == "br\n" {
        // ??? HOW ???
        continue
    }

    _, err = fmt.Fprintf(stdin, "%s", text)

    if err != nil {
        log.Fatal(err)
    }
}

When I run tail -f access.tskv I get the output on my stdin. My problem is that I cannot figure out how to send an interrupt signal to the shell on the remote machine.

Any ideas?

Upvotes: 3

Views: 1257

Answers (2)

magom001
magom001

Reputation: 660

session.Signal(ssh.SIGINT) should be doing the trick according to the API documentation. But for some reason no signal is triggered at the other end. It seems the problem is with sshd on ubuntu 18.04

https://github.com/golang/go/issues/16597#issuecomment-548053530

Upvotes: 1

b_o
b_o

Reputation: 91

In order to send a signal to a process (assuming your remote hosts are Unix-based), we need to find the process ID of the process we wish to send to signal to by using the following command to find it:

ps aux | grep '[process-name]'

reference: https://www.cyberciti.biz/faq/linux-find-process-name/

Once you find the process and its ID (normally it's integers), you can then run the following command to send a signal to it

kill -SIGINT [PID]

reference: https://bash.cyberciti.biz/guide/Sending_signal_to_Processes

Hope this helps!

Upvotes: 0

Related Questions