Set DISPLAY variable constantly in fish - wsl-2

Following Situation I need to setup the DISPLAY Variable for my WSL2 to transmit goutput to the Xserver running on my Host-System.
In general I would do this by using my .bashrc:
export DISPLAY=$(ip route list default | awk '{print $3}'):0
So I started by setting the DISPLAY Variable with
set -Ux DISPLAY $(ip route list default | awk '{print $3}'):0
which worked in the first place.
The Issue: The Variable is now set inside .config/fish/fish_variables as SETUVAR --export DISPLAY:<MY-IP>:0
Seems fine for the moment but since my Homenet uses DHCP it might happen that my IP is changing. How do I set the variable by calling the top used command?

Your mistake was using set -U. That creates a "universal" variable. Instead, simply do set -x in your ~/.config/fish/config.fish so the var is created every time you start a fish shell. See also https://fishshell.com/docs/current/faq.html#why-doesn-t-set-ux-exported-universal-variables-seem-to-work. Universal variables shouldn't be used for values that can change each time you start a fish shell or that might be different for concurrently running shells.

Kurtis's answer would normally be correct, but this is WSL2, and there's (IMHO) a better solution on WSL2 that can use fish universal variables.
set -Ux DISPLAY (hostname).local:0
As long as the hostname matches the Windows Computer Name (which it should and does by default), then that will always use mDNS to return the correct address, even if the IP has changed.
Note that you'll need to remove the global variable definition from ~/.config/fish/config.fish or else the universal will be shadowed by the global.
Explanation:
You might think that it is the dynamically assigned DHCP address changing that is causing the problem, but that's not actually it. The IP address that you get back from ip route list default | awk '{print $3}' is not the one that is assigned to Windows by DHCP on your home network.
It's actually the address of a virtual switch provided by WSL2 that allows WSL2 to communicate with the Windows host (and beyond).
When the Windows IP address changes, it doesn't matter to this virtual switch. Side note: I just confirmed this to make sure on WSL2 by changing my Windows IP manually.
The problem here is actually that the switch address changes each time the computer (or WSL2) restarts. This is why most instructions for setting your DISPLAY on WSL2 tell you to map it to this address, rather than hardcoding it.
As Kurtis said, however, this type of assignment would (typically) be a problem if you were using a fish universal variable, since the virtual switch IP does change each reboot.
But WSL2 provides a convenient mDNS lookup for the address, in the form of <computername>.local. By hardcoding that into the universal variable, DISPLAY should always resolve correctly, even after the IP address itself changes.

Related

have to set port every time in command line when trying to trigger any Perforce command

I'm new to Perforce, need to work in command line, i'm so confused why all the commands on the tutorial websites all write like $p4 command, but I have to add the port every time when try to trigger that command, for an example, for $p4 sync, i have to type $p4 -p myhost:myport sync every time even after I logged in, if i missed the host and port, like if i just typing $p4 sync, the program will just hang in there... anyone can provide some suggestions?
There are a couple ways to make myhost:1666 the default. These are, in the order of decreasing precedence:
command-line options (like -p myhost:1666)
the P4CONFIG file
environment variables (%P4PORT%)
on Windows, the registry (use p4 set P4PORT=myhost:1666 to set it)
See the docs here.
In your case, I guess the registry (if on Windows) or the env. var. (if on Linux) is the best option.
Type:
p4 set P4PORT=myhost:myport
From that point on, "myhost:myport" will be used as the P4PORT when you run commands.
If you use multiple workspaces/servers, P4CONFIG files make it easy to have different sets of settings (based on your working directory), but for a single value, "p4 set" is a nice persistent one-shot method.

xterm do not refresh page after ssh

when I sshed into another machine in xterm, the character I deleted by either backspacce or ctrl+k, remains on the screen. but physically they have been deleted, as when you exectue the script, the machine responds in a way that the delete has conducted. The only way to display properly is to let the cursor moving through the deleted words, either by space or typing other characters.
Same thing applies when you using vim. specifically after page down, the screen displays a overlap between the previous page and current page.
The screen displays properly without ssh.
I am using gentoo with the following compile flag (if it is related).
[ebuild R ] x11-terms/xterm-314 USE="openpty truetype unicode -Xaw3d -toolbar" 0 KiB
Sounds like a locale problem. Some Linux's by the way honor an stty -iutf8 setting, and others may not. Either that, or a difference in locale settings on the local and remote machines is the first place to check.
Thomas's answer led me to a solution for this very same problem. My LANG environment variable was incorrectly set on my local machine. For my case,setting LANG=en_US.UTF-8
fixed the problem.

Advantage/Disadvantage of using uname -n vs. VAR=$(uname -n) in script?

I have a script that on startup of an application is looking for the host. I am declaring a variable at the beginning of the script VAR=$(uname -n) and calling that variable as needed throughout the script. Is there any advantage/disadvantage to just using $(uname -n) throughout the script instead of just calling the variable each time?
If you use a variable it is slightly more efficient, as it will spawn the program only once. You are also sure that the value does not change (which is not typically the case). You also need to handle execution errors only once, so I would say: go for it.
(And you should use the full path to uname or be sure to sanitize the PATH before using relative commands).
BTW: if you call the variable UNAME instead of VAR, then it is also less confusing :)
BTW2: the uts_name you get from this method might not always be the right hostname. Hard to say without knowing for what you use it.

Ubuntu: wait for network link up and execute a bash command

In Ubuntu (the latest distro is fine), I want to reboot a router and inside a bash script I'd like to have a command that waits for the network link to be up again and, when it detects that, it has to start a bash command.
I could implement this with some kind of polling loop, but the ideal solution would be to have a bash command that, when executed, waits for the link to be up and automatically executes a bash command that I gave to it.
I read something about dbus (and dbus seems the way to go) but it also seems that it takes too much time to fully understand how to use it properly. I was suggested to check if a tool like ethtool was able to do that kind of "wait and execute" but in the man pages I didn't find anything about it.
Note: I forgot to say that I'd like the command to check if the PHYSICAL layer of the link is up. So solutions working at upper layers are not accepted. Moreover, solutions involving putting scripts inside directories (such as/etc/network/if-up.d) are not accepted too.
Any ideas?
Thank you
The event listener I suggested:
inotifywait -e modify /sys/class/net/eth0/carrier; echo 'Change detected'
When you plug or unplug network cable, it will trigger echo 'Change detected', of course it could trigger just about anything.
And this will run as one off, but I take you know how to make a daemon out of it, if not it will be a good exercise to learn :)
If you want a command to check if the link is up or down use ip :
ip addr show eth0 | grep -Po "(?<=state ).*?(?=\s)"
DOWN
ip addr show wlan0 | grep -Po "(?<=state ).*?(?=\s)"
UP
You can check link is up/down by /sys/class/net/eth0/carrier file
cat /sys/class/net/eth0/carrier
if output is 1 then ethernet cable is plugged in and link is up.
if output is 0 then ethernet cable is removed and link is down.
Note:if you have interface other then eth0 then replace interface like eth2/eth3 in place of eth0

Randomization of environment variable addresses

I am using bash. I have switched off ASLR in Ubuntu 11.04 using
#sysctl -w kernel.randomize_va_space=0
And I have exported a variable from the shell using
$ export MYSHELL=/bin/sh
I wrote a C program to get the address of the MYSHELL:
void main(){
char* shell = getenv("MYSHELL");
if (shell)
printf("0x%x\n", (unsigned int)shell);
}
It spat out 0xbffffe82.
When I used it as a part of my attack for ret-to-libc, the address changes (although by a very small offset).
Why does this happen?
Also when I change the filename of the binary and use the previously successful address, it won't work, and it has been relocated to a different address. Why? In other words, What is the relation of binary names and environment variable addresses? Is this a protection feature by bash? How do I switch this off?
Note: this is not homework.
Stack layout at program startup is documented here. It should be obvious why changing the name of the program (length really) changes the layout.

Resources