How to run telnet over ssh in python - python

telnet
server ←————→ device
↑
| SSH
↓
localhost (me)
I have a device that is connected with one server computer and I want to talk to the device by ssh'ing into the server computer and send telnet commands to my device. How do I setup things in Python to make this happen?

You can use Python's paramiko package to launch a program on the server via ssh. That program would then in turn receive commands (perhaps via stdin) and return results (via stdout) from the controlling program. So basically you'll use paramiko.SSHClient to connect to the server and run a second Python program which itself uses e.g. telnetlib to talk to the device.

Use tunneling by setting up a SSH session that tunnels the Telnet traffic:
So ssh from localhost to server with the option -L xxx:deviceip:23
(xxx is a free port on localhost, device is the IP address of "device", 23 is the telnet port).
Then open a telnet session on your localhost to localhost:xxx. SSH will tunnel it to the device and response is sent back to you.

Related

How to use WHOIS queries from python script when port 43 is likely not accessible

Im very new to network programming and faced a following problem:
Im working on VMware CentOS7 virtual machine on Windows10 host.
My script should send WHOIS queries and parse their output (e.g. expiration date).
However, an attempt to send a query leads to a connection error:
>>>import whois
>>>whois.query('google.com')
WhoisCommandFailed: connect: Network is unreachable
I tried to whois from terminal, but error was the same.
When i tried to use whois directly from Windows, which hosts virtual machine, the error seemed to look same as well (connection timeout).
As i found out, it was most likely related to access to port 43. I created rules (for in and out) for Windows firewall for this port by a guide , but error still persisted.
It looks like access to this port was blocked by ISP (however ping command is working).
To sum up, I got two questions there:
1) (less important) How to check if port 43 is blocked by firewall either by ISP?
2) (most important) Is it possible somehow to reconfigure WHOIS for usage of another port (i.e. 23) for sending queries by Python script?
Unfortunately, ISP security policy doesn't allow them to open 43 port.
Mostly ISP doesn't block any port but yes, this is not 100% true.
Testing connection:
run tcpdump (install command: yum install tcpdump) command on CentOS: tcpdump -peni any tcp and port 43
You have to see lines with the following text: 192.168.1.1.57350 > 192.34.234.30.43 where 192.34.234.30 IP address means the remote whois server.
Try to telnet to remote server's TCP/43 port: telnet 192.34.234.30 43
You should see the following:
Trying 192.34.234.30...
Connected to 192.34.234.30.
Escape character is '^]'.
If you can`t see context like that and you get back prompt immediately you will a firewall rule somewhere what is block connection. I recommend to switch off firewall temporarily and test again.
You cannot change port number, because it is configured on the remote side, on the server.
Can CentOS7 server communicate towards the internet? In example can you install packages?
Is there any router between windows machine and ISP?

Run python server on VPS

I create a simple python server:
import socket
server=socket.socket()
server.bind(("0.0.0.0",8820))
server.listen(1)
(client_socket,client_address)=server.accept()
client_name=client_socket.recv(1024)
client_socket.send("Hello "+client_name)
client_socket.close()
server.close()
when I run this script in a VPS I cannot connect to this server
why?
(I get the IP address of the server( with ifconfig command on bash console) and when I run a client script that connect to this address it doesn't connect)
Try to bind the socket server to the vps public IP address and try to avoid ports bellow 10,000 I think most of firewalls prevent incoming connections through ports under 10,000.

Connecting to Openshift Python socket server via Adobe Air client

Currently i have work on Python socket server on Openshift. Managed to get it listen to port (15000) and tested on local with telnet seems working fine.
However, i unable to connect to the socket server other than local ( either telnet to the socket server or using Adobe Air xmlsocket )
when i do a netstat on the server, i got following result:
netstat -tan | grep $OPENSHIFT_PYTHON_IP | grep $OPENSHIFT_PYTHON_PORT | grep ESTABLISHED
/proc/net/tcp: Permission denied
not sure whether i missed anything on the configuration on Openshift.
Btw, how can i get the IP of my server? if i use OPENSHIFT_PYTHON_IP the ip is only for local right?
Your websocket server needs to listen on port 8080, and you need to access it at ws://app-domain.rhcloud.com:8000

How can I do SSH port forwarding from within Python Twisted?

Are there any examples of initiating an SSH session to a remote machine with port forwarding options from within Twisted using Conch such that one can pipe normal TCP traffic through the tunnel?
Scenario:
I have a server running a custom Twisted-based Protobuf RPC service and a machine with a Twisted-based RPC client installed. The server is also running SSH. Rather than talking to the RPC service on the server directly, I would like to connect to the server using SSH from the RPC client, setup port forwarding on the server, and communicate with the RPC service using Protobuf through the SSH tunnel.
I'm already able to setup port forwarding manually and have the RPC client talk to the RPC service by pointing the RPC client to a local port on the client box, I'm just curious as to how I can do this within the client directly.
It would be awesome if there were improved documentation in Twisted for doing neat things with Conch (after all, how many other programmable SSH libraries are there?). Until that happy day comes, reading the implementation of the conch command line tool can be a big help.
Here we can see where port forwarding options from the command line are turned into some action over the SSH connection:
https://github.com/twisted/twisted/blob/4ffbe9f6851dbe7e9172f55905f264ecf50da3a6/src/twisted/conch/scripts/conch.py#L226-L238
I think you're asking about a local forwarding rule, so the localForwards loop is doing roughly what you want to do.
Implementing a tunneling Twisted SSH client that does local port forwarding can be surprisingly simple.
Just create a basic Twisted Conch SSH client, and implement the port forwarding part in the serviceStarted method of the SSH connection class of your client:
from twisted.conch.ssh import forwarding
LOCALPORT = 8888
REMOTEHOST = "127.0.0.1"
REMOTEPORT = 9999
class Connection(connection.SSHConnection):
def serviceStarted(self):
Channel = forwarding.SSHListenClientForwardingChannel
Factory = forwarding.SSHListenForwardingFactory
factory = Factory(self, (REMOTEHOST, REMOTEPORT), Channel)
s = reactor.listenTCP(LOCALPORT, factory)
That's all there's to it (REMOTEHOST is set to point to ssh server itself since that's what you said you're connecting to).

TCP-IP Communication via Python and an open port of a SSH connection

This is a newbie question for TCP-based communication in Python. I am trying to establish a TCP-based communication between two *NIX systems via an SSH tunnel and the Python socket module. I have used the two first examples "echo server" and "echo client" from this Python MOTW website: http://www.doughellmann.com/PyMOTW/socket/tcp.html.
The communication worked fine on the same *NIX system (HOST1) but failed over the ssh tunnel.
I logged into the second *NIX system with ssh -L 10000:HOST2:10000 USERNAME#HOST2. Then I played around and tried to establish the communication in the same manner by starting the python script for the server on HOST2 and the script for the client on HOST1. This I got on stderr:
python test_socket_client.py
connecting to localhost port 10000
Traceback (most recent call last):
File "test_socket_client.py", line 10, in <module>
sock.connect(server_address)
File "<string>", line 1, in connect
socket.error: [Errno 111] Connection refused
When I started server and client vice versa on HOST1 and HOST2, respectively, I got the same message.
What am I doing wrong?
Your server binds its socket to localhost:10000 -- which means it listens to connections from the loopback interface only. The SSH command line instructs SSH to tunnel local connections to port 10000 to connections to HOST2:10000. Your client Python program connects to localhost:10000, and the remote end of the SSH link then tries to connect to HOST2:10000. Even though that's the same port and the same machine as your socket is bound to, it's a different interface, to which your socket is not listening.
Change the SSH command line to ssh -L 10000:localhost:10000 USERNAME#HOST2, or bind your server's listening socket to all interfaces: server_address = ('', 10000). The empty string serves as INADDR_ANY.
Of course, what Gary van der Merwe said is true: SSH would need to be running at the time you run your client Python program on HOST1.
I would check that your server is listening on the server host, and that the ssh client is listening on the client host. You can run netstat -tnlp to check this.
Are you sure that your ssh client is still open when you are running your client. You either need to run shh in one terminal, and your client in another, or run ssh -Nf which causes ssh to go to the background after it has authenticated.

Categories

Resources