what about if i have my file1 in this format
word1
word2
word3
and my file2 is always going to be in this format
sentece2
so I need a for loop so that whenever a file2 file3 filex comes will add that 1 sentence to my file1. Or is it just easier to paste with 2 separate files.
I don't understand what kind of things you have nor what kind of this you want to achieve, i need more infos as well as full example of input you have and output you want.
# Logs to each server using file, gets relase information into a file, copies #it to harp server, deletes file.
for host in `cat $PATH_TEMP/name_of_file`
do
ssh $host sh lsb_release -a >> $host/tmp/$host_release.info
scp -p $host:/tmp/$host_release.info $PATH_TEMP/release.info
rm /tmp/$host_release.info
done
So when the script is executed for the fist time...this will be the result in the local host
list_file
server1
server2
server3
server1_file
version
final_file
server1;version
if we keep going this is what I want to have in the final_file
list_file
server1
server2
server3
server2_file
version
final_file
server1;version
server2;version
and the only thing that will change as the script uses the list file would be the serverX file, which willl have a different version and that will be combined with the 2nd entry of file 1 to be entered in final_file as the second entry.
Did you test your script ?
What error message do you get ?
1. for host in `cat $PATH_TEMP/name_of_file`
2. do
3. ssh $host sh lsb_release -a >> $host/tmp/$host_release.info
4. scp -p $host:/tmp/$host_release.info $PATH_TEMP/release.info
5. rm /tmp/$host_release.info
6. done
In line 3 of your code shouldn't you have a colon after $host ($host:/tmp/$host_release.info)?
In line 5 of your code shouldn't the rm occure on the remote $host (ssh $hosts rm /tmp/$host_release.info)?
every time I perform a command in the remote server.
Also, do I need to ssh everytime I execute a command on the server..
This is my current script and It runs but the file does not come as I need it, however if I execute the commands manually on the remote servers, the file is created correctly.
# Logs to each server using file and append the name of file with the release description into
# a single file and copies it to harp server
echo "entering for loop"
for host in `cat $PATH_TEMP/test1.txt`
do
echo "inside do loop"
#logs to server and gets release information into file
echo "before ssh"
ssh $host more /etc/redhat-release >> $host:/tmp/host_release.info
echo "done creating release file"
#creates file and adds name of server to the file
touch $host:/tmp/host_name.info
echo "done creating file with server name"
echo $host >> /tmp/host_name.info
echo "done modifying server name file"
#concatenates both files into a single file with a ';'
paste -d ";" $host:/tmp/host_name.info $host:/tmp/host_release.info >> $host:/tmp/release.info
echo "done formatting file"
scp -p /tmp/release.info harp:$PATH_TEMP/release.info
echo "done copying file to local server"
rm -f $host:/tmp/host_name.info
rm -f $host:/tmp/host_release.info
rm -f $host:/tmp/release.info
echo "done deleting .info files"
done
#Adds new information from server to main file
cat $PATH_TEMP/release.info >> servers_release.info
echo "done concat"
rm $PATH_TEMP/release.info
echo "done -rm"
So if I understood correctly, something like this would look a lot better?
# Logs to each server using file and append the name of file with the release description into
# a single file and copies it to harp server
echo "entering for loop"
for host in `cat $PATH_TEMP/test1.txt`
do
echo "inside do loop"
#logs to server and gets release information into file
echo "before ssh"
ssh $host "more /etc/redhat-release >> $host:/tmp/host_release.info"
echo "done creating release file"
#creates file and adds name of server to the file
ssh $host "touch $host:/tmp/host_name.info"
echo "done creating file with server name"
ssh $host "echo $host >> /tmp/host_name.info"
echo "done modifying server name file"
#concatenates both files into a single file with a ';'
ssh $host "paste -d ";" $host:/tmp/host_name.info $host:/tmp/host_release.info >> $host:/tmp/release.info"
echo "done formatting file"
scp -p /tmp/release.info harp:$PATH_TEMP/release.info
echo "done copying file to local server"
ssh $host "rm -f $host:/tmp/host_name.info"
ssh $host "rm -f $host:/tmp/host_release.info"
ssh $host "rm -f $host:/tmp/release.info"
echo "done deleting .info files"
done
#Adds new information from server to main file
cat $PATH_TEMP/release.info >> servers_release.info
echo "done concat"
rm $PATH_TEMP/release.info
echo "done -rm"
No
remove '$host:' in every filename except when used in an scp
Also read the :
man ssh
and pay special attention to the options '-n' '-x' '-t' '-X' '-T'
all depend what you are trying to do and where you are willing to do
If you are on serv1 , when you want a command to be executed on another system serv2
just
# Logs to each server using file and append the name of file with the release description into
# a single file and copies it to harp server
echo "entering for loop"
for host in `cat $PATH_TEMP/test1.txt`
do
echo "inside do loop"
#logs to server and gets release information into file
echo "before ssh"
ssh $host "more /etc/redhat-release >> /tmp/host_release.info"
echo "done creating release file"
#creates file and adds name of server to the file
ssh $host "touch /tmp/host_name.info"
echo "done creating file with server name"
ssh $host "echo $host >> /tmp/host_name.info"
echo "done modifying server name file"
#concatenates both files into a single file with a ';'
ssh $host "paste -d ";" /tmp/host_name.info /tmp/host_release.info >> /tmp/release.info"
echo "done formatting file"
scp -p /tmp/release.info harp:$PATH_TEMP/release.info
echo "done copying file to local server"
ssh $host "rm -f /tmp/host_name.info"
ssh $host "rm -f /tmp/host_release.info"
ssh $host "rm -f /tmp/release.info"
echo "done deleting .info files"
done
#Adds new information from server to main file
cat $PATH_TEMP/release.info >> servers_release.info
echo "done concat"
rm $PATH_TEMP/release.info
echo "done -rm"
This is what I get on my console...
[root@harp eplc]# sh lastScript.sh
entering for loop
inside do loop
before ssh ---------------> hangs in here
Killed by signal 2. --------> I do a CTRL-C
done creating release file
done creating file with server name
done modifying server name file
paste: option requires an argument -- d
Try `paste --help' for more information.
lastScript.sh: line 19: /tmp/host_name.info /tmp/host_release.info >> /tmp/release.info: No such file or directory
done formatting file
Host key verification failed.
lost connection
done copying file to local server
done deleting .info files
inside do loop
before ssh
ssh_exchange_identification: Connection closed by remote host
done creating release file
ssh_exchange_identification: Connection closed by remote host
done creating file with server name
ssh_exchange_identification: Connection closed by remote host
done modifying server name file
ssh_exchange_identification: Connection closed by remote host
lastScript.sh: line 19: /tmp/host_name.info /tmp/host_release.info >> /tmp/release.info: No such file or directory
done formatting file
ssh_exchange_identification: Connection closed by remote host
done copying file to local server
done deleting .info files
inside do loop
before ssh
Killed by signal 2.
For this error...
paste: option requires an argument -- d
Try `paste --help' for more information.
my paste command has a -d, I am thinking that the multiple "" are making noise in there?
For this error...
lastScript.sh: line 19: /tmp/host_name.info /tmp/host_release.info >> /tmp/release.info: No such file or directory
?
do you want the file name to be append in /tmp/release.info ?? (if so, use "echo" instead of "more")
or do you want the content of the file /etc/redhat-release to be append in /tmp/release.info ?? (if so, use "cat" instead of "more")
I will go ahead and try your suggestions...
lets hope this helps...
I do want the content of the file /etc/redhat-release to be append in /tmp/release.info,
so I will use cat instaed..
Thanks
---------- Post updated at 03:08 PM ---------- Previous update was at 02:47 PM ----------
this is the script I used...
# Logs to each server using file and append the name of file with the release description into
# a single file and copies it to harp server
echo "entering for loop"
for host in `cat $PATH_TEMP/test1.txt`
do
echo "inside do loop"
#logs to server and gets release information into file
echo "before ssh"
ssh $host "cat /etc/redhat-release >> /tmp/host_release.info"
echo "done creating release file"
#creates file and adds name of server to the file
ssh $host "touch /tmp/host_name.info"
echo "done creating file with server name"
ssh $host "echo $host >> /tmp/host_name.info"
echo "done modifying server name file"
#concatenates both files into a single file with a ';'
ssh $host "paste -d \";\" /tmp/host_name.info /tmp/host_release.info >> /tmp/release.info"
echo "done formatting file"
scp -p $host:/tmp/release.info harp:$PATH_TEMP/release.info
echo "done copying file to local server"
ssh $host "rm -f /tmp/host_name.info"
ssh $host "rm -f /tmp/host_release.info"
ssh $host "rm -f /tmp/release.info"
echo "done deleting .info files"
done
#Adds new information from server to main file
cat $PATH_TEMP/release.info >> servers_release.info
echo "done concat"
rm $PATH_TEMP/release.info
echo "done -rm"
this is what I get on my shell
[root@edsrep1 tmp]# more host_name.info
edsrep1
edsrep1
edsrep1
[root@edsrep1 tmp]# more host_release.info
::::::::::::::
/etc/redhat-release
::::::::::::::
Red Hat Enterprise Linux AS release 3 (Taroon Update 4)
::::::::::::::
/etc/redhat-release
::::::::::::::
Red Hat Enterprise Linux AS release 3 (Taroon Update 4)
Red Hat Enterprise Linux AS release 3 (Taroon Update 4)
[root@edsrep1 tmp]# cat /etc/redhat-release >> /tmp/host_release.info2
[root@edsrep1 tmp]# more host_release.info2
Red Hat Enterprise Linux AS release 3 (Taroon Update 4)
[root@edsrep1 tmp]# more release.info
edsrep1;::::::::::::::
edsrep1;/etc/redhat-release
edsrep1;::::::::::::::
;Red Hat Enterprise Linux AS release 3 (Taroon Update 4)
;::::::::::::::
;/etc/redhat-release
;::::::::::::::
;Red Hat Enterprise Linux AS release 3 (Taroon Update 4)
;Red Hat Enterprise Linux AS release 3 (Taroon Update 4)
It looks like its redoing the same thing 3 times for the same server. The file where all the servers are listed looks like this
test1.txt
edsrep1
edsrep2
edsrep3
so the 1st time this script is exected, this should be the content of file file copied over ssh
release.info
edsrep1;Red Hat Enterprise Linux AS release 3 (Taroon Update 4)