Search code examples
perlsshsudottytraceroute

PERL ssh script tty issue


I am trying to write a PERL script to ssh to a remote machine and run some tcptraceroutes. I managed to handle the SSH part fine. However tcptraceroute command requires sudo, and here is where I ran into an issue. Example:

my (@traces, $stderr, $exit) = $sshCali->cmd(tty => 1, 
"sudo tcptraceroute $endpoint", interactive => 1, debug => 1);

When I use tty => 1 system complaints with this error: "not a tty" When I do not use tty => 1 then I get this: "sudo: sorry, you must have a tty to run sudo" Any help will be greatly appreciated. Here is my code:

#!/usr/bin/perl
use strict;
use warnings; 
use Net::SSH::Perl;

#vars
my $endpoint = 172.31.100.1;
my @traces;
my $sshCali;
my $hostCali = 'remotemachine';
my $cmd = 'sudo traceroute';

warn "Starting SSH Services:...\n";

$sshCali = Net::SSH::Perl->new($hostCali, interactive => 1, debug => 0) or die "Couldnt establish connection!";

$sshCali -> login("user"); # for non-interactive mode;

print "here\n";

my (@traces, $stderr, $exit) = $sshCali->cmd("sudo tcptraceroute $endpoint", interactive => 1, debug => 1);

#my (@traces, $stderr, $exit) = $sshCali->cmd(tty => 1,"$cmd $endpoint", interactive => 1, debug => 1);

print STDERR "STDERR: $stderr\n" if $stderr;
print "command exit w/ code: $exit\n";

$sshCali -> cmd("exit");


print "NOW ..... Printing the array: TRACES\n";
print @traces;

Thanks in advance.


Solution

  • It is entirely possible to install a module in your home directory and call it inside your script. See this node on Perlmonks for exact steps: Installing Perl modules in home directory