LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices


Reply
  Search this Thread
Old 09-30-2010, 04:58 PM   #1
jhyland87
LQ Newbie
 
Registered: Dec 2008
Posts: 25

Rep: Reputation: 15
Perl script to SSH into a server, and sudo


Hey guys, so basically, I need to create a perl script to SSH into a list of servers, and run a command as sudo. I tested with this script:

Code:
#!/usr/bin/perl

use Net::SSH::Perl;

$host = "server.name.com";
$user = "jhyland";
$pass = "passwird";

#$ssh = Net::SSH::Perl->new($host, debug => 1, use_pty => 1);
$ssh = Net::SSH::Perl->new($host, use_pty => 1);

$ssh->login($user, $pass);

$cmd = "sudo su -";
$ssh->cmd($cmd);

$cmd = "whoami";
($stdout, $stderr, $exit) = $ssh->cmd($cmd);

print $stdout;
But it would spit out "jhyland" I learned that its because of the TTY limit, which you cant change.

So I tried to use Net::SSH::Expect.. copied from http://search.cpan.org/~bnegrao/Net-...SSH/Expect.pod

Code:
#!/usr/bin/perl

use Net::SSH::Expect;

#
# You can do SSH authentication with user-password or without it.
#

# Making an ssh connection with user-password authentication
# 1) construct the object
my $ssh = Net::SSH::Expect->new (
    host => "server.name.com", 
    password=> 'test', 
    user => 'jhyland', 
    raw_pty => 1
);

# 2) logon to the SSH server using those credentials.
# test the login output to make sure we had success
my $login_output = $ssh->login();
if ($login_output !~ /Last login/) {
    die "Login has failed. Login output was $login_output";
}

# - now you know you're logged in - 

# 3) you should be logged on now. Test if you received the remote prompt:
($ssh->read_all(2) =~ />\s*\z/) or die "where's the remote prompt?";

# - now you know you're logged in - #

# disable terminal translations and echo on the SSH server
# executing on the server the stty command:
$ssh->exec("stty raw -echo");

# runs arbitrary commands and print their outputs 
# (including the remote prompt comming at the end)
my $ls = $ssh->exec("ls -l /");
print($ls);

my $who = $ssh->exec("who");
print ($who);

# When running a command that causes a huge output,
# lets get the output line by line:
$ssh->send("find /");   # using send() instead of exec()
my $line;
# returns the next line, removing it from the input stream:
while ( defined ($line = $ssh->read_line()) ) {
    print $line . "\n";  
}

# take a look in what is immediately available on the input stream
print $ssh->peek(0);    # you'll probably see the remote prompt
 
# the last read_line() on the previous loop will not include the
# remote prompt that appears at the end of the output, because the prompt
# doesn't end with a '\n' character. So let's remove the remainder
# prompt from the input stream:
$ssh->eat($ssh->peek(0));  # removes whatever is on the input stream now

# We can also iterate over the output in chunks,
# printing everything that's available at each 1 second:
$ssh->send ("find /home");
my $chunk;
while ($chunk = $ssh->peek(1)) { # grabs chunks of output each 1 second
    print $ssh->eat($chunk);
}

# closes the ssh connection
$ssh->close();
And that doesnt even successfully login. I doubt that the NET::SSH::Expect is my fault, due to the fact that script actually has a typo in it right from the cpan website, so I doubt it was ever even tested.

Any ideas guys?

basically.. I need to shell in, then either sudo httpd -S, or sudo su -; httpd -S
 
Old 10-01-2010, 02:10 PM   #2
acid_kewpie
Moderator
 
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 43,417

Rep: Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985
Can I suggest not doing this this way at all? Check out a tool like func, which will let you run arbitrary scripts on a remote server over a secure authenticated connection. No SSH required. Check it out.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
BASH - question about using sudo in SSH script xmichielx Programming 7 09-03-2010 03:08 AM
sudo perl script with environment variables powah Programming 1 04-22-2009 11:22 AM
In a perl script, how to test if user has sudo execute w/o password ? etomato Linux - Newbie 6 11-19-2008 10:18 PM
sudo ruby script/server -e production farooqhussain Linux - Distributions 1 11-05-2008 01:13 AM
how to start perl script via ssh? lene Linux - Networking 6 10-24-2007 09:13 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 01:05 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration