Link to home
Start Free TrialLog in
Avatar of smary
smaryFlag for Canada

asked on

Write a BASH script

Hello,

I've a file with a list of hostnames. Following is what it looks like:

hostname1:192.168.1.1:56983558
hostname2:192.168.1.1:56983558
hostname3.some.thing.ca:192.168.1.1:56983558
hostname4:192.168.1.1:56983558
..
and so forth...

I want to write a script which reads only the hostnames from the file, establishes an ssh connection with each hosts. Check if a specific file exists, say the location to the file is:
/home/foo/some/path/test.txt. If the file does NOT exists, log it and try other server. If the file exists, check if the file contains a specific string, say: 'ecxlude /somedir'. If the string exists, log it and move to another server. If the string does NOT exist, append this string to the file. This string should be appended just before the start of the other exclude statements (this is the tricky part) and NOT and the end of file. Finally log it and move to other server till you reach the end of file.

Please comment the script so that I can understand it. I'd appreciate it.

Thank you.
Avatar of skymo
skymo
Flag of Israel image

does the ip is part of a well known hosts list or it can be any address?
if the answer above is Yes then - do you have to supply the ssh password or it's listed in the ~/.ssh/authorized_keys?
can you put an example to the content of /home/foo/some/path/test.txt - emphasize on the "start of the other exclude statements" .

is that a valid input?:

exclude /some/dir1
djklfhasdf
asdfas
asdfasda
/some/dir2
exclude /some/dir/3


what should be happening if the search string is /some/dir,/some/dir2 or /some/dir3?
Avatar of smary

ASKER

Good points.
- The IP could be any IP. We can use hostnames for SSH. ":" can be used as delimeter. Cut -d: -f1 works.

- this is a tricky part. In some files there are a bunch of exclude statements, and in some files there aren't any.  We are looking for specific statements, say, exclude /foo. There might be other exclude statements like exclude /foo/something or like wise. We are not bothered about it. Usually the exclude statements are in a row and not scatters. Something like:  

Blah blah
..
..
exclude /something
exclude /smethin/foo
..
..
Include /blah blah

The point is to have all exclude before include. If there aren't any excludes, appending to the file should be good.
ASKER CERTIFIED SOLUTION
Avatar of skymo
skymo
Flag of Israel image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of smary

ASKER

Your script is very good, but I'm not very good at BASH, so had to write what I understand.
Here's my other question on EE, perhaps you can help:

https://www.experts-exchange.com/questions/25096275/help-writing-a-bash-script.html?anchorAnswerId=26433988#a26433988