Front page | perl.beginners |
Postings from January 2002
using su with ssh module
From:
Justin Hansen
Date:
January 31, 2002 07:50
Subject:
using su with ssh module
Message ID:
F59pwb5XBhoVMUnLMdR00013b1b@hotmail.com
Hi,
I am trying to write a perl5 script on Redhat that will use the ssh module
and pass various commands to various servers. I am trying to run multiple
commands using ssh2 and run commands as root. I am unable to log in
directly as root and need to use the "su -" command. However, I recieve
"standard in must be a tty" and the command fails. Does anyone know how I
could pass the password into the "su" command using the ssh module written
by B. Trott? The hosts.txt file contains a hostname of the server to be
logged on to. Thanks.
btw all names, places, and passwords have been changed to protect the
innocent
Justin
#!/usr/bin/perl
use strict;
use Net::Telnet ();
use Net::SSH::Perl;
use Net::FTP;
#### setting my variables
my ( $fh, $login, $pwd, $pwd2, $t, $s, $ssh, $hname, $error, $wait, $output,
$info, $servers, $servers2, $supwd,
%passwds, @patch, @openports, @unixchk, @pkglist, @seclog, $passwds,
$tmpline, $ftp, $fhost, $crap, $exitst);
$fh = \*STDOUT;
$servers = "hosts.txt";
$servers2 = "foundhosts.txt";
$login = "joe";
$pwd = "password";
print "\n\n\nNew Test\n\n";
open (HOSTS, "$servers") or Error_Log("Could not open HOSTS: $!\n");
while(<HOSTS>) {
($hname,$supwd) = split;
$passwds{$hname} = $supwd;
}
for my $hname ( keys %passwds) {
$s = Net::SSH::Perl->new($hname, protocol => 2, port => 22);
unless($s->login( $login, $pwd)){
Error_Log("SSH Login failed on $hname: $!\n");
$s->close;
next;
}
($wait, $crap, $exitst) = $s->cmd("ls /export/home/joe/test");
if("$wait" ne "/export/home/joe/test") {
print "you rock dude !!!\n";
}
else {
print "Sucks to be you, I know I know!!!\n";
}
($ftp, $crap, $exitst) = $s->cmd("ls /tmp");
print $ftp;
($t, $crap, $exitst) = $s->cmd("su - ",'password');
print "$t, $crap, $exitst\n";
#($t, $crap, $exitst) = $s->cmd("$passwds{$hname}");
#print "$t, $crap, $exitst\n";
($t, $crap, $exitst) = $s->cmd("pwd");
print $t;
}
_________________________________________________________________
Chat with friends online, try MSN Messenger: http://messenger.msn.com
-
using su with ssh module
by Justin Hansen