CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Intermediate:
calling ctrl + c from w/in a program

 



doughyi8u
New User

Feb 24, 2015, 2:46 PM

Post #1 of 6 (3637 views)
calling ctrl + c from w/in a program Can't Post

I have the following code:


Code
#!/usr/bin/perl -w 

use Net::IP;

$addr = $ARGV[0];
my $ip = new Net::IP("$addr") or die "Couldn't stat ip address";

do {
$addrs = $ip->ip();
system "hping3 -S -p 80 $addrs";
} while (++$ip);

return;


This runs the hping coommand and keeps going. How do I send a ctrl+c from within the program and start another iteration?


FishMonger
Veteran / Moderator

Feb 24, 2015, 5:48 PM

Post #2 of 6 (3634 views)
Re: [doughyi8u] calling ctrl + c from w/in a program [In reply to] Can't Post

Instead of using system to run an external command, IMO, it would be better and easier to use Net::Ping which is a core module.

http://search.cpan.org/~smpeters/Net-Ping-2.41/lib/Net/Ping.pm


(This post was edited by FishMonger on Feb 24, 2015, 5:48 PM)


doughyi8u
New User

Feb 25, 2015, 10:56 AM

Post #3 of 6 (3627 views)
Re: [doughyi8u] calling ctrl + c from w/in a program [In reply to] Can't Post

I'm really just experimenting. I was actually trying to find a host that would return the netmask ( hping3 --icmp-gw <host> ). I did see that you could create a child process and kill it after it's run. I'm not exactly a veteran programmer and have never used child processes. Is this what I should do or can you think of a better way to do this?

Thanks for the help.


Laurent_R
Veteran / Moderator

Feb 25, 2015, 11:48 PM

Post #4 of 6 (3610 views)
Re: [doughyi8u] calling ctrl + c from w/in a program [In reply to] Can't Post

Sure you can spawn a child process and let it die, but I haven't seen anything in what you said previously that would require a child process. For what purpose do you want that?


doughyi8u
New User

Feb 26, 2015, 4:37 PM

Post #5 of 6 (3600 views)
Re: [doughyi8u] calling ctrl + c from w/in a program [In reply to] Can't Post

The hping command, once called, will run until a crtl+c is called. I don't know how to call the ctrl+c to terminate the process. That's the reason for calling a child process and terminating it to get it ready for the next iteration.

In Reply To


FishMonger
Veteran / Moderator

Feb 27, 2015, 7:16 AM

Post #6 of 6 (3581 views)
Re: [doughyi8u] calling ctrl + c from w/in a program [In reply to] Can't Post

Instead of using the system function, you could use either IPC::Open2 or IPC::Open3 which returns the PID of the child process. When you want to kill the process, you would use the kill function.

http://search.cpan.org/~rjbs/perl-5.18.4/ext/IPC-Open3/lib/IPC/Open2.pm
http://search.cpan.org/~rjbs/perl-5.18.4/ext/IPC-Open3/lib/IPC/Open3.pm
http://perldoc.perl.org/functions/kill.html

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives