I'm attempting to use Net::OpenSSH to connect to a remote linux host and execute a tcpdump command in the background while then executing a ping, stopping the tcpdump and printing the result to the screen. I realize that each command runs in it's own session but I thought I could use nohup and & to create a background process that wouldn't exit. I know that Expect can be used for a more interactive process but I was wondering what the best/recommended way to do this would be (without using Expect).
#!/usr/bin/perl
use strict;
use warnings;
use Data::Dumper;
use Net::OpenSSH;
my $username = 'username';
my $password = 'password';
my $params;
my $session_obj;
$session_obj = Net::OpenSSH->new("10.2.200.12", user => $username, pas
+sword => $password, strict_mode => 0,master_opts => [ -o => "StrictHo
+stKeyChecking=no"]);
$session_obj->error and die "ssh failed: " . $session_obj->error;
print Dumper($session_obj);
# Each command runs in its own session
my ($out,$err) = $session_obj->capture2('nohup /usr/sbin/tcpdump -i et
+h1 -s 1500 -w /tmp/tmp.pcap &');
print "THE OUT IS $out\nTHE ERROR IS $err\n";
($out,$err) = $session_obj->capture2('ping -I eth1 -c 3 192.168.107.25
+4');
print "THE OUT IS $out\nTHE ERROR IS $err\n";
($out,$err) = $session_obj->capture2('killall tcpdump');
print "THE OUT IS $out\nTHE ERROR IS $err\n";
($out,$err) = $session_obj->capture2('tcpdump -r /tmp/tmp.pcap');
print "THE OUT IS $out\nTHE ERROR IS $err\n";
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.