Submit Hint Search The Forums LinksStatsPollsHeadlinesRSS
14,000 hints and counting!

Set X11 authority file hostname via a script UNIX
When plugging in or out the network cable (ie when moving a laptop), new X11 applications can sometimes no longer be launched. The usual way to get around that is to quit and restart the X11.app: this creates a new ~/.Xauthority file which works.

However, this means quitting running applications with open windows, which can be bothersome. Fortunately, there is another way: with a text editor (like emacs, not TextEdit.app), just edit the file ~/.Xauthority so that the hostname appearing close to the beginning of the file reflects the current hostname. The currnet hostname would have changed after plugging in or out of the network, and can be seen with the command hostname. When unplugged, the name, for instance, ends with .local.

The following perl script does this automatically. It would be nice to have it executed automatically whenever the name of the machine changes: anyone knows how to do that in a clean way (other than having a dedicated daemon running)?

#!/usr/bin/perl -w

$hostname=`hostname`;
chomp $hostname;
$xauthority=$ENV{'HOME'}.'/.Xauthority';
undef $/; # read the whole file in a variable
open IN,$xauthority;
$_=;
s/^x01x00x00x0d([^x00]+)/x01x00x00x0d$hostname/;
print "Old hostname in ~/.Xauthority was $1, replaced by $hostnamen";
rename $xauthority, "$xauthority.back";
open OUT, ">$xauthority";
print OUT;
exit;
[robg adds: I haven't tested this one...]
    •    
  • Currently 2.00 / 5
  You rated: 3 / 5 (4 votes cast)
 
[7,907 views]  

Set X11 authority file hostname via a script | 7 comments | Create New Account
Click here to return to the 'Set X11 authority file hostname via a script' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
Set X11 authority file hostname via a script
Authored by: pjfoley23 on Apr 27, '04 01:33:47PM
Take a look at Peripheral Vision. It allows the exectution of programs or Applescripts on device connection/disconnection type events. Also gives a nice, OS X style notification (like the volume and brightness indicators) when devices become available or unavailable. Peripheral Vision

---
--pjfoley23--

[ Reply to This | # ]

Set X11 authority file hostname via a script
Authored by: ssevenup on Apr 27, '04 05:59:42PM
The following perl script does this automatically. It would be nice to have it executed automatically whenever the name of the machine changes: anyone knows how to do that in a clean way (other than having a dedicated daemon running)?

Try contacting Joel at afp548.com. It should be pretty trivial to make configd take care of this if you knew what file to add or edit. Joel is a professional Mac trainer and he talked about configd at the class I attended. Otherwise some Googling on configd may turn up something.

---
Mark Moorcroft
ELORET Corp. - NASA/Ames RC
Sys. Admin.

[ Reply to This | # ]

Set X11 authority file hostname via a script
Authored by: TokyoJimu on Apr 28, '04 02:02:30AM
I suspect the script was mangled by HTML. I get an error on the line

$_=;
I also get a warnng:
Name "main::IN" used only once: possible typo at bin/xupdate line 7.

And $hostnamen should be $hostname I presume.

I can't get it working in any case.

[ Reply to This | # ]

Set X11 authority file hostname via a script
Authored by: Orj on Apr 28, '04 04:34:24AM

Sorry, this was my first posting, and my script got completely scrambled by the html interpreter that ate all backslashes (\). Here is a plain text version which looks better:

#!/usr/bin/perl -w
# Usage: resetXauth
# updates the ~\.Xauthority for changed hostname (eg for mobile connections)
# possible BUG: not sure this works for any xauth method; tested only with
# MIT-MAGIC-COOKIE on a unix system (actually Mac OSX 10.3)

$hostname=`hostname`;
chomp $hostname;
$xauthority=$ENV{'HOME'}.'/.Xauthority';
undef $/; # read the whole file in a variable
open IN,$xauthority;
$_=<IN>;
s/^\x01\x00\x00\x0d([^\x00]+)/\x01\x00\x00\x0d$hostname/;
print "Old hostname in ~/.Xauthority was $1, replaced by $hostname\n";
rename $xauthority, "$xauthority.back";
open OUT, ">$xauthority";
print OUT;
exit;

---
In theory, theory and practice are the same. In practice, they're not.



[ Reply to This | # ]
Set X11 authority file hostname via a script
Authored by: hlk on Apr 28, '04 08:10:45AM

Hi there

Problem: hostname changes on laptop
Solution: dont change ...

I changed my iBook hostname in /etc/hostconfig:
##
# /etc/hostconfig
##
# This file is maintained by the system control panels
##
# Network configuration
HOSTNAME=-AUTOMATIC-
to
HOSTNAME=otto

Then I made sure that "otto" always resolves to my machine in /etc/hosts:
::1 localhost otto.kramse.dk otto otto.local localhost6
127.0.0.1 localhost otto.kramse.dk otto otto.local

This should make everything work nicely - standard X11 UNIX way :-))

Dont go making things any more complicated ;-)

Best regards

Henrik



[ Reply to This | # ]
Set X11 authority file hostname via a script
Authored by: n8gray on Apr 28, '04 03:26:08PM

This post is exactly right. There should really be a way of amending a hint to say "it turns out the correct way to do this is..."



[ Reply to This | # ]
Set X11 authority file hostname via a script
Authored by: Fletch on Apr 28, '04 10:47:25AM

You could of course just use xauth itself to modify the file rather than mucking with its data.


#!/bin/zsh
cookie=( $( xauth list | grep localhost | awk '{ print $2, $3 }' ) )

if [[ -z "$cookie" ]] ; then 
  print "No xauth data available for localhost; X11.app not running?"
  exit 1
fi

newname=$( hostname )

if [[ $# -eq 1 ]] ; then
  newname="$1" ; shift
fi

for i in ":0" "/unix:0" ; do
  xauth add "${newname}${i}" $cookie
done

exit 0


[ Reply to This | # ]