LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Networking (https://www.linuxquestions.org/questions/linux-networking-3/)
-   -   SSH X forwarding in program using "system" command (https://www.linuxquestions.org/questions/linux-networking-3/ssh-x-forwarding-in-program-using-system-command-4175439472/)

JayKemper 11-30-2012 11:31 AM

SSH X forwarding in program using "system" command
 
I've run into a nuance of ssh that I'd like to solve. Here's the setup:

I'm running a GUI program that launches commands via the "system" function. One such command is 'ssh -X account@remote "./startup"'. Inside the 'startup' script, I set the display variable to localhost:10.0. The desired result is that the X window of the remote program shows up locally. It works when in a terminal:

1. ssh -X account@remote <enter>
./startup

Setting DISPLAY explictly is redundant here because the .bashrc is run.

2. ssh -X account@remote "./startup"

Setting DISPLAY explicitly in script needed because it doesn't get set.

In the program, the call 'system("ssh -X account@remote \"./startup\"")' yields a 'Can't open display: localhost:10.0'. Here's the kicker though, it works if ANOTHER terminal is already sshed into that machine. This probably has something to do with the .Xauthority file.

My question is how can I make this work WITHOUT having to ssh into the machine from another terminal? Any other approaches to this are welcome.


Test script on remote machine (replace gcalctool with any X windows program)
Code:

#! /bin/bash

export DISPLAY=localhost:10.0

gcalctool

Program to execute command:
Code:

#include <stdlib.h>
int main(int argc,char *argv[])
{
    system("ssh -X account@remote \"./startup\"");
    return 0;
}

BTW, using mostly CentOS 5, and one machine is Fedora Core 2 (I know, but it's got special hardware)

JayKemper 11-30-2012 01:04 PM

I found the error...

I use an alias for ssh=ssh -X. When running the 'system' call, it doesn't use the alias. I had to explicitly use the -X for the X forwarding to work.

There is no way anyone could have guessed that from the information I provided. Lesson learned - 'system' does NOT run your .bashrc script!

Reuti 12-01-2012 02:46 PM

Instead of putting the -X into an alias, I suggest to define it in your ~/.ssh/config file which will always be used:
Code:

Host foobar
    hostname remote
    user account

Host *
    ForwardAgent yes
    ForwardX11 yes
    ForwardX11Trusted yes
    Compression yes



All times are GMT -5. The time now is 04:52 PM.