Paramiko for remote ssh - python

I have a common VM and remote VM. we have network devices, only remote VM has access to network devices.
Python script will do configurations in network devices.
I must trigger a python script from common VM.
To implement this Currently I have two options in my mind.
Configure password less between common vm to remote vm. Trigger a script from common vm which copies script to remote vm then start execution.
directly ssh using paramiko to remote and again ssh using paramiko to network devices (This option is not tried, I am going to try now)
Which is the better method or any other suggestions will welcome.
Thanks

Related

how ansible connects to remote devices

I am learning how to create ansible modules in python, I want to connect to remote devices, I am looking at ansible source code to see how they connect, but I still cant understand how???
I took this simple module file from ansible to check how they connect to the remote device and do all the stuff that the module does
https://github.com/ansible/ansible/blob/devel/lib/ansible/modules/ping.py
I don't see any socket or libraries for ssh in this file, so how does it happed?
does ansible first connect to the remote host and then activate the module on the remote host?
is that why it requires python on remote devices?
I cant really understand it
As far as i know, Ansible use ssh to connect.
You can perform a research whith 'repo:ansible/ansible filename:ssh' in
github research here
You can examin this code.
Ansible also use the lib paramiko to connect with ssh.
as we can see here

Microsoft Azure: How to run a Python program on a virtual machine (VM)?

I need to run a Python program on a Microsoft Azure virtual machine (VM), because it uses a significant amount of memory (over 12 GB), so I need to run it in a virtual machine in the cloud, which would provide enough memory.
However, I didn't seem to find an option in the virtual machine's dashboard which lets me run a Python program on it?
You can RDP or SSH into your Azure virtual machine, then you can install the application or execute the scripts as on the traditional VM as you would do.
You'll use the Connect button in the Azure portal to start a Remote Desktop (RDP) session from a Windows desktop. Read https://learn.microsoft.com/en-us/azure/virtual-machines/windows/connect-logon
Alternatively, you will create and use an SSH RSA public-private key file pair for SSH client connections. Read https://learn.microsoft.com/en-us/azure/virtual-machines/linux/create-ssh-keys-detailed
Furthermore, you can use an Azure network security group to filter network traffic to and from Azure virtual machines in an Azure virtual network. Read security rules and how to open ports to a virtual machine with the Azure portal.
As mentioned in the comments, for VMs you will not find any option to install software from Azure Portal. This is something you would need to do by connecting to your Virtual Machine.
You can connect to your VMs using multiple ways: Remote Desktop Connection, SSH, and Bastion etc. You will see all of these options in Azure Portal in the overview blade for your VM. Please see this link as an example of how to connect to your Windows VM using RDP: https://learn.microsoft.com/en-us/azure/virtual-machines/windows/connect-logon.
Once you're connected to your VM, you can install software (python in your case). Installation mechanism would depend on the VM Operating System. Next you will need to copy your application code/binaries in the VM and run it there while still connected to your VM.

Pycharm IDE coding on remote server with ssh

I wonder if there is any way to coding python with remote server data and run the python script on remote server with pycharm only installed on my local machine?
The remote server can be access by two ip address (ssh and ftp each), id and password.
But when I try to use pycharm professional project interpreter, there always an error like this (the ip address I used is for ssh):
I know the easiest way is install jupyter notebbok on my remote server, but I don't have permission to do that.

Python retrieve and configure remote network interface using socket.socket

I am trying to configure network interfaces on remote machines using python. The surrounding context is that I have a service that provisions VM instances and is required to create appropriate network interfaces on the VM host for the instance to use. I am restricted regarding remote code execution.
My first idea was to use SSH and work with the remote /etc/network/interfaces (the remote machines are exclusively debian based).
Additionally, I stumbled upon pynetlinux and others using a python socket and fcntl to retrieve and configure local interfaces (interfaces.py using socket and fcntl). Obviously, the socket is only locally available but that got me thinking: Is it possible to use a python file-like object over SSH directly accessing remote network interfaces in the way socket.socket does?
I hope there is a way to achieve this, parsing and writing /etc/network/interfaces would work but the other solution is imho awesome.
Greetings and thanks in advance

How do I setup a python, telnet proxy for a device on a different network over SSH?

Overview:
I have a device sitting on a local network to a computer that is sitting on an outside network. I would like to create a software program that allows me to seamlessly connect to the device from a computer on a different network. For purposes of this question, I've created a picture to help describe the network flow. What I need help with is what python packages I would need to develop the solution for this problem.
Details:
I have a computer MYPC (IP address 192.168.0.168) that is attached to the internet running through a proxy server (ROUTER1). I have full control over MYPC's environment, which is running Linux.
I have a second computer SOMESERVER (IP address 192.168.1.168) that is attached to the internet running through a proxy server (ROUTER2). In addition, SOMESERVER (IP address 10.0.0.159) is also attached to a local network (LOCAL). SOMESERVER is running windows. I have very limited control with SOMESERVER: I am able to send an executable to SOMESERVER that can run once before it is removed. I do not know the internet/world IP address of the ROUTER2 initially.
I have a device (DEVICE1) attached to SOMESERVER through LOCAL (IP address: 10.0.0.157).
I have another device (DEVICE2) attached to SOMESERVER through LOCAL (IP address: 10.0.0.158). DEVICE(x) runs linux. I have python on DEVICE(x) and I could install a pure python package if I needed to. However, I do not have the ability to compile for DEVICE(x).
I can connect between SOMESERVER and MYPC through the internet using SSH over ROUTER1 and ROUTER2. I can connect between SOMESERVER and DEVICE1 through the local network (LOCAL) using Telnet. I can connect between SOMESERVER and DEVICE2 through the local network (LOCAL) using Telnet.
I want to send a program to SOMESERVER that allows me seamless access over SSH and Telnet to DEVICE1 and DEVICE2 from MYPC. In addition, I want that program to be running python.
Here's a picture that helps explain the above problem:
Solution:
What I think I want is as follows. I need help with the details and what packages I might need to make it happen.
Part 1: The Dial Home Client and Server
Create a "dial home" server program (DIAL_HOME_SERVER) for MYPC which listens for any one dialing home and then will "dial into" any SOMESERVER that "dials home" using SSH.
Create a "dial home" client program (DIAL_HOME_CLIENT) for SOMESERVER which is downloaded as part of a package
Part 2: The Proxy Server
Create a ssh to telnet proxy server program (PROXY_SERVER) for SOMESERVER which listens for connections from MYPC and funnels them into a telnet connection to DEVICE(x).
Thanks to Greg Hewgill, it sounds like I can use Paramiko to pull together the PROXY_SERVER code on SOMESERVER. It appears that Paramiko also requires PyCryto, and the Windows binary for it can be found here.
Future Robustness
At a future date, the telnet connection will be replaced with an SSH client (dropbox on DEVICE(x)).
In Closing
I think the above will allow MYPC to connect "seamlessly", but the details of how to put together these programs is unknown to me. I already know how to package up a python program using Innosetup and/or py2exe. What I'd love to see is links pointing to different pieces of the solution so I can pull it all together. And then I can post it.
Thanks in advance!
I have to admit that I didn't quite follow all of your description, especially the "dial home" client/server part. However, your diagram seems sufficient for understanding.
Set up port forwarding on "router2" that forwards the incoming SSH port to your Windows server.
Write a Python program (you will probably find paramiko helpful) that runs on your Windows server, listens for SSH connections, and opens a telnet connection to one of your devices on the back end.
That seems sufficient to me. If you've got weird restrictions on the Windows server about only being able to run an executable once before it is deleted, that seems like another problem to solve that doesn't really relate to this tunnelling problem.

Categories