Python Forum
cant use ping, sudo or other commands in remote shell script. - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: cant use ping, sudo or other commands in remote shell script. (/thread-33663.html)



cant use ping, sudo or other commands in remote shell script. - throwaway34 - May-15-2021

Ive posted about this script in the past and again I have run aground... whenever I try to enter commands In such as sudo, Clear, ping and so on I do not receive any feedback from the server end please help. Wall

client:
#!/usr/bin/env python3
import socket
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
if True:
	try:
		Host = input('>Remote Host:')
		port = 4444
		s.connect((Host, port))
	except TimeoutError:
		print('Connection Timed out.')
		s.close()
		exit()
	except ConnectionRefusedError:
		print('connection refused.')
		s.close()
		exit()
	except socket.gaierror:
		print('connection refused.')
		s.close()
		exit()
while True:
    cmd = input('>(R)Host:$')
    cmd2 = (cmd).encode('utf-8')
    try:
    	s.sendall(cmd2)
    	data = s.recv(4096)
    	data2 = data.decode('utf-8')
    	print(data2)
    except ConnectionResetError:
    		print('connection was reset.')
    except BrokenPipeError:
    	print('The connection has been closed by the Host.')
server:
#!/usr/bin/env python3

import socket
import platform
import subprocess
s  = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
Host = platform.node()
port = 4444
if  True:
	try:
		s.bind((Host, port))
		s.listen(1)
	except OSError:
		print('The script is already running.')
		s.close()
		exit()
conn, addr = s.accept()
while True:
    data = conn.recv(4096)
    data2 = data.decode('utf-8')
    print(data2)
    z = subprocess.run(data2, shell=True, stdout=subprocess.PIPE,	stderr=subprocess.PIPE)
    z2 = z.stdout + z.stderr
    try:
    	conn.sendall(z2)
    except BrokenPipeError:
    	print('the connection was closed.')



RE: cant use ping, sudo or other commands in remote shell script. - ibreeden - May-15-2021

(May-15-2021, 06:03 AM)throwaway34 Wrote: In such as sudo, Clear, ping and so on I do not receive any feedback from the server end

I'm not sure, but:
  • sudo usually asks for the password on the terminal. But in this client-server construct there is no terminal.
  • clear clears the terminal screen. But what if there is no terminal?
  • ping does not end. It continues forever unless the -c option is used



RE: cant use ping, sudo or other commands in remote shell script. - throwaway34 - May-15-2021

(May-15-2021, 08:28 AM)ibreeden Wrote:
(May-15-2021, 06:03 AM)throwaway34 Wrote: In such as sudo, Clear, ping and so on I do not receive any feedback from the server end

I'm not sure, but:
  • sudo usually asks for the password on the terminal. But in this client-server construct there is no terminal.
  • clear clears the terminal screen. But what if there is no terminal?
  • ping does not end. It continues forever unless the -c option is used

You dont get any output either from ping or sudo. :\


RE: cant use ping, sudo or other commands in remote shell script. - throwaway34 - May-16-2021

when I run the server script on a separate PC. I cannot connect from the client end, all i get is a connection refused.


RE: cant use ping, sudo or other commands in remote shell script. - throwaway34 - May-16-2021

What should I do?


RE: cant use ping, sudo or other commands in remote shell script. - ibreeden - May-17-2021

You say it does not work on a separate PC. So before it worked? Did it work when the server was a server? In that case it is most probably a firewall issue.


RE: cant use ping, sudo or other commands in remote shell script. - throwaway34 - May-17-2021

(May-17-2021, 07:21 AM)ibreeden Wrote: You say it does not work on a separate PC. So before it worked? Did it work when the server was a server? In that case it is most probably a firewall issue.

It never worked before on a separate PC, that was the first attempt at running the two scripts on separate machines and maybe your right about the firewall part but, either way I still cannot get any output from certain commands/programs on the client side.


RE: cant use ping, sudo or other commands in remote shell script. - throwaway34 - May-17-2021

(May-17-2021, 07:21 AM)ibreeden Wrote: You say it does not work on a separate PC. So before it worked? Did it work when the server was a server? In that case it is most probably a firewall issue.

When I do a Nmap scan to see if it(the server script) opens port 4444, no such port appears to be open.