smary
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:5698 3558
hostname2:192.168.1.1:5698 3558
hostname3.some.thing.ca:19 2.168.1.1: 56983558
hostname4:192.168.1.1:5698 3558
..
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.t xt. 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.
I've a file with a list of hostnames. Following is what it looks like:
hostname1:192.168.1.1:5698
hostname2:192.168.1.1:5698
hostname3.some.thing.ca:19
hostname4:192.168.1.1:5698
..
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.t
Please comment the script so that I can understand it. I'd appreciate it.
Thank you.
can you put an example to the content of /home/foo/some/path/test.t xt - 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?
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?
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.
- 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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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
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
if the answer above is Yes then - do you have to supply the ssh password or it's listed in the ~/.ssh/authorized_keys?