72909

ssh not recognized as a command when executed from python using subprocess?

<h3>Question</h3>

This is my code -

import subprocess import sys HOST="xyz3511.uhc.com" # Ports are handled in ~/.ssh/config since we use OpenSSH COMMAND="uptime" ssh = subprocess.Popen(["ssh", "%s" % HOST, COMMAND], shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE) result = ssh.stdout.readlines() if result == []: error = ssh.stderr.readlines() print (sys.stderr, "ERROR: %s" % error) else: print (result)

and this is the error I'm getting-

<blockquote>

ERROR: [b"'ssh' is not recognized as an internal or external command,\r\n", b'operable program or batch file.\r\n'].

</blockquote>

Not sure what I'm doing wrong over here. Also, I haven't mentioned any port. All I want is to use subprocess and connect to remote server, execute a simple command like ls. Python version is 3.x.


<h3>Answer1:</h3>

Apparently this happens in python3. Workaround found at this link: https://gist.github.com/bortzmeyer/1284249

system32 = os.path.join(os.environ['SystemRoot'], 'SysNative' if platform.architecture()[0] == '32bit' else 'System32') ssh_path = os.path.join(system32, 'OpenSSH/ssh.exe') out1, err1 = Popen([ssh_path, "pi@%s"%self.host, "%s"%cmd], shell=False, stdout=PIPE, stderr=PIPE).communicate()

来源:https://stackoverflow.com/questions/51156884/ssh-not-recognized-as-a-command-when-executed-from-python-using-subprocess

Recommend

  • AJAX 一
  • How to inspect animation of an HTML element?
  • Make google Recaptcha work with special characters in the domain name
  • Block webpages and display error page
  • Laravel error with 404 page
  • swift xml rss reader… make it async
  • How to use symmetricDS in Embedded mode
  • Spring Cloud: default redirecting from Gateway to UI
  • How to get the name of a file downloaded with Angular5
  • virtualenvwrapper seemingly ignoring VIRTUALENVWRAPPER_PYTHON
  • Microsoft speech API 5.1, 5.3?
  • Best way to send continuous data in Java using Netty
  • Is it possible to use pseudo-elements to make containing elements wrap around an absolutely-position
  • FMX: dropping selfmade component on form duplicates subcomponents
  • RSRuntimeException: Loading of ScriptC script failed (build tools 19.0.2)
  • flexbox sticky footer
  • Is there a function to calculate the difference between two times and display a relative result?
  • BREAK ON instruction in MySQL queries?
  • Trying to get lotusscript json reader
  • Spongycastle is missing many algorithms when loaded on android
  • yii rewrite url with many sub categories
  • How to close a WebView with double-click?
  • What are advantages/disadvantages of using Selenium for Java vs .NET applications?
  • Disable account chooser FirebaseUI React
  • SQL Server 2012 not showing unicode character in results
  • Stop an element moving with padding on hover
  • Java .policy file - how to prevent java.util.Date() from being accessible
  • xpath assertion failure with dynamic xpath
  • how to run a different select statement based on condition in Hive SQL
  • Runtime complexity of getting the length of a string in different representations
  • using maven pom while creating jar:test-jar some times it says JAR will be empty - no content was ma
  • JavaScript Regex to Match Boundaries of Words with diacritics
  • How do I add a mouse over tooltip to an Image using .DrawImage()
  • Google App Engine Datastore: Dealing with eventual consistency
  • How to get rgb from transparent pixel in js
  • ssh remote server login script
  • Computing the discrete fourier transform of audio data with FFTW
  • How to call different template for different category archive page in woocommerce
  • Firebase: How to read from external DB?
  • convert json to excel in java