I am facing a problem while executing commands over ssh. please let me know how can i use/enable tty via Net::SSH2

[root@ip:/tmp] ssh root@ip "sudo -u bob date" root@ip's password: sudo: sorry, you must have a tty to run sudo [root@ip:/tmp] but ssh -t root@ip "sudo -u bob date" solves the problem
How can i do same or enable tty using Net::SSH2 object ?

Sample code which tries to execute command as sudo
use Carp qw(carp confess croak); use Net::SSH2; my $host = "ip"; my $user = "root"; my $pass = "mypassword"; # Connect to host my $ssh = Net::SSH2->new(); if (! $ssh->connect($host)) { print "Failed connection to $host\n"; exit(1); } if ($pass ne "") { if (! $ssh->auth_password($user,$pass)) { print "FAILED SCP password authentication for $username to $host\n"; exit(1); } } else { print "FAILED SCP not the correct credentials provided in config file\ +n"; exit(1); } my $chan = $ssh->channel(); $chan->exec($ARGV[$0]); $stdout = undef; $chan->read( $stdout, 10000 ); $chan->close(); print $stdout; $ssh->disconnect(); exit(0);
usage:
perl myssh.pl "sudo -u bob date" won't work perl myssh.pl "date" works and gives the date command output.
Thanks.

In reply to tty Problem while executing command's with sudo over Net::SSH2 by dipesh777

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • 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:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.