Results 1 to 6 of 6
Hi, I'm trying to write a script that ssh's to another server and does some commands.
I have problems with this:
Code:
LIST=Žls -l /DIR | grep -v gz | ...
- 09-14-2011 #1Just Joined!
- Join Date
- Sep 2011
- Posts
- 2
ssh+commands in shell script
Hi, I'm trying to write a script that ssh's to another server and does some commands.
I have problems with this:
I then run the script on the local server and outputs it to a .txt file.Code:LIST=Žls -l /DIR | grep -v gz | wc -l` ssh user(at)server <<EOT echo $LIST if [ $LIST -gt 1 ] then echo " One or more files are not gzipped " else echo " It's fine " fi EOT
But the output is 0 everytime, even if there are files that's not gzipped in that directory.
What am I missing?
- 09-15-2011 #2Just Joined!
- Join Date
- May 2011
- Posts
- 44
Is there a reason you chose to use Bash to do this?
Perl offers some very easy to implement modules that will make you life much easier, and make maintenance easier.
Specifically:
Net::SSH::Perl
Net::SSH
Here is an example I wrote a while ago:
This is a basic example, but you could easily extend this for multiple hosts.Code:#!/usr/bin/perl use strict; use warnings; use Net::SSH::Perl; my $host = "192.168.1.150"; my $user = 'root'; my $pass = 'password'; my $ssh = Net::SSH::Perl->new($host); $ssh->login($user, $pass); my ($stdout, $stderr, $exit) = $ssh->cmd("ls -l"); print "$stdout, $stderr, $exit \n"; ($stdout, $stderr, $exit) = $ssh->cmd("find -name 'somefile'");
Past that, I really need a better idea of what it is you are attempting to do.
- 09-15-2011 #3Linux Guru
- Join Date
- May 2011
- Posts
- 1,842
I agree w/three18ti - there are more sophisticated ways to do this.
But looking at your example, I would modify it like so in order to get it work like you want (I think) - but most important is that you have to escape the variable holding your count value:
Code:#!/bin/bash #LIST='ls -l /tmp/DIR | grep -v gz|wc -l' ssh user(at)server <<EOT cnt=$(find /DIR -type f -not -iname '*.gz'|wc -l) echo \$cnt if [ \$cnt -gt 1 ]; then echo " One or more files are not gzipped " else echo " It's fine " fi EOT
- 09-15-2011 #4
Hi, your script doesn't work because you defined your LIST variable locally. When you ssh'd to the remote server, you were in a new shell and $LIST didn't exist. Also rather than using ssh name@server <<EOT, I would use semicolons for line breaks. Something like:
btw, I didn't test that. Might put you on the right lines though.Code:ssh user@server 'if [ "$(ls -l /DIR | grep -v gz | wc -l)" -gt 1 ]; then echo " One or more files are not gzipped "; else echo "its fine";fi'
Registered Linux user #389109
My Semi-Linux Blog
- 09-15-2011 #5Just Joined!
- Join Date
- Sep 2011
- Posts
- 2
Thanks for the input, learned something atleast.
I did it the "lazy" way and made a script on the remote server with the commands and called that script from a script on the local server.
- 09-26-2011 #6Just Joined!
- Join Date
- Sep 2011
- Posts
- 1
And ...
+ Net::OpenSSH
+ Net::SSH2


Reply With Quote