Bash on Windows: practical experiments on the breeding of hedgehogs and snakes

arrived In the Windows 10 upgrade Creators Update has a very interesting ability to run Windows programs in their Linux. Official examples I categorically did not suit the evangelists of Microsoft suggested that I to draw a cow in PowerShell and run Notepad from bash. Wait, really? It's all up to what you thought?

image

As a man passionately waiting for the ability to run. exe files in WSL, I want to share the experience of the proper use of the new features.

Bash on Windows I use for all small mechanization — to extort, to parse, to analyze. Take 10-20 gigs of logs and work your magic on them in search of any sort of what. Take 200 gigs of source data, make VZHUH and shoot a couple of megs results in the base of the site. In General, normal everyday life like everyone else. No?

So my experience

the

Case number one


That's what I was waiting for the ability to run. exe files on WSL.

There are a number of websites for different programs, you want to check that all links to download are on the latest version.

image

Skipping the stage of searching for links and downloading the files, go to extract the desired information.
Do this in two ways.

the

the first Way — an appeal to WMIC


AT CMD call this information looks like this:

the
wmic datafile where name="c:\\Windows\\System32\\cmd.exe" GET /VALUE

/VALUE — to get all possible information

In bash it looks like this

the
cmd.exe /C "wmic datafile where name='c:\\\\Windows\\\\System32\\\\cmd.exe' GET /VALUE"

slashes a lot and all needed

But I need not just the screen display, and to receive and process.

So, in PHP it looks like this:

the
// file name
$file='c:\Windows\System32\cmd.exe';

// get data via WMI
$q="/mnt/c/Windows/System32/cmd.exe /C \"wmic datafile where name='".addslashes(addslashes($file))."' GET /VALUE\"";
$a=exec($q,$b);
$ini=implode("\n",$b);

/mnt/c/Windows/System32/cmd.exe — the full path, which would probably
$b — the result of a multi-line, so get it in an array ($a gets only the last line)
$ini — output format is compatible with a. ini file, sin not to use it — transform the array $b in the text for the subsequent transformation with the help of parse_ini_string()
.

the

second Way — the use of Scripting.FileSystemObject


The solution I found here and drank a little.

In the original version was call the short list with the results that I was not satisfied

the
WScript.Echo(objFolder.GetDetailsOf(objItem,-1));

I replaced this line in a loop to retrieve ALL possible properties file

the
var folder=objShell.NameSpace(namespace);
for (var i=0; i<0xFFFF; i++)
{
fileinfo=folder.GetDetailsOf(null, i);
if (!fileinfo)
{
break;
}
WScript.Echo( fileinfo+" = "+objFolder.GetDetailsOf(objItem,i) );
}

the format of the output is made compatible with a. ini file.

PHP code that calls the bat file was this

the
$q1="/mnt/c/Windows/System32/cmd.exe /C \"C:\\test_exe\\test_exe.bat ".addslashes($file)."\"";
$a1=exec($q1,$b1);
$ini1=implode("\n",$b1);
$ini1=iconv("CP866","UTF-8", $ini1);

Almost the same as in the case of WMI, only have to convert the result to Unicode. ( CP866 — wept )

Bonding the obtained results in a single text and parsim

the
$result_ini="[WMIC]\n".$ini."\n[FileSystemObject]\n".$ini1;
$result_arr = parse_ini_string($result_ini, true, INI_SCANNER_RAW);
print_r($result_arr);

Do split into sections (the second parameter is true) and to avoid the problem, include INI_SCANNER_RAW

VZHUH and get an array of all possible file properties with which convenient to operate.

the
~$ cd /mnt/c/test_exe/
/mnt/c/test_exe$ php test_exe.php

Array
(
[WMIC] => Array
(
[AccessMask] => 1179817
[Archive] => TRUE
[Caption] => c:\windows\system32\cmd.exe
[Compressed] => FALSE
[CompressionMethod] =>
[CreationClassName] => CIM_LogicalFile
[CSCreationClassName] => Win32_ComputerSystem
[CSName] =>
[Description] => c:\windows\system32\cmd.exe
[Drive] => c:
[EightDotThreeFileName] => c:\windows\system32\cmd.exe
[Encrypted] => FALSE
[EncryptionMethod] =>
[Extension] => exe
[FileName] => cmd
[FileSize] => 271872
[FileType] => Application
[FSCreationClassName] => Win32_FileSystem
[FSName] => NTFS
[Hidden] => FALSE
[InstallDate] => 20170318235750.921718+180
[InUseCount] =>
[LastAccessed] => 20170318235750.921718+180
[LastModified] => 20170318235750.921718+180
[Manufacturer] => Microsoft Corporation
[Name] => c:\windows\system32\cmd.exe
[Path] => \windows\system32\
[Readable] => TRUE
[Status] => OK
[System] => FALSE
[Version] => 10.0.15063.0
[Writeable] => TRUE
)

[FileSystemObject] => Array
(
[Name] => cmd.exe
[Size] = > 265 KB
[Item type] => Application
[Modified date] => 18.03.2017 23:57
[Creation date] => 18.03.2017 23:57
[Access date] => 18.03.2017 23:57
[Attributes] => A
[Autonomy] =>
[Availability] = > Available offline
[Recognized type] => Application
[Owner] = > TrustedInstaller
[View] => Program
[Shooting date] =>
[Performers] =>
[Album] =>
[Year] =>
[Genre] =>
[Conductor] =>
[Tags] =>
[Rating] = > No rating
[Authors] =>
[Name] =>
[Theme] =>
[Category] =>
[Comments] =>
[Copyright] => c Microsoft Corporation. All rights reserved.
[no] =>
[Duration] =>
[Speed of flow] =>
[Protected] =>
[Camera model] =>
[Dimensions] =>
[Camera manufacturer] =>
[Organization] => Microsoft Corporation
[File description] = > Windows Command Processor
[Keywords-samples] =>
[Program name] =>
[Duration] =>
[Network] =>
[Repeat] =>
[Location] =>
[Address optional participants] =>
[Optional] =>
[Organizer] =>
[Name of organizer] =>
[Time alert] =>
[The addresses of the required attendees] =>
[Required attendees] =>
[Resources] =>
[State Assembly] =>
[Free/Busy] =>
[Common size] = > 227 GB
[Account] =>
)

)

What was all this stranocum? And here's what.

As I mentioned earlier, Microsoft revealed that we can now draw cows in PowerShell and run notepad.exe from bash.

A discussion Microsoft's translation of this text people find out how honest Linux and can it pile up Docker.

People, you do not look back! I bash made PHP script through exec() launched the bat-the file where the JScript created a ActiveXObject.

It's Kind Of Magic!

And more I could from CMD to do this:

the
C:\test_exe>bash -c "php test_exe.php"

Ah-Ah-Ah-Ah-Ah-Ah!

the

Case number two


Windows 10 upgrade Creators Update I install 6, and last week were distracted by new toys accounting. Accounting has requested the original of the primary.

Eureka, I thought, and put the console petacalco

the Climb in bash and just run the program

the
~$ 2Printer.exe -s "c:\2Printer\_input\*.*"

The result from the printer crawling leaves:

image
(this screenshot — subsequent simulation on a virtual printer, but on the real HP work, too)

The result is:

the
    the
  • the program showed interactivity (trelka asked to press a button)
  • the
  • from baevskogo the window, climbed on the Windows paths
  • the
  • took out the files with Russian names
  • the
  • called office editor for COM objects
  • the
  • send the result to the printer

It is possible not only to write simple clear and use complex programs.

the

Summary


I haven't realized what else you can do, but this thing can do much more than just a conversation with the cows and run the LAMP.

You just have to allow yourself to escape from dichotomiini "either Linux or Windows" and start to breed hedgehogs and grass snakes in the most incredible proportions, and sequences.
Article based on information from habrahabr.ru

Комментарии

Популярные сообщения из этого блога

A Bunch Of MODx Revolution + LiveStreet

Monitoring PostgreSQL with Zabbix

PostgreSQL load testing using JMeter, Yandex.Tank and Overload