3

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?

Dexygen
  • 12,287
  • 13
  • 80
  • 147
magom001
  • 608
  • 6
  • 16
  • Does this answer your question? [How to send control+c from a bash script?](https://stackoverflow.com/questions/5789642/how-to-send-controlc-from-a-bash-script) – Dexygen Apr 24 '20 at 11:59
  • @Dexygen not in the slightest – magom001 Apr 24 '20 at 12:26
  • Ok maybe it's not a close reason but certainly there are ample clues in there: get the PID for the shell process you're running, then KILL it. Maybe modify your general approach to so it can be done this way? – Dexygen Apr 24 '20 at 18:37
  • @Dexygen my remote host is executing a foreground task, namely "tail -f nginx.og". How do you suggest I execute another bash command like `ps aux` without first sending a SIGINT to the `tail` ? – magom001 Apr 25 '20 at 09:30
  • It may seem a bit messy, but I guess you could always open another shell afterwards, merely to get the PID. – Dexygen Apr 26 '20 at 02:20
  • There are other ideas here: https://unix.stackexchange.com/questions/102956/how-to-run-a-command-in-the-background-with-a-delay/102961 It's an interesting problem to me – Dexygen Apr 26 '20 at 11:29

2 Answers2

1

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

magom001
  • 608
  • 6
  • 16
0

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!

b_o
  • 91
  • 5
  • it is not even close to my question – magom001 Apr 25 '20 at 09:28
  • Thanks for the comment, i guess couldn’t really comprehend what you are trying to do here. Maybe some further clarification on exactly what you are trying to do step by step could paint a better picture, or even just the use case you are trying to achieve. – b_o Apr 25 '20 at 18:07