openssh module in iron python from windows to linux to run commands - python

I am using openssh in iron python to run commands from windows to linux. I have installed openssh in both the machines and the service is running. I have added keys in authorized users even
conn = SSHConnection(ip_addr, login=name)
ret = conn.run('ls')
or
ret = conn.run(['ls'])
I am getting error as follows
"C:\Program Files\IronPython 2.7\lib\site-packages\openssh_wrapper.py", line 164, in run
"C:\Program Files\IronPython 2.7\Lib\subprocess.py", line 675, in __init__
"C:\Program Files\IronPython 2.7\Lib\subprocess.py", line 887, in _execute_child
Error: [Errno 2] The system cannot find the file specified.
What is wrong in the code?

The line in my copy is
ssh_command = self.ssh_command(interpreter, forward_ssh_agent)
where interpreter, if not specified, defaults to /bin/bash. I think it's saying it doesn't see /bin/bash on the remote machine.

Related

Python does not find the Brownie file

I'm following a tutorial on Solidity and Python (for reference https://www.youtube.com/watch?v=M576WGiDBdQ&t=5447s).
I'm at the stage where Python and Brownie have been installed and running through VSCode, but I am getting the below notification when asking for Brownie's version brownie --version INFO: Could not find files for the given pattern(s). Brownie v1.17.1 - Python development framework for Ethereum
When I try to run an extremely basic script on a file called deploy.py (def main(): print ("hello!") ), here is the result :
PS C:\Users\chret\Documents\demo\brownie_simple_storage> brownie run scripts/deploy.py
INFO: Could not find files for the given pattern(s).
Brownie v1.17.1 - Python development framework for Ethereum
BrownieSimpleStorageProject is the active project.
Launching 'ganache-cli.cmd --port 8545 --gasLimit 12000000 --accounts 10 --hardfork istanbul --mnemonic brownie'...
File "C:\Users\chret\AppData\Roaming\Python\Python39\site-packages\brownie\_cli\__main__.py", line 64, in main
importlib.import_module(f"brownie._cli.{cmd}").main()
File "C:\Users\chret\AppData\Roaming\Python\Python39\site-packages\brownie\_cli\run.py", line 44, in main
network.connect(CONFIG.argv["network"])
File "C:\Users\chret\AppData\Roaming\Python\Python39\site-packages\brownie\network\main.py", line 50, in connect
rpc.launch(active["cmd"], **active["cmd_settings"])
File "C:\Users\chret\AppData\Roaming\Python\Python39\site-packages\brownie\network\rpc\__init__.py", line 75, in launch
self.process = self.backend.launch(cmd, **kwargs)
File "C:\Users\chret\AppData\Roaming\Python\Python39\site-packages\brownie\network\rpc\ganache.py", line 73, in launch
return psutil.Popen(cmd_list, stdin=DEVNULL, stdout=out, stderr=out)
File "C:\Users\chret\AppData\Roaming\Python\Python39\site-packages\psutil\__init__.py", line 1312, in __init__
self.__subproc = subprocess.Popen(*args, **kwargs)
File "C:\Program, line line, in in
self._execute_child(args, executable, preexec_fn, close_fds,
File "C:\Program, line line, in in
hp, ht, pid, tid = _winapi.CreateProcess(executable, args,
FileNotFoundError: [WinError 2] The system cannot find the file specified
I've tried uninstalling and reinstalling from Python to Brownie, changing the place where Brownie's files are, but I'm now stuck due to my very limited knowledge (full beginner).
UPDATE
I've reinstalled Python and pipx and brownie once more, and now the message is slightly different, but still doesn't run the script of deploy.py. The script is :
def main():
print("Hello!")
and the current error message is :
PS C:\Users\chret\Documents\demo\brownie_simple_storage> brownie run scripts.deploy.py
INFO: Could not find files for the given pattern(s).
Brownie v1.17.1 - Python development framework for Ethereum
BrownieSimpleStorageProject is the active project.
Launching 'ganache-cli.cmd --port 8545 --gasLimit 12000000 --accounts 10 --hardfork istanbul --mnemonic brownie'...
File "C:\Users\chret\AppData\Local\Programs\Python\Python39\lib\site-packages\brownie\_cli\__main__.py", line 64, in main
importlib.import_module(f"brownie._cli.{cmd}").main()
File "C:\Users\chret\AppData\Local\Programs\Python\Python39\lib\site-packages\brownie\_cli\run.py", line 44, in main
network.connect(CONFIG.argv["network"])
File "C:\Users\chret\AppData\Local\Programs\Python\Python39\lib\site-packages\brownie\network\main.py", line 50, in connect
rpc.launch(active["cmd"], **active["cmd_settings"])
File "C:\Users\chret\AppData\Local\Programs\Python\Python39\lib\site-packages\brownie\network\rpc\__init__.py", line 75, in launch
self.process = self.backend.launch(cmd, **kwargs)
File "C:\Users\chret\AppData\Local\Programs\Python\Python39\lib\site-packages\brownie\network\rpc\ganache.py", line 73, in launch
return psutil.Popen(cmd_list, stdin=DEVNULL, stdout=out, stderr=out)
File "C:\Users\chret\AppData\Local\Programs\Python\Python39\lib\site-packages\psutil\__init__.py", line 1312, in __init__
self.__subproc = subprocess.Popen(*args, **kwargs)
File "C:\Users\chret\AppData\Local\Programs\Python\Python39\lib\subprocess.py", line 951, in __init__
self._execute_child(args, executable, preexec_fn, close_fds,
File "C:\Users\chret\AppData\Local\Programs\Python\Python39\lib\subprocess.py", line 1420, in _execute_child
hp, ht, pid, tid = _winapi.CreateProcess(executable, args,
FileNotFoundError: [WinError 2] The system cannot find the file specified
PS C:\Users\chret\Documents\demo\brownie_simple_storage>
Image of the directory:
Any help would be greatly appreciated :)
After trying all suggestions, I ended up making it work only by uninstalling and removing every single program and related files.
Uninstalled Python, removed all the files related to Python from my computer, did the same for VSCode, removed all the npm packages, yarn, solc, everything related to the tutorial (I didn't have anything for coding before).
Then, reproduced all the steps until the original post issue, and everything worked fine... A bit frustrating not to know how the issue really got resolved, but it did.
Your deploy script is in the contracts folder/directory.
It needs to be in scripts directory of your project, your brownie_simple_storage/scripts folder.
Try moving it there, and running your script in that folder.
EDIT:
You may need to install nodejs and ganache-cli
I think since brownie is installed via pipx, you have to tell vs code for the path.
ctrl + shift + p
Python: select Interpreter
Find the path in os. this is in linux
~/.local/pipx/venvs/eth-brownie/bin/python
I am using Linux, so find the path in your system and add it there.

Subprocess access denied error when running nmap as System User

I'm trying to run a python script when the system boots on a Windows 10 workstation. My python script runs fine when I kick it off in a different directory as a user with administrative privileges but when I run it from a bat file in the C:\Windows\System32\GroupPolicy\Machine\Scripts\Startup folder on system boot I receive the following error in my logs.
2018-11-13 04:43:58,913 - VP - Level 55 - nmap args: ['"C:\\Program Files (x86)\\Nmap\\nmap.exe"', '-oX', 'C:\\\\nmap\\NmapResults\\scan-192.168.1.5-20181113044358.xml', '-sS', '-n', '-p-', '--allports', '--scan-delay', '1ms', '-sU', '-sV', '--version-all', '192.168.1.5']
Traceback (most recent call last):
File "secnmap\nmap_scanner.py", line 596, in <module>
exit_code = main()
File "secnmap\nmap_scanner.py", line 248, in main
run_nmap(args, scan_options, host_data_list, use_service_name_check)
File "secnmap\nmap_scanner.py", line 268, in run_nmap
nmap_proc = subprocess.Popen(nmap_cmd_list, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
File "C:\Python27\lib\subprocess.py", line 390, in __init__
errread, errwrite)
File "C:\Python27\lib\subprocess.py", line 640, in _execute_child
startupinfo)
WindowsError: [Error 5] Access is denied
Here is the content of the .bat file.
cd C:\\nmap_startup
python nmap_startup.py
I have tried using the full nmap.exe path as shown in the code snippet as well as plain old nmap. I get the access denied error either way. I didn't see much in subprocess.py that would lead me to believe this to be an error with the python code either so I am leaning on it being an issue with System user running the script. If someone thinks otherwise or has any ideas, I'd love to hear them. Thanks
EDIT: I chose to run from a bat file because my python version is 32bit and I was having issues running from the C:\windows\system32 directory
It turns out that the path to nmap, C:\Program Files (x86)\Nmap, was not included in the system environment variables. When the script ran as a System user, it did not recognize the location of nmap

How to run a bash file with no extension in Python

I was wondering how I could use the script from this page in Python:
http://www.fmwconcepts.com/imagemagick/textcleaner/index.php
When downloading it, there is no extension. I tried renaming it to "textcleaner.sh" and run the following:
import subprocess
subprocess.check_call(['textcleaner.sh', '-g', '-e', 'normalize', 'input_image.jpg', 'output_image.jpg'])
But I get this error when doing so:
Traceback (most recent call last):
File "C:/Users/ikdem/PycharmProjects/McAffeeeeeee/bash test.py", line 11, in <module>
subprocess.check_call(['textcleaner.sh', '-g', '-e', 'normalize', 'opl-small-1.jpg', 'output.jpg'])
File "C:\Users\ikdem\AppData\Local\Programs\Python\Python35\lib\subprocess.py", line 579, in check_call
retcode = call(*popenargs, **kwargs)
File "C:\Users\ikdem\AppData\Local\Programs\Python\Python35\lib\subprocess.py", line 560, in call
with Popen(*popenargs, **kwargs) as p:
File "C:\Users\ikdem\AppData\Local\Programs\Python\Python35\lib\subprocess.py", line 950, in __init__
restore_signals, start_new_session)
File "C:\Users\ikdem\AppData\Local\Programs\Python\Python35\lib\subprocess.py", line 1220, in _execute_child
startupinfo)
OSError: [WinError 193] %1 is not a valid Win32 application
On a windows system, the default shell isn't bash. Renaming to .sh won't help, and windows tries to execv your file, which isn't a Windows executable, which explains the (bad) error message from Windows.
The best thing you could do would be to:
download & install MSYS2 ex in C:\MSYS2
change your line as follows:
code:
subprocess.check_call([r'C:\MSYS2\bin\sh','-c','textcleaner.sh', '-g', '-e', 'normalize', 'input_image.jpg', 'output_image.jpg'])
Windows does not have support for parsing a Unix-style shebang within its binary loader, and will require you to invoke a copy of sh.exe or bash.exe instead, passing it the path to the script followed by arguments to the script. You can typically find these executables as part of a MinGW or MSYS installation.
You can run my script on Windows, only if you use a Unix terminal, such as installing Cygwin or using Windows 10 built in Unix and install ImageMagick.
If you run Python from a unix terminal on Windows, it can be run. See http://www.imagemagick.org/discourse-server/viewtopic.php?f=4&t=32920, where I run a simple Imagemagick command.
You can run it natively on Windows using a version of that script converted into Magick.Net. See https://github.com/dlemstra/FredsImageMagickScripts.NET
Usage of my script in either form for commercial purposes will require obtaining a license. Usage otherwise is free.

IOError: [Errno 0] Error while writing to output of Powershell in VSCode

This is the error I get in VSCode when running docker-compose.
PS D:\Work\Imho\Api> docker-compose -f docker-compose.yml up
Creating network "api_default" with the default driver
Pulling ignite (apacheignite/ignite:latest)...
latest: Pulling from apacheignite/ignite
5040bd298390: Pulling fs layer
fce5728aad85: Pulling fs layer
Traceback (most recent call last):
File "docker-compose", line 3, in <module>
File "compose\cli\main.py", line 68, in main
File "compose\cli\main.py", line 118, in perform_command
File "compose\cli\main.py", line 926, in up
File "compose\project.py", line 401, in up
File "compose\service.py", line 305, in ensure_image_exists
File "compose\service.py", line 1001, in pull
File "compose\progress_stream.py", line 37, in stream_output
File "codecs.py", line 370, in write
File "site-packages\colorama\ansitowin32.py", line 40, in write
File "site-packages\colorama\ansitowin32.py", line 141, in write
File "site-packages\colorama\ansitowin32.py", line 169, in write_and_convert
File "site-packages\colorama\ansitowin32.py", line 174, in write_plain_text
IOError: [Errno 0] Error
Failed to execute script docker-compose
docker-compose.yml:
version: '3'
services:
ignite:
image: apacheignite/ignite
This error only happens on one of my 3 machines, so I have no idea what causes it. Resetting to factory defaults or reinstalling docker and vscode doesn't help. Without docker compose the image is pulling and then working fine. All the machines are on windows 10 and latest stable docker installed.
Here is the issue I opened on github. It appeared that this behavior only occurs in VSCode powershell console, but not in regular powershell console. The solution with changing encoding suggested there didn't help.
Solution: Update to 1803 on Windows 10. If you can't update, use external command prompt.
The same thing happens to me with Python scripts. Apparently, this is a Windows Update issue:
https://github.com/Microsoft/vscode/issues/36630#issuecomment-359969098
Edit: My workaround due to my company restricting updates is to use external terminal in VS Code

Running Linux Commands from Python on Windows Computer

I am trying to learn how to run command line commands from Python. I am able to do this with DOS:
import subprocess
subprocess.call("dir",shell=True)
This is fine, but I need to be able to do this for linux commands because my company uses linux servers. I am using Mobaxterm to run a local linux session. When I try this:
import subprocess
subprocess.call("ls",shell=True)
I get this error from the terminal:
'ls' is not recognized as an internal or external command, operable program or batch file.
Which sounds ridiculous to me, because ls is clearly a linux command.
If I don't include shell=True I get this error:
Traceback (most recent call last):
File "ConsolePractice.py", line 4, in <module>
subprocess.call("ls")
File "C:\Users\my_username\AppData\Local\Programs\Python\Python35-32\lib\subprocess.py", line 560, in call
with Popen(*popenargs, **kwargs) as p:
File "C:\Users\my_username\AppData\Local\Programs\Python\Python35-32\lib\subprocess.py", line 950, in __init__
restore_signals, start_new_session)
File "C:\Users\my_username\AppData\Local\Programs\Python\Python35-32\lib\subprocess.py", line 1220, in _execute_child
startupinfo)
FileNotFoundError: [WinError 2] The system cannot find the file specified
So what am I doing wrong? How can I get this to work?
EDIT: Thanks for the comments. Python for Windows won't work for what I'm trying to do. I connected to a linux server and did:
cat > ConsoleCmd.py
import subprocess
subprocess.call("ls",shell=True)
^C
python ConsoleCmd.py
... and ls ran. Found the issue. I'll just have to get my scripts on to the server and run them from there. Thanks everyone
I was able to resolve this issue by opening an SFTP Session within Mobaxterm, transferring the python file to the server, then running the file directly on the server (python was already installed on the server).

Categories