9
9
9600795872020-07-02 00:18:34
Python
960079587, 2020-07-02 00:18:34

How to combine text into 1 message?

import shodan
import config
import requests
import traceback
import telebot
from telebot import TeleBot


SHODAN_API_KEY = "PSKINdQe1GyxGgecYz2191H2JoS9qvgD" 
api = shodan.Shodan(SHODAN_API_KEY)

ApiToken = config.API_TOKEN
bot = TeleBot(ApiToken)


def shodan(message):
    try:
        target = message.text

        dnsResolve = 'https://api.shodan.io/dns/resolve?hostnames=' + target + '&key=' + SHODAN_API_KEY

        try:
            # First we need to resolve our targets domain to an IP
            resolved = requests.get(dnsResolve)
            hostIP = resolved.json()[target]
            host = api.host(hostIP)
        
            bot.send_message(message.chat.id,f"IP: %s" % host['ip_str'] + "\n" +"Organization: %s" % host.get('org', 'n/a') + "\n" + "Operating System: %s" % host.get('os', 'n/a'))
            
            for item in host['data']:
                bot.send_message(message.chat.id,"Порты: %s" % item['port'])

         
            for item in host['vulns']:
                CVE = item.replace('!','')
                bot.send_message(message.chat.id,f'Уязвимости: %s' % item)
                exploits = api.exploits.search(CVE)
            
    
        except:
            pass
    except:
        bot.send_message(messege.chat.id,"<code>По Shodan ничего не найдено</code>",parse_mode="html")

Help to combine the text in the for loop where the output of ports and vulnerabilities

I need it to be in 1 message and not 2
5efcfd97e027b481550662.png

Thanks in advance

Answer the question

In order to leave comments, you need to log in

2 answer(s)
T
Trefame, 2020-07-02
@Trefame

It is necessary to write the values ​​into the variable in a loop, and then output them. In PHP, this is implemented using the usual concatenation, I suspect that in python the solution to the problem is similar.

I
Igor, 2020-07-02
@adnim

First, do not shine SHODAN_API_KEY)
Answer:

ports = []
for item in host['data']:
    ports.append(item['port'])
bot.send_message(message.chat.id, f"Порты: {ports}" )

Or:
ports = ""
for item in host['data']:
    ports += str(item['port']) + ", "
bot.send_message(message.chat.id, f"Порты: {ports[:-2]}" )

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question