socket.error: port/proto not found - python

I'm trying to get the service name of port number 1521 using socket package.
This is the code I'm trying to execute in shell
import socket
portname = socket.getservbyport(1521)
print portname
The error I'm getting is as below
portname = socket.getservbyport(i)
socket.error: port/proto not found
If I'm using any other port such as 22,80,1433,3306 their isn't any problem in retrieving the port/service name.
I've tried similar code on two other Linux Machine but their isn't any response.

Related

Is there any way to test the weblogic admin connecting URL (t3/t3s) before connecting to it

I'm using following command to connect to weblgic using WLST,
java weblogic.wlst core.py
inside core.py I'm calling following command to connect to the weblogic admin. but some times the service url becomes unresponsive And my script hangs occasionally due to this. Is there any way to give a timeout to this connect() method or any other method to implement a timeout functionality?. Appreciate if someone can shed some light on this. Thanks.
connect(username,password,t3://:)
in earlier WebLogic versions they have provided following functionality(to ping), but they have removed it after 12.2*
java weblogic.Admin -url t3://localhost:7001 -username weblogic -password weblog
ic ping 3 100
This is a very common situation, where you can use Python's socket module to check that the Admin port is opened or not with the following function.
import socket
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
AdminIP = '192.168.33.10'
result = sock.connect_ex((AdminIP,7001))
if result == 0:
print "AdminPort is open you can connect"
else:
print "Admin Port is not yet open"
sock.close()
add your logic accordingly, HTH!

Problem accessing unknown SMB server via pysm while the server is up and running

I am trying to get all shared devices of unknown SMB servers.
While trying to connect via SMBConnection.connect() function it refuses to connect to the server and connection get lost or no connection at all. Meanwhile I get exceptions in Python I can connect to that server from my Desktop environment.
What could cause this. I have a list of SMB servers varying from Linux to Windows hosts.
from smb.SMBConnection import SMBConnection
import socket
ips ['x.x.x.x','x.x.x.x'...]
for ip in ips :
print(ip)
try:
hostname = socket.gethostbyaddr(ip)
except socket.herror as e :
print("[-][-][-]Uknknown host")
continue
smb_conn = SMBConnection(username='Guest',password="",my_name='name',remote_name=hostname)
try:
is_connected = smb_conn.connect(ip,timeout=60)
except :
print("[!] %s is up but refuses smb connection:(( "%ip)
#print("Na not responding shit")
continue
if is_connected:
print("[+] %s connected"%ip)
try:
shares = smb_conn.listShares()
for share in shares :
print(share.name)
print("\n\n\n")
except smb.smb_structs.OperationFailure as e:
print (e.message)
break
UPDATE:
While executing this script I get TimeoutError while the server is up and running and accessible through Desktop SMB connection.
SOLVED:
In some cases the SMB server is listening or port 445 and not the NetBIOS port 139. By default the connect() function tries to connect to port 139 but it can be modified by connect(ip,port,timeout) parameters.

Host command and ifconfig giving different ips

I am using server(server_name.corp.com) inside a corporate company. On the server i am running a flask server to listen on 0.0.0.0:5000.
servers are not exposed to outside world but accessible via vpns.
Now when i run host server_name.corp.com in the box i get some ip1(10.*.*.*)
When i run ifconfig in the box it gives me ip2(10.*.*.*).
Also if i run ping server_name.corp.com in same box i get ip2.
Also i can ssh into server with ip1 not ip2
I am able to access the flask server at ip1:5000 but not on ip2:5000.
I am not into networking so fully confused on why there are 2 different ips and why i can access ip1:5000 from browser not ip2:5000.
Also what is equivalent of host command in python ( how to get ip1 from python. I am using socktet.gethostbyname(server_name.corp.com) which gives me ip2)
As far as I can tell, you have some kind of routing configured that allows external connections to the server by hostname (or ip1), but it does not allow connection by ip2. And there is nothing unusual in this. Probably, the system administrator can advise why it is done just like this. Assuming that there are no assynchronous network routes, the following function can help to determine public ip of server:
import socket
def get_ip():
try:
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
sock.connect(("8.8.8.8", 80))
local_address = sock.getsockname()
sock.close()
local_address = local_address[0]
except OSError:
local_address = socket.gethostbyname(socket.gethostname())
return local_address
Not quite clear about the network status by your statements, I can only tell that if you want to get ip1 by python, you could use standard lib subprocess, which usually be used to execute os command. (See subprocess.Popen)

What could be the reason for a socket error "[Errno 9] Bad file descriptor"

I have a complex python (2.7.3) script which is trying to open a socket connection via
self.socket.close()
# doing something else
self.socket.connect((host, port))
but all I get is the following socket error:
error: [Errno 9] Bad file descriptor
The host:port accepts connections as I have verified this with nc host port manually. So what could be the possible reasons I get this error for opening a connection to the given port, which actually works?
I cannot and will not post the full script as it is too complex and irrelavent for this question. I just would like to know all possible reasons for this error, and how to check them and fix them.
You will need to create a new socket object. Maybe self.socket = socket.socket() after closing the previous socket and before connecting.

Grab banner of different ports with python

I'm working on a script that grabs the banner from common ports of a host. I'm using sockets to make the connection but I'm facing some issues. Here is the code:
try:
connsocket = socket(AF_INET, SOCK_STREAM)
connsocket.settimeout( 5 )
connsocket.connect((ip, port))
connsocket.send("HEAD / HTTP/1.0")
results = connsocket.recv(400)
connsocket.close()
return str(results)
except:
print '[ERROR]Failed to connect or Connection timed out'
The are two major issues:
First time I run the script to a host all the banners are retrieved correctly except port 80 which exits with the timeout
The second problem is that when I relaunch the script to the same host there is no response from any port.
I suspect that the second issue is due to the connection is still open and the script fails retying to connect. With the first issue I have no idea why it's not working.
Any idea?
Regards.

Categories