0

I wrote a kill_all_ssh CLI program in Linux using C code, and use a secure shell to connect with the ssh server. When I issue the CLI command to close all ssh sessions, I can get a prompt that said ssh session closed as follow:

cc@server:~$ ./cli-kill-all-ssh
Connection to 192.168.1.102 closed by remote host.
Connection to 192.168.1.102 closed.

I also have a program that can change the IP address of the ssh server, but when I change the IP address of the ssh server, I lost the connection with the server, and the ssh console hang without any response.

I try to fork a child to kill all ssh session and change IP address in the parent at the same time, but it doesn't work.

Can anyone give me a hint?

Thanks in advance!

charles.cc.hsu
  • 689
  • 11
  • 15

1 Answers1

0

if you want to kill all ssh connections AND change the IP of the server, put both commands in a single script and run with the nohup command so killing the ssh connections does not kill the script.
nohup /path/to/script/killscript.sh &
may be what you need.

General Foch
  • 333
  • 1
  • 7
  • Thanks your suggestion, I'll take a try and give you my testing result. – charles.cc.hsu Jan 06 '17 at 00:53
  • I follow your information and find some link about implement nohup in C code http://stackoverflow.com/questions/10985544/using-c-to-send-an-exec-process-to-the-background – charles.cc.hsu Jan 06 '17 at 01:06
  • I found the change IP address and kill all ssh session commands can not be daemonized. If you daemonize these two commands, they both do not work properly. – charles.cc.hsu Jan 10 '17 at 13:59
  • not sure what you mean by daemonized since typically a daemon is a service running in the background which is not what we were discussing. – General Foch Jan 10 '17 at 15:45
  • No, I does not work. I put both commands in one script, and run as your suggestion. the IP address changed but the ssh console hang without response. After one minute, the console response `packet_write_wait: Connection to 192.168.1.102: Broken pipe` – charles.cc.hsu Jan 11 '17 at 06:39
  • Here is a link [How to Daemonize in Linux](http://www.itp.uzh.ch/~dpotter/howto/daemonize) in C code – charles.cc.hsu Jan 11 '17 at 11:52
  • what order did you run the commands in your script ? you should kill the ssh connections first. – General Foch Jan 11 '17 at 15:41
  • `cli-kill-all-ssh` `cli-set-ip enp6s0 192.168.1.103` I kill all ssh first as you said – charles.cc.hsu Jan 12 '17 at 05:41