我正在制作自己的网络工具,并且正在实现一个 ssh 选项来 ssh 到服务器,但是当我使用联网的 ssh 服务器对其进行测试时,我可以输入并 ssh 进入其中,但是当我运行命令时,例如:ls、mkdir等。我只是得到一个空括号作为输出。输出如下所示:“[]”。从字面上看,这就是我得到的输出,而不是实际执行我想要的命令。我怎样才能解决这个问题???
pyNet 代码:
import socket
import time
import urllib
import pyfiglet
import paramiko
import ipaddress
import subprocess
import sys
import os
import scapy.all as scapy
from datetime import datetime
# banner
print("*" * 100)
ascii_banner = pyfiglet.figlet_format("ALL IN ONE")
print(ascii_banner)
print("*" * 100)
# ping host
def ping(host):
res = os.system("ping -c 10 " + host)
if res == 0:
print("")
print("Connection is alive....")
else:
print("Connection is down....")
# scan ports
def port_scanner():
print("")
host = input("Enter website to scan: ")
ip = socket.gethostbyname(host)
print("*" * 60)
print("Please wait, scanning host", ip)
print("*" * 60)
t = datetime.now()
try:
for port in range(1, 65535):
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
result = sock.connect_ex((ip, port))
if result == 0:
print("Port {}: Open".format(port))
sock.close()
except KeyboardInterrupt:
print("You pressed Ctrl+C")
sys.exit()
except socket.gaierror:
print("Hostname could not be resolved.... Exiting")
sys.exit()
except socket.error:
print("Couldn't connect to server")
sys.exit()
t2 = datetime.now()
total = t2 - t
print("Scan Completed in: ", total)
# ssh session
def ssh_option():
ip = input("Enter url: ")
t_ipaddr = socket.gethostbyname(ip)
print("Making connection to:", t_ipaddr)
user = input("Enter your username: ")
passwd = input("Enter you password: ")
ssh = paramiko.SSHClient()
ssh.load_system_host_keys()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect(t_ipaddr, username=user, password=passwd, look_for_keys=False)
command = input("Enter command: ")
ssh_stdin, ssh_stdout, ssh_stderr = ssh.exec_command(command)
output = ssh_stdout.readlines()
print(ssh_stdin)
ssh.close()
# menu
run = True
while run:
print("Enter an option (1 - 3)")
print("")
print("1. Ping")
print("2. Scan Ports")
print("3. SSH")
choice = int(input("cmd: "))
if choice == 1:
url = input("enter website url: ")
print(ping(url))
if choice == 2:
port_scanner()
if choice == 3:
ssh_option()