Perl open a remote shell


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Perl open a remote shell
# 8  
Old 08-26-2010
Hello

yes I can do ("ssh user@hostname"); but I do not want to share my password with other user hence I need to make it common for every one.

Here is my code which logs in and performs some task but it does not show the output of top and pstree command what would i do to get the output of these commands along with "vmstat 1 10" and "iostat 1" , "top -cd2" etc..

Code:
#!/usr/bin/perl -w
use Net::SSH::Perl

$host = '192.168.0.114';
$username = 'root';
$login_passwd = 'password';

my $ssh = Net::SSH::Perl->new($host);

$ssh->login($username, $login_passwd);

while(<>) {
$cmd = $_;
my($stdout,$sterr, $stderr) = $ssh->cmd("$cmd");
print "\n$stdout\n$stderr       ";
print "logging out from the server " if eof;
}

Thanks
Pratap

Last edited by Yogesh Sawant; 08-27-2010 at 04:44 AM.. Reason: added code tags
# 9  
Old 08-26-2010
Quote:
Originally Posted by pratapsingh
Hello

yes I can do ("ssh user@hostname"); but I do not want to share my password with other user
So you want to log in securely and passwordlessly? Try keys. Then you could do
Code:
system("ssh -i /path/to/authfiles user@hostname")

You shouldn't be trying to feed in plaintext passwords automatically, that's very insecure.

You can further restrict the danger to the remote system by configuring sudo on the remote hosts instead of logging in as root all the time.

Last edited by Corona688; 08-26-2010 at 04:17 PM..
# 10  
Old 08-27-2010
Hello Corona,

Thanks for your reply but I think you did'nt check the first post of this thread where I had already mentioned it, I am storing all the password and details in a database and fetching it from there , i mentioned some details in my code since it is just a part of my full code which executes the command entered by user on the remote system.

Regarding using ssh keygen, I have dynamic updates on my database of system details so every time I can not create ssh-keygen and keep it and that is not secure too if you are not the only one accessing the remote system from your system and any one can access the remote system.

And if you think they can do the same with my script then wrong, to access the script you have to provide your login credential which I have done already.

Thank you.
# 11  
Old 08-27-2010
Quote:
Originally Posted by pratapsingh
Hello Corona,

Thanks for your reply but I think you did'nt check the first post of this thread where I had already mentioned it, I am storing all the password and details in a database and fetching it from there
I know how you're trying to accomplish the solution, yes. But things like su, sudo, ssh, scp, and sftp are all designed to stop you from doing this. That's why strange contortions and imported libraries are needed to do it at all: You need to create a convincing enough virtual terminal to fool ssh into thinking it's an interactive user...
Quote:
Regarding using ssh keygen, I have dynamic updates on my database of system details so every time I can not create ssh-keygen and keep it and that is not secure too if you are not the only one accessing the remote system from your system
You may have spent a lot of time painting yourself into a corner building a password database instead of using ssh's built-in security features, then. And, no. You can't get the key from the remote host.
Quote:
...and any one can access the remote system.
Nope. You can't get the keys from the remote host, and you can only login with the keys if you have access to the keys.
# 12  
Old 08-31-2010
Thanks for your reply ..

I am now able to login to server and execute as many command as I want . I am now able to open an interactive session with my script. Still facing some issue but I will try to fix them too ..

Here is the sample code which I am using :

Code:
#!/usr/bin/perl -w
use strict;
use Net::SSH::Perl;
my $host = '192.168.0.114;
my $username = 'pratap';
my $login_passwd = 'password';
my $ssh = Net::SSH::Perl->new($host, $user);
$ssh->config->set('interactive', 1)
    unless defined $ssh->config->get('interactive');
$ssh->login($username, $login_passwd);

if ($cmd) {
    my($out, $err, $exit) = $ssh->cmd($cmd);
    print $out if $out;
    print $err if $err;
}
else {
    eval "use Term::ReadKey;";
    ReadMode('raw');
    eval "END { ReadMode('restore') };";
    $ssh->shell;
    print "Connection to $host closed.\n";
}


Last edited by pludi; 08-31-2010 at 09:52 AM.. Reason: code tags, please...
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Converting shell to Perl I run into shell built in function trap and need alternative in Perl

I am working on converting shell to Perl script. In shell we have built in function trap Do you know alternative in Perl or actually we don't need it? Thanks for contribution (3 Replies)
Discussion started by: digioleg54
3 Replies

2. UNIX for Dummies Questions & Answers

Check port 2222 is open on a remote host

I am able to connect to a remote host using the legacy IP and port 2222. Today the remote has a new IP I am unable to connect. How to check if the remote host is blocking or if its my server is unable to connect. Err Msg : telnet: Unable to connect to remote host: Connection refused Err... (5 Replies)
Discussion started by: ITDev01
5 Replies

3. Shell Programming and Scripting

Triggering remote UNIX shell script from Remote desktop

I m trying to run a batch script in remote desktop which executes unix commands on the unix server...the problem is i wnt the output in HTML format.so in my batch script i m giving the cmd like ssh hostname path ksh HC_Report.ksh>out.html ...but it generates the HTML file in remote desktop .i... (2 Replies)
Discussion started by: navsan
2 Replies

4. Shell Programming and Scripting

Help !! perl open function

Help Please perl Gurus, I am trying to add ungrouped passengers in a group and I creating a script however it fails on first step only I tried all the options it returns following error. syntax error at junki line 4, near "open " Execution of junki aborted due to compilation errors. ... (2 Replies)
Discussion started by: dynamax
2 Replies

5. UNIX for Dummies Questions & Answers

How to Know is a Remote hosts have open port

Hi gurus of unix!!!!, I have a little question. I nedd your helps The scenarios is the following I have tree equipment that are installed in different places. I use a carrier to interconnect the equipment. Some Port's (TCP) need to be open for an application that must be function correctly. For... (3 Replies)
Discussion started by: andresguillen
3 Replies

6. Shell Programming and Scripting

Open n number of remote sessions

I m need of something useful to reduce my n number of login sessions to save my time. Scenario : I will login 40+ servers daily with ip address. i hv generated ssh key gens in order to avoid passwords. what i need is : when i click a terminal, i need these 40 servers logged in... (1 Reply)
Discussion started by: anand.linux1984
1 Replies

7. Shell Programming and Scripting

Simple open remote file script

I use Rsync to copy files to a remote folder in a command like this rsync -aNPHAXxrvh --protect-args --fileflags --force-change --delete $file user@remotehost:/Volumes/backup That works great with passwordless key exchange ;) Now I'm trying to open a remote file using this command ssh -q... (5 Replies)
Discussion started by: elbombillo
5 Replies

8. Solaris

Unable to open remote connections

Hello everybody, This is an unusual problem that I am facing on my Solaris 9 on Sun Blade 150 workstation. I can ping remote machines (outside subnet) but I can't open up a connection/port on those machines. For example, `ping ftp.xyz.com` gives ftp.xyz.com is alive but if I do a `ftp... (1 Reply)
Discussion started by: red_crab
1 Replies

9. UNIX for Dummies Questions & Answers

Remote Connect - SCO Open Server

Hello.. Please helppppppp...!!! I am using SCO Open Server version 5.0 at work. I am trying to find a third party software to use on my computer at home which has windows 98 on it to do remote connect to my work. Is there any such software ? I know there is PC ANYWHERE but i don't think it... (2 Replies)
Discussion started by: jackpotp
2 Replies
Login or Register to Ask a Question