Operating System - HP-UX
1820234 Members
3366 Online
109620 Solutions
New Discussion юеВ

Re: Script to export the DISPLAY between two machines

 
Mark_541
Frequent Advisor

Script to export the DISPLAY between two machines

Hello,

I would like to know if any one has an idea how to export (via script) the DISPLAY of machine A to another machine B (using, SSH, Telent or Expect).

Thanks in Advance
13 REPLIES 13
Rodney Hills
Honored Contributor

Re: Script to export the DISPLAY between two machines

ssh by default will pass the current setting for DISPLAY on machine A to machine B.

Rod Hills
There be dragons...
Mark_541
Frequent Advisor

Re: Script to export the DISPLAY between two machines

Hi Rodney,

Are you sure ?

Personally, I do not think so..
Rick Garland
Honored Contributor

Re: Script to export the DISPLAY between two machines

Not sure what you mean by exporting the DISLAY environment variable.

What I do, issue the command 'who -um' and this will give a 1 line response. The last field is the IP/host you are coming from.

export DISPLAY=`who -um | awk '{print $NF}'`:0

I put this in the .profile

If it resolves then you get the host name. If you are on a Linux system, the IP address will be enclosed in parenthesis, use the 'tr -d ()' command to get rid of the parenthesis.

All of this is done within the $HOME/.profile so the DISPLAY is set once you log in.

Rodney Hills
Honored Contributor

Re: Script to export the DISPLAY between two machines

Do a "man ssh". This is a section on X11 processing-

If the ForwardX11 variable is set to ``yes'' (or, see the description
of the -X and -x options described later) and the user is using X11
(the DISPLAY environment variable is set), the connection to the X11
display is automatically forwarded to the remote side in such a way
that any X11 programs started from the shell (or command) will go
through the encrypted channel, and the connection to the real X server
will be made from the local machine. The user should not manually set
DISPLAY. Forwarding of X11 connections can be configured on the
command line or in configuration files.

HTH

-- Rod Hills
There be dragons...
Mark_541
Frequent Advisor

Re: Script to export the DISPLAY between two machines

Hi Rodney,

I have machine 1: echo $DISPLAY >> 1.2.3.4 (for instance).

I would like from machine 1 connect via SSH to machine 2 and do: export DISPLAY=1.2.3.4 (export the display of machine 1 to machine2)

BR
Jannik
Honored Contributor

Re: Script to export the DISPLAY between two machines

Hey Mark,

This would do the export if you are at display 0 from server one:

ssh server 2:
Execute command:
export DISPLAY=`who -um | awk '{print $8}' | tr -d '()'`:0.0

Test it with:
xclock

But you could enable X-forward in ssh (if you config are at /etc/ssh):
vi /etc/ssh/sshd_config
# X11 tunneling options
X11Forwarding yes
X11DisplayOffset 10

Then restart you sshd deamon on the changed system. You may need to change X11 options on the connecting server as well.

Regards,
Jannik

jaton
Doug O'Leary
Honored Contributor

Re: Script to export the DISPLAY between two machines

Hey;

In addition the the sshd_config:X11Forwarding yes mentioned in the previous post, ensure the ssh_config:ForwardX11 yes is also set. The one in the sshd_config tells the daemon whether or not it's possible, the one in ssh_config determines whether it's on by default. That can be tweaked by user in a ~/.ssh/config file if needed.

HTH;

Doug

------
Senior UNIX Admin
O'Leary Computers Inc
linkedin: http://www.linkedin.com/dkoleary
Resume: http://www.olearycomputers.com/resume.html
Peter Nikitka
Honored Contributor

Re: Script to export the DISPLAY between two machines

Hi Mark,

this is some code of my multi-OS profile concerning the DISPLAY variable. You should run this piece of code only, when you have a tty, so put it in block like
if [ -t 1 ]
then
...
fi

#### START
get_os () { OSs=$1; HOST=$2; OSr=$3 ;}
get_os `uname -snr`
export HOST OSs OSr

if [ -z "$ENVONLY" ] # SGIonly
then
tty=`tty | awk -F/ '{if ($NF ~ "tty") print(substr($NF,4,length($NF)-3))
else print $NF}'`
fi

case $OSs in
HP-UX)
if [ "$tty" = con ]
then remhost=
else remhost=`who am i -R | awk '{print(substr($NF,2,length($NF)-2))}'`
fi
;;
DomainOS)
if [ "$tty" = display ]
then remhost=
else remhost=`who am i | awk "/$tty /"' {print $NF}'`
fi
;;
SUN*)
remhost=`finger -fhmpsw $LOGNAME | awk "/$tty /"' {print $NF}'`
;;
IRIX*) remhost=$REMOTEHOST
;;
Linux) remhost=`who am i | awk '{print(substr($NF,2,length($NF)-2))}'`
;;
SCO*)remhost=`who -mx | awk '{print $NF}'`
;;
*)
remhost=`finger -fhmpsw $LOGNAME | awk "/$tty /"' {print $NF}'`
;;
esac
# post-processing of remhost
case "$remhost" in
'') remhost=localhost
disp=:0.0 ;;
*:0.0) disp=$remhost
remhost=${remhost%:*} ;;
*:*) remhost=${remhost%:*}
disp=$remhost:0.0 ;;
*.*) remhost=${remhost%%.*}
disp=$remhost:0.0 ;;
*) disp=$remhost:0.0 ;;
esac
DISPLAY=${DISPLAY:-$disp}
# or overide:
# DISPLAY=$disp

export DISPLAY
#### FINI


mfG Peter
The Universe is a pretty big place, it's bigger than anything anyone has ever dreamed of before. So if it's just us, seems like an awful waste of space, right? Jodie Foster in "Contact"
Mark_541
Frequent Advisor

Re: Script to export the DISPLAY between two machines

Hi Guys,

I think there are a miss-understanding of the problem. Let me explain you the situation:

1/Server1 (where I am logged as user1) from PC: echo $DISPLAY gives 1.2.3.4:0.0

2/ Server2 (where I would like to connect as user2 via SSH without password and export the display to the same value as server1). Some thing like: export DISPLAY=1.2.3.4:0.0

3/ server1-user1# ssh user2@server2 script

==> So, as user1 at server1, I will connect via SSH to server2 as user 2 (without password) : this step is OK.

Then I would like to export the DISPLAY of server2/user2 to the same value as user1/server1.

Note: 1.2.3.4 may changes depending on the PC I am using.

Hope now it is well explained

Thx in Advance
Mark_541
Frequent Advisor

Re: Script to export the DISPLAY between two machines

Any help plz ?
Rick Garland
Honored Contributor

Re: Script to export the DISPLAY between two machines

Sounds as if you are needing to parse the output of 'who -u' command. (As opposed to the 'who -um command which will only This command will list all connections to a server including the IP/hostname.)

who -u | grep user1 | awk '{print $NF}' | tr -d ()


Parse the relevant info from the connection of user1/server1 and pass that to the export DISPLAY for user2/server2.

Again, this can be included in the .profile so it will execute on login. Can also make it a standalone script.

Mark_541
Frequent Advisor

Re: Script to export the DISPLAY between two machines

Yeah but may be there are many users connected to server2 using the same login

BR
Rick Garland
Honored Contributor

Re: Script to export the DISPLAY between two machines

The who -u command will list out the all the users currently connected. It will also list your specific session. Granted there may be many users with the same login but there will only be 1 line that describes your specific login session - this line provides the IP or host name at the end.

root pts/1 Apr 13 08:48 0:11 1337 (1.2.3.4)
root pts/2 Apr 13 08:49 0:10 1338 (1.2.3.5)

In this example I have 2 login sessions for root but they have unique IP addresses from where they logged in from.

If you do the command "who -u | awk '{print $NF}' | tr -d ()" then you will get only the IP addresses. Match that up with the PC you are working on (what is IP of the PC you are working on?).

So the result is
1.2.3.4
1.2.3.5

Which IP address are you wanting, export the DISPLAY with that IP.

Works with hostnames as well if you have name resolution