Posts tagged ‘Linux’

Redirect a System Process into Java Application for Windows and Linux

The output or input redirection is often used by the command line script guys. The output redirection (“>”) is usually to the file instead of the standard output. The input redirection (“<”) is to have a file as input instead of the standard input. It is sometimes needed in the programs too.

Interoperability with other programs is another concept especially when developing interoperable application. To do that the programming framework often sports different libraries for interoperability. However sometimes, for a simple code you don’t want to call the low level heavy libraries for doing the operation while there is a simple workaround.

There might be three ways of doing it.

  1. Trusting to the environment shell or command prompt.
  2. Use the Java internal process libraries and do the redirection ourselves.
  3. Use JNI class libraries for doing cross platform operation with the native code.

In one of the projects I was working with one of the earthquake modules that does some calculations. The program is in compiled form that it won’t worth the reimplementation. Instead I needed to call from the environment and display the output.

In order to use the system command prompt it is needed to use the “exec” method of the Runtime. This solution might not be guaranteed to work if the command line is broken or it is not with the standard names. Also it is not really beautiful to have if statements detecting the operating system and behaving on that.

Runtime.getRuntime().exec(cmdCommand);

void creatproc()
{
	String osName = System.getProperty("os.name");
	Process p;
 
	if (osName.startsWith("Windows"))
	{
		String cmdwin = "cmd /c C:EWv6.2binhyp2000.exe < C:EWv6.2bininput.txt";
		try
		{
			File f = new File("C:EWv6.2bin");
			Runtime.getRuntime().exec(cmdwin, null, f);
		}
		catch (IOException e)
		{
 
			e.printStackTrace();
		}
	}
	else
	{
		String[] cmdlinux = new String[3];
		cmdlinux[0] = "/bin/sh";
		cmdlinux[1] = "-c";
		cmdlinux[2] = "hypo2000 < input.txt";
		try
		{
			Runtime.getRuntime().exec(cmdlinux);
		}
		catch (IOException e)
		{
 
			e.printStackTrace();
		}
	}
}

After detecting the operating system it for windows it is needed to call cmd and for Linux> it is needed to call sh. There is one implementation between the command prompt of those operating systems as well. Likewise Windows could tell the difference of a single string while in Linux you have to specify the command arguments as the arrays of string. Other than that everything works exactly the same as you would expect.

A better implementation would be to use the java.io.BufferedReader; and feed the process that we have opened with the file contents. It is like implementing the behind scenes in our Java code. We create the process and write to the process output while reading from the file.

public void Redirect()
{
	BufferedReader freader = null;
	PrintStream fout = null;
	java.io.PipedReader pRead = null;
	PipedOutputStream po = null;
	Process p = null;
	try
	{
		freader = new BufferedReader(new FileReader("C:EWv6.2bininput.txt"));
 
	}
	catch (FileNotFoundException e1)
	{
 
		e1.printStackTrace();
	}
	try
	{
		File f = new File("C:EWv6.2bin");
 
		p = Runtime.getRuntime().exec("C:EWv6.2binhyp2000.exe",
				null, f);
		fout = new PrintStream(p.getOutputStream());
	}
	catch (IOException e)
	{
 
		e.printStackTrace();
	}
	String line = "";
	try
	{
		while ((line = freader.readLine()) != null)
		{
			fout.println(line);
		}
		fout.flush();
	}
	catch (IOException e1)
	{
 
		e1.printStackTrace();
	}
	try
	{
		p.waitFor();
	}
	catch (InterruptedException e)
	{
 
		e.printStackTrace();
	}
}

I found the usage of Java process libraries better than the others, but for quick solution the others might be helpful as well. Also it doesn’t look good in terms of “portability” of the application code.

Blog Writing Software: Live Writer vs Word 2007 or Google Writer ?

I usually don’t use WordPress’ “write post” page to write blog entries unless I am using linux. Although there are some web clients as well, it is not better than wordpress’ post screen. I was using Windows Live Writer but nowadays I have switched to Word 2007 to post blogs. First of all, Word 2007 offers a reach interface comparing to Windows Live Writer. On the other hand Live Writer’s plugin support is better than Word’s; however I don’t use any plugin for the user interface. Second, Windows Live Writer is so slow to start and work. Windows Live Writer is not as good at formatting html as Word 2007 does. With Word 2007 you have the benefits of the rich platform, especially if you have a document written in word, it is easy to post it.

I am surprised with the support of many blog platforms, including wordpress of course.

sshot-24.pngsshot-25.png

You are able to use existing documents to format your post and the interface is nice to use for formatting the post.
sshot-26.png
sshot-27.png

Secure Remote Desktop on Linux and Windows

Remote Desktop is one the great features added to Windows since XP.  From then many clients exists for connecting to remote desktops including linux, Mac OS X. Remote desktop connection uses Remote Desktop Protocol (RDP) and the protocol has 128 bit encryption; however it is possible to decrypt entire connection because of its implementation.  We need to use some other layers to make the connection secure

TLS/SSL can be used to secure the connection but it is for server systems. Also for cross platform issues it might not be the best solution. Linux’s famous secure desktop shell (ssh) can be used for such purpose. SSH is not just a remote shell, more importantly it provides TCP tunneling and port forwarding with the built-in encryption of course.

You need to have an ssh server either on windows or linux machine that is accessible from the outside world. For remote connections normally you need to open port 3389, in this case only ssh server port needs to be open from the firewall. For windows ssh server OpenSSh for Windows might be a good choice for client and server. Simply you need to install ssh server and add users to the server.

What you need to do is to logon the remote system and ask the system to redirect you to a machine with the port number. As a result you get an encrypted tunneled connection to your remote machine.

can@host-174-92:~> ssh -L 3389:192.168.1.111:3389 -C 112.232.121.111 -l can
The authenticity of host ’112.232.121.111′ can’t be established.
RSA key fingerprint is 47:da:4e:ab:94:2b:d7:39:cc:19:17:33:55:6a:73:61. Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added ’112.232.121.111′ (RSA) to the list of known hosts.
can@112.232.121.111′s password:
Last login: Fri Oct 13 15:49:55 2006 from x

can@host-174-92:~> rdesktop -u username -a 24 localhost

.

Linux

On linux you use the remote desktop client rdesktop for connection remote desktop servers. Since the redirection is done to localhost with the previous command. Connection to local rdesktop will actually connect us to the remote.

can@host-174-92:~> rdesktop -u username -a 24 localhost

.

logon

Remote Desktop

Visual Studio

Windows

In windows the process is almost the same unless you have a Windows XP Pro or Vista Ultimate Editions. The problem with those versions is that they have a remote desktop server running on port 3389. In that case, you need to tunnel through an unused different port. For instance 3390 should be available. The only thing you will change is the port connection to the ssh server.

openssh -L 3390:192.168.1.111:3389 -C 112.232.121.111 -l username

.

With this command you redirect your system’s 3390 port to remote system’s 3389 port. Of course the ports must be the server’s port. After that we just connect using Windows Remote Desktop client with the adress “localhost:3390″. We will be connected to the remote host than after.

Conclusion

As a result on your corporation, the only port to open to the outside world is the ssh server port which is usually port 22. From that you can redirect every traffic to an internal machine using secure connection and tunneling. This is not limited to remote desktop connections of course, you can use some other services or protocols to make them more secure.  

Dual boot linux and windows with windows boot manager — Windows Vista and Windows 7

They are many documents on the web for doing that. However most of them needs to have an installed linux based system to do that. Installing linux without destruction your harddisk is possible for years. I assume that you have windows installed. Here is the walkthrough to do that.

  1. Allocate space for linux partition. The main partition for linux has to be primary partition just like your windows partition. You might need a partition tool to resize and change partition type to do that operation.
  2. Install linux on the partition that you have just created. On the installation screens, for the boot loader, select the partition itself as the partition (don’t select mbr). When the installation has finished, your computer restarts and windows will be restarted. Don’t be afraid linux is somewhere in your disk.
  3. You need dd for windows to set linux to the boot manager.
  4. Extract the contents of dd in a folder and run
    dd --list

    D:\Tools\dd>dd –list
    rawwrite dd for windows version 0.3.
    Written by John Newbigin
    This program is covered by the GPL. See copying.txt for details
    Win32 Available Volume Information
    \\.\Volume{c5d941f0-8093-11da-b7d7-806d6172696f}\
    link to \\?\Device\HarddiskVolume1
    fixed media
    Mounted on c:\
    \\.\Volume{c5d941f1-8093-11da-b7d7-806d6172696f}\
    link to \\?\Device\HarddiskVolume3
    fixed media
    Mounted on d:\
    \\.\Volume{5c5aa360-7406-11da-b7c2-806d6172696f}\
    link to \\?\Device\CdRom0
    CD-ROM
    Mounted on e:\
    \\.\Volume{23c0e842-75dd-11da-a45d-000e3536c876}\
    link to \\?\Device\CdRom1
    CD-ROM
    Mounted on x:\
    NT Block Device Objects
    \\?\Device\CdRom0
    \\?\Device\CdRom1
    \\?\Device\Harddisk0\Partition0
    link to \\?\Device\Harddisk0\DR0
    Fixed hard disk media. Block size = 512
    \\?\Device\Harddisk0\Partition1
    link to \\?\Device\HarddiskVolume1
    \\?\Device\Harddisk0\Partition2
    link to \\?\Device\HarddiskVolume2
    Fixed hard disk media. Block size = 512
    \\?\Device\Harddisk0\Partition3
    link to \\?\Device\HarddiskVolume3
    \\?\Device\Harddisk0\Partition4
    link to \\?\Device\HarddiskVolume4
    Fixed hard disk media. Block size = 512


    to find the linux partition.

  5. Once you think you’ve found it just use this command to generate the boot file.
    dd if=\\?\Device\HarddiskVolume2 of=linux.boot bs=512 count=1

    D:\Tools\dd>dd if=\\?\Device\HarddiskVolume2 of=linux.bot bs=512 count=1
    rawwrite dd for windows version 0.3.
    Written by John Newbigin
    This program is covered by the GPL. See copying.txt for details
    1+0 records in
    1+0 records out

    Here Partition2 is the linux partition. You need to feel that from the dd –list output

  6. Move the generated file to the root C:
  7. Then add
    C:\LINUX.BOOT="Linux"

    to boot.ini file.

Vista Update

The above process is still working for Windows Vista beside boot.ini file. Instead we need to bcdedit to add the new entry.

Run cmd and execute the following commands to add the linux entry

bcdedit /create /d “Linux” /application BOOTSECTOR
bcdedit /set {LinuxID} device boot
bcdedit /set {LinuxID} PATH \LINUX.BOOT
bcdedit /displayorder {LinuxID} /addlast
bcdedit /timeout 5

Here you are you have used windows boot manager to do that inside windows.

Mono on Windows just like linux

There is two live cds especially designed for mono on linux.

  1. Monoppix
  2. Mono Live

Both distributions include mono librarires and tools. Mono Live distribution includes some applications written in mono like beagle or tomboy. Also it includes some known ASP.Net web applications. As a result it is 200Mb more than monoppix.
These cds are live cds meaning that you can boot from them and work on them without the need of a harddisk. As .net developer you will probably use windows and maybe you want to give a try to the opensource alternative. It is difficult to switch to live cds than go back to windows. Instead there is a tool to emulate your processor on windows (on linux as well actually) called qemu. This tool can also work on windows. You can download qemu in windows. You can boot any live cd distribution with this tool.

Simply just edit your .bat file
qemu -L . -m 300 -cdrom ./monoppix.iso
qemu -L . -m 300 -cdrom ./monolive.iso

Monoppix