[Network] Use VNC to connect to a remote Linux machine

Tags

, , , ,

Let’s assume that you are on computer A and you wan to connect to a remote linux machine B using VNC. You need to follow these steps in order to achieve just that:
1. Install x11vnc and openssh-server on B. The corresponding daemons should start automatically, but if not, then
$ /sbin/service sshd start
$ vncserver :0 -localhost
.

2. Make sure that on B the firewall (if installed and active) allows connection to port 22 from anywhere (for ssh connection) and to 5900 from localhost (i.e., 127.0.0.1). These are the commands (in case of ufw as the firewall) to allow traffic to the specific ports.
$ sudo ufw allow 22
$sudo ufw allow from 127.0.0.1 to any port 5900

3. If B is behind a router firewall, you need to forward port 22 to the router. (You should do this in the router configuration page).
4. On A, create a newfile, vnc2b.sh and enter the following


#!/bin/sh
ssh -f -L 5900:localhost:5900 username@remote_hostname \
x11vnc -safer -localhost -nopw -once -display :0 \
&& sleep 5 \
&& vncviewer localhost:0

and make the file executable:
$ chmode u+x vnc2b.sh.

Next time when you want to vnc to B, just type:
$ ./vnc2b.sh

Reference: here.

[grep] grep, egrep and fgrep

Tags

, , , , ,

I always get confused about various flavors of grep. Here’s a summary to shine some light: (man grep to know more!)

  • egrep or grep -E (in linux only) is extended grep where additional regular expression metacharacters have been added like +, ?, | and ()
  • fgrep or grep -F (in linux only) is fixed or fast grep and behaves as grep but does not recognize any regular expression metacharacters as being special.

[grep] AND, OR, and “these” but NOT “those”

Tags

, , , , , , , , , ,

For grepping line-by-line in a file filename, I often find these very useful

Match pattern1 OR pattern2 in the same line:
$ grep -E 'pattern1|pattern2' filename

Match pattern1 AND pattern2 in the same line:
$ grep -E 'pattern1.*pattern2' filename
The above command searches for pattern1 followed by pattern2. If the order does not matter or you want to search them in either order, then use the follwoing
$ grep -E 'pattern1.*pattern2|pattern2.*pattern1' filename
The pipe enables the OR search which we saw earlier. Another option for this situation (i.e., AND search when the order is not important):
$ grep -E 'pattern1' filename | grep -E 'pattern2'
which basically greps the STDOUT of the first grep.

Match pattern1 AND pattern2, but NOT pattern3 in the same line:
$ grep -E 'pattern1.*pattern2' filename | grep -Ev 'pattern3'
when the order of the first two patterns is important. When that order is NOT important:
$ grep -E 'pattern1' filename | grep -E 'pattern2' | grep -Ev 'pattern3'

Match pattern1 OR pattern2, but NOT pattern3 in the same line:
$ grep -E 'pattern1|pattern2' filename | grep -Ev 'pattern3'

N.B. (1) grep -E may be replaced by egrep.  I used grep -E everywhere in this post assuming a general case of regular expressions as patterns. Lowercase -e is also used for regex, but this is more “basic” than -E which supports “extended” regex, e.g. regular expression metacharacters like +, ?, | and (). (2) The -v flag is for non-matching grep.

[cli] Arbitrary precision calculator in the commandline

Tags

, , ,

Sure, you can use commandline python or ruby (irb); but you may use bc, the inbuilt arbitrary precision calculator, as well. A typical syntax would be:

$ echo "scale=4; 7/9" | bc

where scale=4 tells bc to print the result of 7 divided by 9 with 4 digits after the decimal point.

You may save the following script in a file say cal.bsh in the home directory


#!/bin/bash
echo "scale=4; $1" | bc ;exit

Then make it executable and use it to do the calculation

$ ~/cal.bsh 7/9

For more information: man bc
While you are at it, you may want to have a look at some built-in math functions there, e.g.


s(x) = sin(x)
c(x) = cos(x)
a(x) = arctan(x)
l(x) = Ln(x)
e(x) = exp(x)

These functions are preloaded using the -l flag. Moreover, when this flag is used the default scale is set to 20.

[cli] Install .deb packages in the commandline

Tags

, , , , , , ,

You may use package installer like gdebi to install .deb packages. But would it feel as good as it does when you do it in the commandline?

$ sudo dpkg -i name-of-your-package.deb

Reference: here.

Design a site like this with WordPress.com
Get started