Python Forum
cant use ping, sudo or other commands in remote shell script.
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
cant use ping, sudo or other commands in remote shell script.
#1
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.')
Reply
#2
(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
Reply
#3
(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. :\
Reply
#4
when I run the server script on a separate PC. I cannot connect from the client end, all i get is a connection refused.
Reply
#5
What should I do?
Reply
#6
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.
Reply
#7
(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.
Reply
#8
(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.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Possible to execute a python script before log off/shutdown with input commands? Kaltex 1 219 May-18-2021, 06:31 AM
Last Post: Skaperen
  Ping command using python 3.6.5 Martin2998 6 11,632 Apr-19-2021, 06:24 PM
Last Post: blazejwiecha
  Raspi Commands via Python (VS Code) Script AS4188 5 564 Oct-21-2020, 03:49 PM
Last Post: AS4188
  GPIO high if network IP has good ping duckredbeard 3 498 Oct-12-2020, 10:41 PM
Last Post: bowlofred
  crontab on RHEL7 not calling python script wrapped in shell script benthomson 1 744 May-28-2020, 05:27 PM
Last Post: micseydel
  Create a program that PING a list of IPs skaailet 7 1,132 Mar-26-2020, 10:46 PM
Last Post: snippsat
  ping with output jacklee26 1 1,304 Nov-28-2019, 01:01 PM
Last Post: Axel_Erfurt
  How to use subprocess send commands to windows shell hlhp 3 1,641 Nov-26-2019, 04:40 AM
Last Post: LeanbridgeTech
  Python script dioesn`t run all commands exploit123 5 1,036 Sep-25-2019, 03:15 PM
Last Post: snippsat
  Linux Script Sudo and "&" s3fac5 14 2,064 Aug-30-2019, 08:02 AM
Last Post: s3fac5

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020