script to get file size


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting script to get file size
# 8  
Old 08-23-2008
Thanks for your reply...

ls -l /some/path/*txt

This worked. I put it in file and saved it in .sh format and ran it and it gave me the output how I want to. thats great. Then I checked ssh. it seems to be installed on my machine. I tried with ssh, it gave me something. so ssh installed. Then how can I use ssh to run the same .sh file to get the same information from other servers.(I mean as I given output in 1st thread). Thanks
# 9  
Old 08-23-2008
Code:
for host in server1 server2 golem elemental shibboleth; do
  ssh $host 'ls -l /some/path/*.txt'
done

... assuming your servers have names like server1, server2, golem, elemental, and shibboleth. (Sorry, not very imaginative.)
# 10  
Old 08-23-2008
hmm,

I just tried with this code

for host in server1 server2; do
ls -l /some/path/*.txt (since it is the same server where I'm running the script)
ssh $ 111.111.111.111 'ls -l /some/path/*.txt' (I also tried with server name)
done

when i ran this, I got the first server information,
-rw------- 1 root root 32768 Aug 23 14:58 /some/pathcert.txt
-rw------- 1 root root 3238 Aug 23 14:58 /some/path/cert.txt
ssh: $: Temporary failure in name resolution
-rw------- 1 root root 32768 Aug 23 14:58 /some/pathcert.txt
-rw------- 1 root root 3238 Aug 23 14:58 /some/path/cert.txt
ssh: $: Temporary failure in name resolution

I also tried this....

for host in 111.111.111.111; do
#ls -l /some/path/*.txt (since it is the same server where I'm running the script)
ssh $ 111.111.111.111 'ls -l /some/path/*.txt' (I also tried with server name)
done

I got the same error...

Thanks for your reply
# 11  
Old 08-23-2008
This is how to do it on one remote host:

Code:
ssh 111.111.111.111 'ls -l /some/path/*.txt'

The argument $host (including the dollar sign) is a single token which expands to server1 on the first iteration of the for loop, to server2 on the next iteration, etc. A lone dollar sign is not meaningful or useful in this context.

So if your remote hosts are called 111.111.111.111 and 222.222.222.222 that would give you

Code:
for host in 111.111.111.111 222.222.222.222; do
  ssh $host 'ls -l /some/path/*.txt'
done

The results you report are from the command running locally.
# 12  
Old 08-23-2008
great that works. I ran the below code

for host in 111.111.111.111 222.222.222.222; do
ssh $host 'ls -l /some/path/*.txt'
done

it prompted me to do some steps...I did all those and finally i got the output as i want to. But when I ran it for second and even third time, its prompting me for password for both servers like below...

abc:/home # ./shell2.sh
Password:
-rw------- 1 root root 32768 Aug 23 15:22 /some/path/cert.db
-rw------- 1 root root 8192 Aug 23 15:22 /some/path/crl.db
-rw-rw-rw- 1 root root 40960 Aug 16 10:12 /some/path/dx32847.db
-rw-rw-rw- 1 root root 40960 Aug 23 20:39 /some/path/dx32858.db
-rw------- 1 root root 294912 Aug 23 15:25 /some/path/nds.db
Password:
-rw------- 1 root root 8192 Aug 23 22:06 /some/path/crl.db
-rw------- 1 root root 724992 Aug 23 14:19 /some/path/nds.db
abc:/home #

can we do something to not prompt for password again and again when we run the script.

Thanks once agian....
# 13  
Old 08-23-2008
That's (also) a frequent topic in these forums; google for "passwordless ssh" or "public key".
# 14  
Old 08-23-2008
Quote:
Originally Posted by era
The big question is how you connect to those other servers now, if you have to walk to the console of each one to type commands then that's not easily scriptable, but telnet or rsh or ssh can be scripted (ssh more easily, and of course, it's the only secure option).
My bad, i have misunderstood the question. How about the following script function:

Code:
# ------------------------------------------------------------------------------
# f_CheckConnectivity                          checking connectivity for a host
# ------------------------------------------------------------------------------
# Author.....: Wolf Machowitsch
# last update: 2007 01 18    by: Wolf Machowitsch
# ------------------------------------------------------------------------------
# Revision Log:
# - 0.99   2007 01 18   Original Creation
#                       -
#
# ------------------------------------------------------------------------------
# Usage:
#     f_CheckConnectivity  char Hostname [ char user ] 
#     checks the connectivity for the host given in $1 optionally using a
#     username given in $2. If no user name is given the current user is
#     assumed.
#
#     Example:  f_CheckConnectivity $host    # checks if $host can be worked on
#                                            # using the current user (usually
#                                            # this will be root)
#
# Prerequisites:
# -   to use this function, the FPATH variable must be set
#
# ------------------------------------------------------------------------------
# Documentation:
#     f_CheckConnectivity() checks in a successive manner. First an (IP)-ping
#     is issued. If this is successful a connection test is done by issuing
#     a command via ssh. f_CheckConnectivity() does NOT try to correct any
#     errors, merely stating them. 
#
#     Parameters: char Host
#
#     returns:    0: connectivity test passed
#                 1: no ssh connection
#                 2: no IP connection
#                 3: parameter/other/internal error
#
# ------------------------------------------------------------------------------
# known bugs:
#
#     none
# ------------------------------------------------------------------------------
# .....................(C) 2007 Wolf Machowitsch ...............................
# ------------------------------------------------------------------------------

f_CheckConnectivity ()
{

$chFullDebug
                                                 # internal variables
typeset -i iRetVal=0                             # return value (see docu)
typeset    chHost="$1"                           # hostname
typeset    chUser="$2"                           # optional username

if [ -z "$1" ] ; then                            # check for prereqs
     iRetVal=3
elif [ -z "$chUser" ] ; then
     chUser="$(who am i | cut -d' ' -f1)"
fi

if [ $iRetVal -eq 0 ] ; then
     if [ $(ping -c1 $chHost 1>/dev/null 2>&1 ; print - $?) -gt 0 ] ; then
	  iRetVal=2
     fi
fi
if [ $iRetVal -eq 0 ] ; then
     if [ $(ssh -o 'BatchMode = yes' ${chUser}@${chHost} date 1>/dev/null 2>&1;\
	    print - $?
	   ) -gt 0 ] ; then
	  iRetVal=1
     else
	  iRetVal=0
     fi
fi

return $iRetVal

}
# --- EOF f_CheckConnectivity

I hope this helps.

bakunin
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Bash Script - File Size

I have a bash script. I need a modification for safety. my original bash script: mv /home/script/backup /home/script/backup2 mysql -u user -ppassword -Ddatabase --batch --skip-column-names -e 'select id, url from videos where url like "%http%" limit 1' | while read id url do youtube-dl... (1 Reply)
Discussion started by: tara123
1 Replies

2. Shell Programming and Scripting

Split file based on file size in Korn script

I need to split a file if it is over 2GB in size (or any size), preferably split on the lines. I have figured out how to get the file size using awk, and I can split the file based on the number of lines (which I got with wc -l) but I can't figure out how to connect them together in the script. ... (6 Replies)
Discussion started by: ssemple2000
6 Replies

3. Shell Programming and Scripting

shell script for getting the file size

Hi can some one please help me how i can get the output i require: My text file "sample.txt" contains the text like below Filesystem Size Used Avail Use% Mounted on /dev/mapper/vg_fedora-lv_root 15G 2.6G 12G 19% /hari Filesystem Size ... (3 Replies)
Discussion started by: harimhkr
3 Replies

4. Shell Programming and Scripting

Script to read file size and send email only if size > 0.

Hi Experts, I have a script like $ORACLE_HOME/bin/sqlplus username/password # << ENDSQL set pagesize 0 trim on feedback off verify off echo off newp none timing off set serveroutput on set heading off spool Schemaerrtmp.txt select ' TIMESTAMP COMPUTER NAME ... (5 Replies)
Discussion started by: welldone
5 Replies

5. Shell Programming and Scripting

Not able to make the file size 0 with the mentioned script

#!/bin/sh ########################################################################################################## #This script is being used for AOK application for cleaning up the .out files and zip it under logs directory. # IBM # Created #For pdocap201/pdoca202 .out files for AOK #1.... (3 Replies)
Discussion started by: mridul10_crj
3 Replies

6. Shell Programming and Scripting

Script to check file system size

Dears, the output of this command df -h | tr -s ' ' | cut -f5 -d' ' is capacity 24% 0% 0% 0% 0% 1% 0% 24% 24% 0% 93% 1% (4 Replies)
Discussion started by: xxmasrawy
4 Replies

7. Shell Programming and Scripting

compare file size from a output file from a script

Hi guys, firstly I'm working on SunOS 5.10 Generic_125100-10 sun4u sparc SUNW,Sun-Fire-V240 I've made a script to compress two directory and then send them to an other server via ftp. This is working very well. Inside theis script I decide to log usefull data for troubleshooting in case of... (7 Replies)
Discussion started by: moustik
7 Replies

8. UNIX for Dummies Questions & Answers

file size script

Hi, I am trying to write a script that will send an email to me if the size of a folder is below a certain amount. Does anyone know how to write the if (size < 1000) statement. I know how to send the email? I just need the code for determing a folder size. Thanks, Eric (5 Replies)
Discussion started by: ejbrever
5 Replies

9. Shell Programming and Scripting

Get file size in c shell script?

Hi, I want to use an 'if statement' that will check if a certian file is greater in size than a certain value given by the user, but cannot get it to work. Do you have any ideas how this can be done? Your help is appreciated! (6 Replies)
Discussion started by: Dado
6 Replies

10. UNIX for Dummies Questions & Answers

testing for file size in script

Has anyone got a few tips on how I can test if the file size is 0? I am moving files on a regular basis from one location to another with ftp. The files which are 0 bytes in size we want to discard. Thankyou in advance. (3 Replies)
Discussion started by: Ivo
3 Replies
Login or Register to Ask a Question