nicely formatted directory listing from batch ssh session


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers nicely formatted directory listing from batch ssh session
# 1  
Old 03-08-2008
nicely formatted directory listing from batch ssh session

Hi,

I am really struggling to finish of a script I have been assigned.
The script's purpose is to log on to each server defined in an array, determine the Web Server version, and list the directory beneath the installation directory. In my case, this installation directory is almost always "/opt/IBMHTTPServer" (sometimes "/opt/IBMHttpServer")

Here's a snippet from the code.

Code:
for host in ${Hosts}
do
  AssumedDir=$(batch_ssh ${host} "ls -1 /opt")
  if [[ ${AssumedDir} = *IBMHTTPServer*  ]]; then
    ServerInstallDir='/opt/IBMHTTPServer'
  else
    ServerInstallDir='/opt/IBMHttpServer'
  fi

 #DirectoryListing=$(batch_ssh ${host} "find ${ServerInstallDir} -type d -print | sed -e 's;[^/]*/;|____;g;s;____|; |;g'")
 #DirectoryListing=$(batch_ssh ${host} "ls -1 $ServerInstallDir")
 DirectoryListing=$(batch_ssh ${host} "ls -1 $ServerInstallDir | while read d; do echo $d ; done")

  ServerVersion=$(batch_ssh ${host} "${ServerInstallDir}/bin/httpd -V | awk 'NR < 2'")

  echo '<br /><h3>'${host}'</h3>' >> $HTMLFILE
  echo '<b>Server Version</b>: ' ${ServerVersion} >> $HTMLFILE
  echo '<br /><b>Directory Structure</b><br /> ' $DirectoryListing >> $HTMLFILE

done

#- Email results
html_mail -t "some.body@company.co.uk" -r "noreply@servername" -s "Apache Security Audit" -a "$HTMLFILE"

(That sed command above come from a UNIX forums frequent, i forget the name! Thanks though - the two commented DirectoryListing vars are just experimentation)

So after this script has done it's thing, it invokes a perl script (html_mail) to send the report. (I didn't make this, and I know it works well). The mail comes through as planned, however the directory listing is very ugly, it looks like this:

Code:
_uninst admindocs bin cgi-bin conf example_module htdocs icons include keys.q.hpicheck.com keys.q.hpicheck.com.20061002_141729 keys.q.racexaminations.co.uk keys.q.racvehiclehealthcheck.co.uk keys.q.vehiclestatusreport.co.uk libexec license logs man readme ssl tivready version.signature

I would like the above files/directory's to be listed like they would be after issuing `ls -1` from the shell.

Thank you for reading my post, I hope someone can help me. If you have any comments of the quality of the code above please air them, as it is my first (somewhat) useful script, and I'm eager for all the feedback I can get. (Full version of the script is Bash pastebin - collaborative debugging tool)

Thank you.
# 2  
Old 03-08-2008
Try adding double quotes:
echo '<br /><b>Directory Structure</b><br /> ' "$DirectoryListing" >> $HTMLFILE
# 3  
Old 03-09-2008
Thanks for the reply, Perderabo.

When I do as you suggest, no output returns for the directory structure listing.

I tried my other two experimental "DirectoryListing" variables with the variable "double quoted" in the echo statement, that gives output, but the same, ugly, output I was having before.

Thanks,
grebbux.
# 4  
Old 03-09-2008
I think I'm getting somewhere, when I try this:

Code:
echo $DirectoryListing | sed 's/ /<br>/' >> $HTMLFILE

FYI: Nothing is listed if I put the above $DirectoryListing in "".

The listing looks like this:

Code:
_uninst
admindocs bin cgi-bin conf example_module htdocs icons include keys.q.hpicheck.com keys.q.hpicheck.com.20061002_141729 keys.q.racexaminations.co.uk keys.q.racvehiclehealthcheck.co.uk keys.q.vehiclestatusreport.co.uk libexec license logs man readme ssl tivready version.signature

So I have one <br> after the first listing. I don't understand why the break isn't substituted for all the other spaces though!

Thanks again for reading.
grebbux.
# 5  
Old 03-09-2008
I did it.

I went for the array approach.

Code:
declare -a DirectoryListing
$DirectoryListing=$(batch_ssh ${host} "ls -1 ${ServerInstallDir}")
for dir in ${DirectoryListing[@]}
do
echo $dir '<br>' >> $HTMLFILE
done

Thanks for the help anyway. Smilie

***SOLVED***

grebbux.
# 6  
Old 03-09-2008
Quote:
Originally Posted by grebbux
I think I'm getting somewhere, when I try this:

Code:
echo $DirectoryListing | sed 's/ /<br>/' >> $HTMLFILE

So I have one <br> after the first listing. I don't understand why the break isn't substituted for all the other spaces though!
That sed s command only replaces the first match. You need the g flag to do them all.

Code:
echo $DirectoryListing | sed 's/ /<br>/g' >> $HTMLFILE

But I'm glad you got it solved.
# 7  
Old 03-09-2008
Ah thanks for answer there, I think the sed substitution is a better solution. Smilie
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Advanced & Expert Users

Formatted Directory Listing Question

Greetings to you. I have a quick question for the community today ;) I'm interested in the following format for recursive output from a command such as "ls" or "dir" when pointed to a folder: ...but there doesn't seem to be coverage for this type of output formatting in the manpages. Maybe... (11 Replies)
Discussion started by: LinQ
11 Replies

2. UNIX for Dummies Questions & Answers

SSH batch help

Dear Gurus, I had hundreds of equipments which i can only use SSH to login and retrieve informations. Thus, how should i do that? eg:ssh batch @ echo off ssh <server1>@10.168.1.1 ECHO testing1 exit ssh <server2>@10.168.1.2 ECHO testing2 exit Can the above method used? :) (6 Replies)
Discussion started by: ymeyaw
6 Replies

3. UNIX for Dummies Questions & Answers

SSH session

I ran a job using SSH and then used screen command followed by space bar. Then I ran the job. But after some hours, the internet connection was lost.This had happened to me before. I used to run the whole job again. Today I came across the fact that I could restart the jobs without having to do it... (2 Replies)
Discussion started by: nellierd
2 Replies

4. Shell Programming and Scripting

SSH session saving

Hi, I want to execute some script on the remote session and transfer the processed file on remote machine to local machine. I tried with ssh-keygen and ssh-copy-id but seems that this has been disable on the remote machines, Is there any possiblity that I can save the remote username and... (7 Replies)
Discussion started by: maruthavanan
7 Replies

5. HP-UX

ssh session getting hung (smilar to hpux telnet session is getting hung after about 15 minutes)

Our network administrators implemented some sort of check to kill idle sessions and now burden is on us to run some sort of keep alive. Client based keep alive doesn't do a very good job. I have same issue with ssh. Does solution 2 provided above apply for ssh sessions also? (1 Reply)
Discussion started by: yoda9691
1 Replies

6. Shell Programming and Scripting

could not send commands SSH session with Net::SSH::Expect

I am using Net::SSH::Expect to connect to the device(iLO) with SSH. After the $ssh->login() I'm able to view the prompt, but not able to send any coommands. With the putty I can connect to the device and execute the commands without any issues. Here is the sample script my $ssh =... (0 Replies)
Discussion started by: hansini
0 Replies

7. Solaris

mbx_bad_handle during ssh -X session

We have a Solaris 10 server that we're migrating an old Cadre Teamwork application to. We find that when users on Sun workstations use ssh -X <username>@<new server> and run Teamwork on this new server it is fine. When cygwin users and linux users run ssh -X to get to this new platform they... (0 Replies)
Discussion started by: pzxkys
0 Replies

8. Shell Programming and Scripting

scripting an ssh session?

I know the root login/password for a machines, and I want to automate some commands like this from each: ssh root@remoteHost1 "tail /var/log/messages" ssh root@remoteHost2 "tail /var/log/messages" ssh root@remoteHost3 "tail /var/log/messages" ssh root@remoteHost4 "tail /var/log/messages" ssh... (2 Replies)
Discussion started by: jjinno
2 Replies

9. UNIX for Dummies Questions & Answers

SSH Batch configuration

Team, I am trying to do ssh batch as root. I have ssh set-up to do work w/o requiring password/uid on remotes. The problem is I need to submit batch work to remote systems, and NOT have my terminal program.sh lock/wait for the response from remote. This is so I can submit batch work to other... (2 Replies)
Discussion started by: xcom007
2 Replies

10. UNIX for Dummies Questions & Answers

Recursive directory listing without listing files

Does any one know how to get a recursive directory listing in long format (showing owner, group, permission etc) without listing the files contained in the directories. The following command also shows the files but I only want to see the directories. ls -lrtR * (4 Replies)
Discussion started by: psingh
4 Replies
Login or Register to Ask a Question