I am facing a problem while executing commands over ssh. please let me know how can i use/enable tty via Net::SSH2
How can i do same or enable tty using Net::SSH2 object ? Sample code which tries to execute command as sudo[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
usage: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);
Thanks.perl myssh.pl "sudo -u bob date" won't work perl myssh.pl "date" works and gives the date command output.
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |