• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Changing hostname in Cocoa?
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Changing hostname in Cocoa?


  • Subject: Re: Changing hostname in Cocoa?
  • From: "Sven A. Schmidt" <email@hidden>
  • Date: Mon, 15 Oct 2001 16:57:17 +0200

You can change the hostname from the command line as root (or administrator) like this:

hostname newhostname

I don't think there's a Cocao framework for this, so from within Cocao, you could something like this to set the hostname:

- (NSData *)runCliCommand: (NSString *)cmd
WithArgs: (NSArray *)args
{
int status;
NSTask *task = [[ NSTask alloc ] init ];
NSPipe *pipe = [ NSPipe pipe ];
NSFileHandle *readHandle = [ pipe fileHandleForReading ];
NSData *cmdOutput;

//NSLog( @"Starting task %@", cmd );

[ task setCurrentDirectoryPath: @"." ];
[ task setLaunchPath: cmd ];
if ( args )
[ task setArguments: args ];
[ task setStandardOutput: pipe ];

[ task launch ];
[ task waitUntilExit ];

cmdOutput = [ [ NSData alloc ] initWithData :[ readHandle readDataToEndOfFile ] ];

status = [ task terminationStatus ];
//NSLog( @"Exit status %i", status );

[ task release ];

return [ cmdOutput autorelease ];
}

Just call it like this:

runCliCommand: @"/bin/hostname" withArgs: [ NSArray arrayWithObjects: @"newhostname", nil ];

Of course for this to work, you need to be admin (the admin runs the program) or authenticate. The latter is a bit more complicated. There are frameworks by Brian Hill / Ben Lachman available, see stepwise or contact me if you need the classes.

I'm not sure if the lookup is updated by this, though, i.e. if your intended "ftp newhostname" will work without having to update netinfo or /etc/hosts. (You are correct that by default the flat file /etc/hosts is not used for name lookup in multiuser mode, but you can get the system to do it.)

HTH,
Sven

On Montag, Oktober 15, 2001, at 05:14 Uhr, David Lee wrote:

I require my application to change the hostname of my computer, the reasons are irrelevant to this post but how do I go about doing this in Cocoa?

I thought I was pretty good on UNIX systems and it turns out that I can't even do this manually. I tried editing /etc/hostconfig and changing the HOSTNAME=evolution part to the name of my choice and fair enough, my command prompt reads with the correct hostname, but it's only asthetic so how do I enable this to work so that I can type ftp evolution from my console and get to my own machine just as with localhost?

I heard that OSX doesn't use /etc/hosts in multiple user mode and it uses netinfo for this sort of thing. Is there a guide to netinfo or something?

Is it even possible to change the hostname in Cocoa?

Thanks for any help in advance,
David Lee
_______________________________________________
cocoa-dev mailing list
email@hidden
http://www.lists.apple.com/mailman/listinfo/cocoa-dev


References: 
 >Changing hostname in Cocoa? (From: David Lee <email@hidden>)

  • Prev by Date: Re: Zooming in/out of images
  • Next by Date: Q's about Obj-C
  • Previous by thread: Changing hostname in Cocoa?
  • Next by thread: Re: Changing hostname in Cocoa?
  • Index(es):
    • Date
    • Thread