# Establishing SSH Connection from Ignition and executing commands & Python

**URL:** <https://forum.inductiveautomation.com/t/establishing-ssh-connection-from-ignition-and-executing-commands-python/60397>\
**Category:** Ignition\
**Created:** [June 6, 2022, 1:03pm UTC](https://forum.inductiveautomation.com/t/establishing-ssh-connection-from-ignition-and-executing-commands-python/60397 "2022-06-06T13:03:28Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Sri\_Haran](https://avatars.discourse-cdn.com/v4/letter/s/c77e96/32.png) [@Sri\_Haran](https://forum.inductiveautomation.com/u/Sri_Haran)\
**Post date:** [June 6, 2022, 1:03pm UTC](https://forum.inductiveautomation.com/t/establishing-ssh-connection-from-ignition-and-executing-commands-python/60397/1 "2022-06-06T13:03:28Z")

</div>

Hey All,  
This is the Exact task i need to do with, Though i have tried this with python using the library paramiko,  
i was unable to find the way for doing can anyone guide me.

> [@Python/Java Library for SSH from Ignition to SSH server computer to run a script](https://forum.inductiveautomation.com/t/python-java-library-for-ssh-from-ignition-to-ssh-server-computer-to-run-a-script/49420):
>
> Working on the an application where when I press a button on Ignition, I need the the action to establish a SSH connection to the remote server and run a python script and also get a return value. ParaMiko is a python library and has a lot of Dependencies which I am assuming wont work in Jython [I haven’t tried it] Wanted to get some guidance on what would be the best option to execute this application ? Are there any other libraries in python I could use since I am not a java programmer ?

Thanks,  
Sri

---

<div class="post-metadata">

**Author:** ![pturmel](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pturmel/32/3295_2.png) [@pturmel](https://forum.inductiveautomation.com/u/pturmel)\
**Post date:** [June 6, 2022, 1:30pm UTC](https://forum.inductiveautomation.com/t/establishing-ssh-connection-from-ignition-and-executing-commands-python/60397/2 "2022-06-06T13:30:03Z")

</div>

To run python externally, you need no libraries locally. You need them wherever python will run.

In Ignition, I would use java’s normal classes (starting with [ProcessBuilder](https://docs.oracle.com/javase/8/docs/api/java/lang/ProcessBuilder.html)) to spawn your platform’s `ssh` executable with suitable parameters (to connect to the remote system and launch your script), with a stream attached to the process’ `stdout`. Parse/decode that stream to get your result.

Use SSH keys to avoid any prompts while connecting. I recommend arranging for the selected key to only allow running the target script. (A limitation applied to the corresponding line of `authorized_keys` in the remote system.)

---

<div class="post-metadata">

**Author:** ![Sri\_Haran](https://avatars.discourse-cdn.com/v4/letter/s/c77e96/32.png) [@Sri\_Haran](https://forum.inductiveautomation.com/u/Sri_Haran)\
**Post date:** [June 6, 2022, 2:05pm UTC](https://forum.inductiveautomation.com/t/establishing-ssh-connection-from-ignition-and-executing-commands-python/60397/3 "2022-06-06T14:05:31Z")

</div>

hi @pturmel can you give me any links for example.  
Thanks  
Sri

---

<div class="post-metadata">

**Author:** ![pturmel](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pturmel/32/3295_2.png) [@pturmel](https://forum.inductiveautomation.com/u/pturmel)\
**Post date:** [June 6, 2022, 5:43pm UTC](https://forum.inductiveautomation.com/t/establishing-ssh-connection-from-ignition-and-executing-commands-python/60397/4 "2022-06-06T17:43:45Z")

</div>

I have no sample I can share, sorry. Look at the man-page for `ssh` and StackOverflow for `ProcessBuilder`.

---

<div class="post-metadata">

**Author:** ![paul-griffith](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/paul-griffith/32/37249_2.png) [@paul-griffith](https://forum.inductiveautomation.com/u/paul-griffith)\
**Post date:** [June 6, 2022, 6:02pm UTC](https://forum.inductiveautomation.com/t/establishing-ssh-connection-from-ignition-and-executing-commands-python/60397/5 "2022-06-06T18:02:55Z")

</div>

ProcessBuilder sample:

> [@Procedure for installing python libraries from source](https://forum.inductiveautomation.com/t/procedure-for-installing-python-libraries-from-source/26022/16):
>
> Oh, right - system.util.execute is a 'fire-and-forget', it doesn't give you access to standard output. Try adapting this: from java.lang import ProcessBuilder, String pb = ProcessBuilder([r"C:\Users\pgriffith\AppData\Local\Microsoft\WindowsApps\python3.exe", r"C:\Users\pgriffith\Downloads\test.py"]) process = pb.start() output = String(process.getInputStream().readAllBytes()) print output (Note: This blocks until the process completes waiting on the input stream. It may or may not ever compl…

---

<div class="post-metadata">

**Author:** ![Sri\_Haran](https://avatars.discourse-cdn.com/v4/letter/s/c77e96/32.png) [@Sri\_Haran](https://forum.inductiveautomation.com/u/Sri_Haran)\
**Post date:** [June 7, 2022, 7:23am UTC](https://forum.inductiveautomation.com/t/establishing-ssh-connection-from-ignition-and-executing-commands-python/60397/6 "2022-06-07T07:23:29Z")

</div>

Thanks @paul-griffith,  
That worked Fine for me  
Regards,  
Sri

---

<div class="post-metadata">

**Author:** ![codersmurf](https://avatars.discourse-cdn.com/v4/letter/c/df788c/32.png) [@codersmurf](https://forum.inductiveautomation.com/u/codersmurf)\
**Post date:** [January 22, 2024, 11:07pm UTC](https://forum.inductiveautomation.com/t/establishing-ssh-connection-from-ignition-and-executing-commands-python/60397/7 "2024-01-22T23:07:50Z")

</div>

@pturmel

I tried running this:

```auto
from java.lang import ProcessBuilder, String
pb = ProcessBuilder([r"/usr/bin/pwd"])
process = pb.start()
output = String(process.getInputStream().readAllBytes())
print output

```

get this:

java.io.IOException: java.io.IOException: Cannot run program "/usr/bin/pwd": CreateProcess error=2, The system cannot find the file specified

Tried system.util.execute, same error. Actually I've been able to run whoami without an error using execute. So I'm not sure how to import the java into the python script in ignition.Thx,jake
