LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices


Reply
  Search this Thread
Old 02-16-2007, 05:45 AM   #1
lion1978
LQ Newbie
 
Registered: Feb 2007
Posts: 5

Rep: Reputation: 0
Thumbs up Using shell script to change hostname(Fedora Core6)


I'm trying to make shell script which can change about 500 client's hostname.
I believe that rewriting /etc/sysconfig/network is needed to change hostname.

Does anyone have any idea to rewrite /etc/sysconfig/network by use of shell script??
 
Old 02-16-2007, 07:31 AM   #2
zaichik
Member
 
Registered: May 2004
Location: Iowa USA
Distribution: CentOS
Posts: 419

Rep: Reputation: 30
Try this:
Code:
#!/bin/sh

NEWHOST=$1
if [ $# -ne 1 ]
   then echo "Usage: scriptname new_hostname"
   exit 1
fi

sed -i.BAK -e "s/HOSTNAME=.*/HOSTNAME=$NEWHOST/" /etc/sysconfig/network
This script takes exactly one argument, like
Code:
./scriptname host.domain.com
If there are more arguments than one, or none at all, it exits. Otherwise, it replaces the line
HOSTNAME=<whatever>
with
HOSTNAME=<the supplied argument>
in /etc/sysconfig/network, making a backup of the original contents of the file to /etc/sysconfig/network.BAK

Hope that helps.
 
Old 02-23-2007, 12:58 AM   #3
lion1978
LQ Newbie
 
Registered: Feb 2007
Posts: 5

Original Poster
Rep: Reputation: 0
Thank you very mach Zaichik. It works well.

But I have more than 500 clients...
It seems very hard to give them hostname one at a time.
And I also have to change ip address at the same time.(without using DHCP)

Does anyone have any idea to do this simply?
 
Old 02-23-2007, 09:49 AM   #4
zaichik
Member
 
Registered: May 2004
Location: Iowa USA
Distribution: CentOS
Posts: 419

Rep: Reputation: 30
Hmm. No, not the simplest. I can't think of a simple method for changing the IPs and hostnames of 500 client machines short of using DHCP. Even there, you would have to touch 500 boxes to enable DHCP on them.

Are the clients already configured to contact the server in anyway? If not, I just can't see how you going to get around having to touch all 500 machines.

How are you planning on assigning the IP addresses? Is there any kind of relationship to the current IP address and the new one that you want to assign? Is the new hostname based on the new IP address, or is there a relationship between new and old hostnames?

If there are such relationships, the changes could be made accordingly. Still, you would have to download and run the script on each client.
 
Old 02-25-2007, 11:07 PM   #5
lion1978
LQ Newbie
 
Registered: Feb 2007
Posts: 5

Original Poster
Rep: Reputation: 0
Thank you very much for your prompt response Zaichik!!

Well...,actually clients are configured as dual-boot system, Vista and FC6.

Vista's IP address and hostname could be configured by use of Altiris which doesn't support FC6.

I'm planning to configure Vista and FC6 as mentioned below.

Client01
Vista---hostname:win01, IP:192.168.10.1/24
FC6-----hostname:lin01, IP:192.168.10.101/24
Client02
Vista---hostname:win02, IP:192.168.10.2/24
FC6-----hostname:lin02, IP:192.168.10.102/24

Hostname is basically the same except for initial letter, and FC6's IP address is Vista's one plus 100.

Since FC6 is able to mount Vista's file system and read all files, I think I can do anything thorough this point to change configuration.

Do you have any idea??
 
Old 02-26-2007, 01:42 AM   #6
CaptainInsane
Member
 
Registered: Nov 2003
Location: Peoria
Distribution: Fedora 8
Posts: 92

Rep: Reputation: 15
Havent played with vista yet, but since you can mount up the file system, is there a way you could
latch onto the hostname from the vista partition, and update the linux hostname ?

If you could find the vista hostname in some config file, or even a log file of some kind, you would then
be able to run 1 script to update them all.

Also curious as to why you have 2 IPs for each box. I usually set up my dualboot machines with the
same IP for each OS, since only one will be running at a time anyways....
 
Old 02-26-2007, 06:10 AM   #7
zaichik
Member
 
Registered: May 2004
Location: Iowa USA
Distribution: CentOS
Posts: 419

Rep: Reputation: 30
Is the Vista boot already properly configured with the hostname and IP?
 
Old 02-26-2007, 10:26 PM   #8
lion1978
LQ Newbie
 
Registered: Feb 2007
Posts: 5

Original Poster
Rep: Reputation: 0
>Also curious as to why you have 2 IPs for each box. I usually set up my dualboot machines with the
same IP for each OS, since only one will be running at a time anyways....

Yes I think the same way. It's nonsense.
But customer wants this system... Nothing I said made difference.

>Is the Vista boot already properly configured with the hostname and IP?

Yes, it's configured properly.
 
Old 02-27-2007, 05:13 AM   #9
timmeke
Senior Member
 
Registered: Nov 2005
Location: Belgium
Distribution: Red Hat, Fedora
Posts: 1,515

Rep: Reputation: 61
A few remarks:
1. The IP Linux = IP Win +100 rule can't apply to all hosts (the last number in the IP only ranges from 0 to 255). So, please provide the full IP numbering scheme for all your hosts.
2. Small enhancement to zaichik's script:
Code:
#!/bin/sh
NEWHOST=$1
if [ $# -ne 1 ]
   then echo "Usage: $0 new_hostname"  #better use $0 or `basename $0` here, 
                                       #just in case you rename the script later on
   exit 1
fi
sed -i.BAK -e "s/HOSTNAME=.*/HOSTNAME=$NEWHOST/" /etc/sysconfig/network
3. Do you have a list of the current hostnames?
And is there some way to remotely execute commands on them (ie login via ssh or rsh)?
If you could ssh to them, then you could use "scp" to distribute your script to all hosts and then use "ssh" to run it on each of the hosts, in a simple for-loop. If you could use rsh, maybe "rdist" can help as well.
 
Old 03-04-2007, 10:42 PM   #10
lion1978
LQ Newbie
 
Registered: Feb 2007
Posts: 5

Original Poster
Rep: Reputation: 0
IP numbering of my hosts are listed below.

Vista:192.168.10.5/24 - 192.168.10.105/24

And there are other 4 segments, 192.168.11.0 - 192.168.14.0.

IP linux = IP Vista + 100.

Also Hostnames are listed below.

Vista:win001 - win500
Linux:lin001 - lin500

>And is there some way to remotely execute commands on them (ie login via ssh or rsh)?

Yes, I can ssh to all of my client!!
 
Old 03-05-2007, 02:14 AM   #11
timmeke
Senior Member
 
Registered: Nov 2005
Location: Belgium
Distribution: Red Hat, Fedora
Posts: 1,515

Rep: Reputation: 61
Then maybe you can use an approach like this:
Code:
sub='192.168.10.';
for (( i=100; i<=200; i=$i+1 )); do #to loop over all hosts, ie per sub-domain
   num=`printf '%3.3d' $i`;
   hostnum=(($i -100));
   hostnum=`printf '%2.2d' $hostnum`;
   scp yourScript.sh you@${sub}{num}:/some/path
   #now that we've copied your script to the remote hosts, we can execute it on each host
   ssh you@lin${num}.your.domain "/some/path/yourScript.sh lin${hostnum}"
   if (( $? != 0 )); then
      echo "Remote command failed on iteration $i";
   fi;
done
As I'm not sure that I got all the syntax right, you might want to replace the remote execution of yourScript.sh with a harmless echo command first, like:
Code:
ssh you@lin${num}.your.domain "echo /some/path/yourScript.sh lin${hostnum}"
and see if that prints the right commands to execute.

Of course, you can encapsulate the above for-loop in a second loop that runs over all 4 sub-domains (ie for (( net=10; net<60; net=$net+10 )); do sub="192.168.${net}"; ... done)

Last edited by timmeke; 03-05-2007 at 02:16 AM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Fedora Core6 ndiswrapper issue lqamoebafc6 Linux - Wireless Networking 3 01-15-2007 06:58 PM
How to change hostname on shell [user@tochange] guarriman Fedora 2 01-15-2007 12:04 AM
Problem Ati x700 install Fedora Core6 64bit muratt Linux - Hardware 0 11-09-2006 09:47 AM
script to change IP and hostname under Solaris 10 xpucto Solaris / OpenSolaris 9 06-08-2006 05:37 AM
Running a shell script remotely via 'ssh <hostname> <command>' davee Linux - General 4 10-09-2005 01:38 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 03:32 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration