Tag: command line interface / cli

ImageMagick error on Windows: “magick: unable to open image ”test’: No such file or directory @ error/blob.c/OpenBlob/3565. magick: no decode delegate for this image format `’ @ error/constitute.c/ReadImage/741.” (SOLVED)

If in Windows 11 open CMD:

cmd

And then run the command:

magick '.\Для теста.jpg' test.png

An error will be received that there is no such file or directory:

magick: unable to open image ''.\╨Ф╨╗╤П': No such file or directory @ error/blob.c/OpenBlob/3565.
magick: no decode delegate for this image format `' @ error/constitute.c/ReadImage/741.

You can see that non-Latin characters are used in the file name, so you might think that this is the problem – that is, the “magick” program does not support alphabets other than English.

But if you try to rename the file and run the following command:

magick 'test file.jpg' new.png

Then the same error will be received again:

magick: unable to open image ''test': No such file or directory @ error/blob.c/OpenBlob/3565.
magick: no decode delegate for this image format `' @ error/constitute.c/ReadImage/741.

How to fix “magick: unable to open image ''test': No such file or directory @ error/blob.c/OpenBlob/3565. magick: no decode delegate for this image format `' @ error/constitute.c/ReadImage/741.”

1. Use double quotes instead of single quotes

If the filename is enclosed in double quotes instead of single quotes, then the command works correctly:

magick ".\Для теста.jpg" test.png

That is, try putting the filename in double quotes. If the problem persists, then it is related to the encoding of the file name.

2. Use PowerShell instead of CMD

In Windows 11 and Windows 10, the default command prompt is PowerShell, not CMD.

If your Windows uses CMD by default, then either set it to use PowerShell in the settings, or run one of the following commands at the command prompt:

powershell
pwsh

In Windows 11 + Windows Terminal Preview + PowerShell 7 command

magick '.\Для теста.jpg' test.png

works without errors.

See also: ImageMagick guide: installing, using, and troubleshooting

Basics of launching and using command line utilities in Windows

Are the command line and command line utilities relevant today?

Windows users are accustomed to using programs (applications) with a graphical user interface, in which actions are performed using mouse clicks or keyboard input.

At the same time, even in Windows there are and can be installed many utilities, programs without a graphical interface, with very useful functionality. As a rule, programs without a graphical interface are designed to perform a highly specialized action or function and are designed for professionals.

Programs without a graphical interface are called “command line utilities”, “programs with a command line interface” (CLI).

There is no need to think that command line utilities are some kind of atavism and something outdated. This is very far from the truth! Entire layers of specialized programs in various fields are developed precisely as utilities with a command line interface.

The purpose of this note is to give a general idea to Windows users about how to deal with the command line. If you have downloaded the utility and cannot run it, then this note is for you!

When I click on the exe file, a black window flickers and then disappears

When you try to launch the command line utility by double-clicking, you will most likely encounter the console window flashing for a second, which immediately closes.

Programs with a command line interface are not designed to be launched by double-clicking. Instead, they need to be run, as you might have guessed, on the command line.

Windows has several environments for executing commands:

  • CMD
  • PowerShell (includes all the features of CMD and provides many cmdlets for administering Windows desktops and servers)

You can also remember Windows Terminal, but this is not a separate environment that has its own commands, but just an application for conveniently entering CMD and PowerShell commands.

To run the program, you need to open a Windows Terminal window (or PowerShell). To do this, press the key combination Win+x, and select “Windows Terminal” or “Windows Terminal (Admin)”:

What to choose: “Windows Terminal” or “Windows Terminal (Admin)”

Utilities can be run with normal user rights, or require running with Administrator rights.

With Administrator rights, for example, the following programs should be launched:

  • Programs that register and install themselves as system services
  • Programs that require low-level access to devices (for example, programs for partitioning or fixing disk errors)
  • Programs that change system settings that only an administrator can change.

An example of programs with a command line interface that require Administrator rights:

  • Apache (web server, it registers and starts itself as a system service)
  • MySQL (DBMS, it registers and starts itself as a system service)

An example of utilities that do not require elevated privileges:

How to run an executable on the command line

Consider running a program with a command line interface using the Apache web server (httpd) as an example.

The first option: you can simply drag and drop the executable file into the command line window. The Apache executable is httpd.exe.

The second option: on the command line, you can change the current working directory to the one where the Apache executable files are located. For example, my program is located in the C:\Apache24\bin\ folder, to change the current working folder, the “cd” command is used, after which the folder you want to go to is indicated, in my case the command looks like this:

cd C:\Apache24\bin\

As you can see from the screenshot, the C:\Users\MiAl folder has been changed to C:\Apache24\bin\.

Now, to run the program, it is enough to type the name of the executable file indicating the current folder. The current folder is indicated by a dot (.), then you need to put a backslash, it turns out like this:

.\httpd.exe

Apache is a network service, that is, a program that uses a computer network for its work. Specifically, Apache listens for incoming connections on port 80 (the service opens a port). For this reason, the Windows Firewall asks whether to allow the Apache HTTP Server program to communicate on the network, select “Allow access”.

Already at this stage, the web server is running, and you can open the address http://localhost/ in a web browser

To stop the service, press Ctrl+c.

How to get help using the utility

Typically, command-line utilities support various options that can be specified with a space after the executable file name.

Also, utilities usually have built-in help on available options, which can be displayed using the -h option or the --help option, which must be specified after the executable file name.

For example:

.\httpd.exe -h

In addition to command line options, many services are configured using configuration files, which are text files with a specific syntax. This is especially common for utilities that came to Windows from Linux, since in this operating system many programs are configured using text files.

In this case, quite often explanations of custom directives are contained in the configuration file itself in the form of notes, or in accompanying documentation.

README, ReadMe.txt, README.md files and software documentation

Utilities are usually accompanied by documentation. For example, this is an Apache archive:

As you can see, the ReadMe.txt file with documentation is attached to the archive – this is documentation from those who compiled Apache for Windows.

Inside the Apache24 folder there are even more files and folders with installation information and more.

This is a PHP archive – it also contains a README.md file that provides help information.

A MySQL archive containing a README file and a documentation folder or a link to the documentation.

Program in PATH environment variable

The above shows that in order to run the program, you need to go to the folder with this program.

Alternatively, you can specify the full path to the executable, for example:

C:\Server\bin\Apache24\bin\httpd.exe

It is this method that works when you drag and drop the utility into the console to run the utility – in this case, the full path to the file is inserted into the command line.

But some commands run from any folder, wherever you are on the command line. For example, try the following command:

find /?

The fact is that for operating systems there is such a thing as the PATH environment variable.

The essence of the PATH environment variable is as follows:

1. The PATH variable is assigned a value that consists of a list of folders, that is, paths in the system.

2. When you run a file on the command line, the operating system tries to find it both in the current folder (where you went with the “cd” command, or open by default), and in each folder specified in the PATH environment variable

3. If the file is found in the current folder, or in any PATH folder, then it is launched.

This is the reason the following command always works:

find /?

The reason is that it is placed in one of the folders listed in PATH (namely C:\Windows\System32\).

To view the current value of the PATH environment variable, run any of the following commands:

Get-ChildItem -Path Env:PATH

$env:PATH

Example PATH content in Windows 11:

C:\Program Files\PowerShell\7-preview;C:\Program Files\ImageMagick-7.1.0-Q16-HDRI;C:\Server\bin\PHP\;C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v11.6\bin;C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v11.6\libnvvp;C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;C:\Windows\System32\OpenSSH\;C:\Program Files\NVIDIA Corporation\Nsight Compute 2022.1.0\;C:\Program Files (x86)\NVIDIA Corporation\PhysX\Common;C:\Program Files\PowerShell\7-preview\preview;C:\Users\MiAl\AppData\Local\Microsoft\WindowsApps

You can place frequently used utilities in a folder included in PATH. Either you can add a new folder to PATH with the utilities you want to run without specifying the full paths to the executable files, or without having to change to the directory with these utilities.

You can set the value of PATH (and other environment variables) both in the GUI and on the command line.

See also:

Do I need to specify the extension of the executable file

When running executable files, it is not necessary to specify the extension, because the console will understand what you mean. For example, the following two commands are identical:

hashcat.exe

hashcat

Conclusion

This note is intended to give you a general idea of how to start and use the command line utilities. For basic information on using a particular utility, see the help displayed with the -h option or the program's documentation.

If you encounter an error when starting the utility, try running the program in an elevated command prompt (with admin rights).

If you get a message that “file not found”, go to the folder with the installed program using the “cd” command, or drag and drop the executable file to the command line.

If you received an error message inside the program when starting the program, then study the utility options and examples of its launch in more detail.

How to run PHP script on the command line (without web server)

How to run PHP script on Windows command line

If you want not only the ability to run PHP from the command line, but also a full-fledged Apache + PHP + MySQL web server on Windows, then complete the installation according to this article.

To run PHP on the command line, you do not need to install a web server, just download and unpack the archive with the PHP interpreter.

Let's start by downloading the latest PHP version for Windows from the official website: https://windows.php.net/download/

There are several options that differ:

  • Version (e.g. 8.0, 7.4, 7.3)
  • Computer architecture, (x64 and x86)
  • Thread Safe or Non Thread Safe

Choose the latest version, between x64 and x86 choose the same version as your webserver. That is, if your Apache is 64-bit, then PHP must be 64-bit as well. Always choose ‘Thread Safe’ version.

There are two links for each file:

  • Zip
  • Debug Pack

Choose Zip because the debug package is only for those who really know why they need it. This does not mean debugging PHP scripts, but debugging the PHP interpreter itself.

If you often run PHP scripts from the Windows command line, it is highly recommended to To add PHP path to %PATH% environment variable on Windows. This eliminates the need to specify the full path to the php.exe file every time.

Now that PHP is installed and the path to php.exe has been added to the Windows environment variable, open a command prompt by pressing the Win+x key combination and select Windows PowerShell.

To check what works fine, look at the PHP help:

php -h

In fact, we are running the php.exe file, but the extension can be dropped. That is, the previous entry is equivalent to

php.exe -h

How to run .php script on Windows and Linux command line

The following command is used to run the .php file in the Windows console:

php -f path\to\file.php

The -f option can be skipped, that is, the previous and next commands are the same:

php path\to\file.php

I created a test file which is located in the path C:\Users\Alex\Documents\PHP\test.php then I can run it in PHP like so:

php -f C:\Users\Alex\Documents\PHP\test.php

or like this:

php C:\Users\Alex\Documents\PHP\test.php

How to pass arguments to a PHP script on the command line

To pass arguments to the script, list them after the filename, separated by a space. If the arguments themselves contain spaces or other characters that are special to the command line shell, then enclose those arguments in single or double quotes.

php path\to\file.php 'arg1' 'arg2' 'arg3' 'arg_n'

An example of running a PHP script with three arguments:

php C:\Users\Alex\Documents\PHP\test.php 'Alex' 'hackware.ru' 'Admin'

How to access arguments in a PHP script

The arguments passed are contained in the $argv array. Moreover, the ordinal number of the argument corresponds to the number in the array. That is, the first argument will be placed in $argv[1], the second in $argv[2], and so on.

The very first element of the array named $argv[0] contains the full path to the script to run.

The content of the test.php file:

<?php

echo 'Name: ' . $argv[1] . PHP_EOL;
echo 'Web site: ' . $argv[2] . PHP_EOL;
echo 'Status: ' . $argv[3] . PHP_EOL;

Let's run it and pass three arguments to the script:

php C:\Users\Alex\Documents\PHP\test.php 'Alex' 'hackware.ru' 'Admin'

How to get data from user in the console in PHP

Thanks to the passed arguments, the script can perform actions not only with the data written in it, but also with other values specified when the script was run.

By the way, when working in a web server environment, that is, when a PHP script performs tasks for a website, the ability to pass arguments to it is implemented using the HTTP GET and POST methods. These arguments are passed before starting the script, and after starting the PHP script, new data cannot be sent – you need to wait for the program to finish running, and, if necessary, run it again with new data.

While the script is running, it may be necessary to enter new data; in the console, this is achieved using a prompt, into which the user can enter a value and press Enter to pass it to the script. In the context of a website, there is no such possibility – to transfer data already during the execution of the script. That is, the console launch of PHP scripts with arguments is not only easier (no need to fiddle with an HTML form), but even more flexible.

PHP uses the readline function to query the user.

This feature works the same on both Windows and Linux. Moreover, on Linux, it has the interactive capabilities of Bash, for example, it saves the input history, to which you can return using the arrows. On Windows, this feature has appeared since PHP 7.1.

If you really need to, you can configure the auto-completion of the input data. All GNU Readline features are covered here. I will only touch on readline, which reads the line entered by the user. With this function, you can specify one optional argument – a string that will be shown to the user at the prompt.

An example of a PHP console script that asks the user for data at a command line prompt:

<?php

$len_min = readline("The minimum number of characters in a password is: ");
$len_max = readline("Maximum number of characters in a password: ");
$base = readline("Password base (0 - digits; 1 - lowercase letters; 2 - uppercase letters): ");

echo "Received initial data: \r\nPassword length from $len_min to $len_max characters ";
switch ($base) {
	case 0:
		echo "and the base of the password is in lowercase digits.";
		break;
	case 1:
		echo "and the base of the password is in lowercase letters.";
		break;
	case 2:
		echo "and the base of the password is in uppercase letters.";
		break;
}

How to execute PHP commands interactively

If you need it, then you can work with the PHP interpreter interactively, entering the code line by line. In this case, the code is executed after pressing the Enter button, but the values of the variables are saved within one session. That is, you can assign a value to a variable and then use it in other lines.

To run an interactive shell:

php -a

How to run individual PHP commands

Use the -r option to run individual commands:

php -r 'echo 2**100;'

ONVIF client with command line interface

ONVIF is an open industry forum that provides and promotes standardized interfaces for effective interoperability of IP-based physical security products.

ONVIF protocol can be found on security cameras. Using ONVIF protocol, you can receive information from IP cameras and control them (set settings, rotate, etc.).

This article will talk about python-onvif - an ONVIF client implemented in Python. This program can be considered the successor to foscam-python-lib - Foscam Python Library for H.264 IP Cameras (FI9821W/P/HD816W/P).

This instruction will show working examples of using ONVIF on the command line.

To install python-onvif run:

sudo pip3 install --upgrade onvif_zeep

If you get an error that the pip3 command was not found, then you need to install the python3-pip package (for Debian based distributions) or python-pip (for Arch Linux based distributions).

You can find various examples of using python-onvif (in Python code, in scripts, in interactive mode, etc.) on the program home page: https://github.com/quatanium/python-onvif. The documentation is not informative, and due to the fact that you need to specify the wsdl directory, the path to which by default differs from that used in the examples, and also differs from distribution to distribution, all examples turn out to be inoperative.

Therefore, I propose to consider the use of python-onvif using an example of working code.

Create an extractor.py file with the following content:

import sys
from onvif import ONVIFCamera

# check if a user has been specified to connect. If not, use an empty string as the username
if len(sys.argv) < 4:
	user = ''
else:
	user = sys.argv[3]

# check if a password was specified for the connection. If not, use an empty string as a password
if len(sys.argv) < 5:
	password = ''
else:
	password = sys.argv[4] 		

# we create an object indicating the host, port, user, password and path to wsdl
mycam = ONVIFCamera(sys.argv[1], sys.argv[2], user, password, '/usr/lib/python3.9/site-packages/wsdl/')

# request and display information about the device
resp = mycam.devicemgmt.GetDeviceInformation()
print (str(resp))

# request and display information about network interfaces
resp = mycam.devicemgmt.GetNetworkInterfaces()
print (str(resp))

# request the address of the media stream
media_service = mycam.create_media_service()
profiles = media_service.GetProfiles()
token = profiles[0].token
mycam = media_service.create_type('GetStreamUri')
mycam.ProfileToken = token
mycam.StreamSetup = {'Stream': 'RTP-Unicast', 'Transport': {'Protocol': 'RTSP'}}
print(media_service.GetStreamUri(mycam))

Notice the line

/usr/local/lib/python3.9/site-packages/wsdl/

You need to replace it with your own value. This line is fine for Kali Linux. And on BlackArch/Arch Linux you need to use the following line:

/usr/lib/python3.9/site-packages/wsdl/

When the Python version changes, the line may change as well.

The path to it can be found by the following algorithm:

sudo updatedb # Update DB with file information
locate accesscontrol.wsdl # We are looking for a file that is in the desired directory

For example found:

/usr/local/lib/python3.9/site-packages/wsdl/accesscontrol.wsdl

We take the entire line, except for the file name, that is, /usr/local/lib/python3.9/site-packages/wsdl/.

Run like this:

python3 extractor.py HOST PORT [USER_NAME] [PASSWORD]

The script makes three separate requests and displays three groups of data: device information, network interfaces, and media stream.

USER_NAME and PASSWORD are optional - if they are absent, an attempt will be made to connect with empty credentials.

Launch example:

python3 extractor.py 118.39.210.69 80

Output example:

{
    'Manufacturer': 'BOSCH',
    'Model': 'AUTODOME IP starlight 7000 HD',
    'FirmwareVersion': '25500593',
    'SerialNumber': '044123455',
    'HardwareId': 'F0004D43'
}
[{
    'Enabled': True,
    'Info': {
        'Name': 'Network Interface 1',
        'HwAddress': '00-07-5f-8b-5d-2b',
        'MTU': 1514
    },
    'Link': {
        'AdminSettings': {
            'AutoNegotiation': True,
            'Speed': 100,
            'Duplex': 'Full'
        },
        'OperSettings': {
            'AutoNegotiation': True,
            'Speed': 100,
            'Duplex': 'Full'
        },
        'InterfaceType': 6
    },
    'IPv4': {
        'Enabled': True,
        'Config': {
            'Manual': [],
            'LinkLocal': None,
            'FromDHCP': {
                'Address': '118.39.210.69',
                'PrefixLength': 24
            },
            'DHCP': True,
            '_value_1': None,
            '_attr_1': None
        }
    },
    'IPv6': None,
    'Extension': None,
    'token': '1',
    '_attr_1': {
}
}]
{
    'Uri': 'rtsp://118.39.210.69/rtsp_tunnel?h26x=4&line=1&inst=1',
    'InvalidAfterConnect': False,
    'InvalidAfterReboot': True,
    'Timeout': datetime.timedelta(0),
    '_value_1': None,
    '_attr_1': None
}

Manufacturer, MAC address, video URI:

    'Manufacturer': 'BOSCH',
        'HwAddress': '00-07-5f-8b-5d-2b',
    'Uri': 'rtsp://118.39.210.69/rtsp_tunnel?h26x=4&line=1&inst=1',

Now that we know how to create an ONVIFCamera object and perform elementary actions with it, we can return to https://github.com/quatanium/python-onvif for more examples.

Also see:

Loading...
X