be a code ninja

Code for Messaging

This post contains miscellaneous code for messaging.

  1. Detect Email addresses in Text
  2. Open text and find Email Addresses
  3. Simple Console App to Send Mail
  4. Find an SMTP Relay in a Domain
  5. Simple Mail Form
  6. Detect URL in Text
  7. Detect SMB & NFS links in text
  8. Detect PKI in Text
  9. Detect SIP in text
  10. Find SIP and Send Skype for Business
  11. Some code for credential handling
  12. Using GetPass
  13. Passing credentials from a Windows Session
  14. Reading Proxy Settings from Windows
  15. Retrieving Session information from a Browser
  16. Raising a Request in ServiceNow

Detect Email addresses in Text

Here’s a regular expression pattern that can help you extract email addresses from a given text:

import re

text = "This is a sample text with email addresses such as [email protected] or [email protected]"
pattern = r'\b[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,}\b'

emails = re.findall(pattern, text)
print(emails)

Output:

This regex pattern will match email addresses that follow the standard format, such as [email protected]. It allows for alphanumeric characters, as well as certain special characters like dot (.), underscore (_), percent (%), plus (+), and hyphen (-) in the username portion. The domain portion can include alphanumeric characters, hyphens (-), and dots (.) for subdomains.

Please note that while this regex pattern covers many common email address formats, it may not capture all possible variations or account for more complex email address structures.

Open text and find Email Addresses

Here’s an example Python code that opens a text document, reads through it, extracts all email addresses, sorts them in domain suffix and prefix order, removes duplicates, and outputs the sorted email addresses as a list into another text file:

import re

def extract_email_addresses(text):
    pattern = r'\b[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,}\b'
    return re.findall(pattern, text)

def sort_email_addresses(emails):
    return sorted(emails, key=lambda x: (x.split('@')[1], x.split('@')[0]))

def remove_duplicates(emails):
    return list(set(emails))

# Read input text file
input_file = 'input.txt'
with open(input_file, 'r') as file:
    text = file.read()

# Extract email addresses
emails = extract_email_addresses(text)

# Sort and remove duplicates
sorted_emails = sort_email_addresses(remove_duplicates(emails))

# Write output to text file
output_file = 'output.txt'
with open(output_file, 'w') as file:
    for email in sorted_emails:
        file.write(email + '\n')

Make sure to replace ‘input.txt’ with the path to your input text file. The extracted email addresses will be sorted first by domain suffix and then by prefix. The final sorted and deduplicated email addresses will be written to the ‘output.txt’ file, with each email address on a new line.

Please note that this code assumes that the text document contains email addresses in the expected format and doesn’t account for email addresses that span across multiple lines. You may need to adjust the regex pattern or the logic if your specific use case requires additional handling.

Here’s an updated version of the code that includes sending a preformatted email to each recipient in the output text file using an SMTP relay:

import re
import smtplib
from email.mime.text import MIMEText

def extract_email_addresses(text):
    pattern = r'\b[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,}\b'
    return re.findall(pattern, text)

def sort_email_addresses(emails):
    return sorted(emails, key=lambda x: (x.split('@')[1], x.split('@')[0]))

def remove_duplicates(emails):
    return list(set(emails))

# Read input text file
input_file = 'input.txt'
with open(input_file, 'r') as file:
    text = file.read()

# Extract email addresses
emails = extract_email_addresses(text)

# Sort and remove duplicates
sorted_emails = sort_email_addresses(remove_duplicates(emails))

# Write output to text file
output_file = 'output.txt'
with open(output_file, 'w') as file:
    for email in sorted_emails:
        file.write(email + '\n')

# SMTP Configuration
smtp_host = 'smtp.example.com'
smtp_port = 587
smtp_username = 'your_username'
smtp_password = 'your_password'

# Email Configuration
sender = '[email protected]'
subject = 'Example Subject'
message = 'Hello, This is an example email content.'

# Send emails
for recipient in sorted_emails:
    msg = MIMEText(message)
    msg['Subject'] = subject
    msg['From'] = sender
    msg['To'] = recipient

    try:
        with smtplib.SMTP(smtp_host, smtp_port) as server:
            server.starttls()
            server.login(smtp_username, smtp_password)
            server.send_message(msg)
        print(f"Email sent to {recipient}")
    except smtplib.SMTPException as e:
        print(f"Error sending email to {recipient}: {str(e)}")

Make sure to replace the placeholders ‘smtp.example.com’, 587, ‘your_username’, and ‘your_password’ with the appropriate values for your SMTP relay. Similarly, update ‘[email protected]’ with the sender’s email address and customize the subject and message variables as needed for your email content.

The code will iterate through the sorted and deduplicated email addresses, sending a preformatted email to each recipient using the provided SMTP relay. It uses the smtplib module to establish an SMTP connection, login with the provided credentials, and send the email using the send_message() method.

Please note that you may need to adjust the SMTP configuration parameters depending on the specific SMTP relay you are using. Additionally, ensure that your system allows outbound SMTP traffic on the specified port.

Simple Console App to Send Mail

Here’s an example of a C# console application that accepts command-line parameters for login, recipient, and message:

using System;

namespace CommandLineBinary
{
    class Program
    {
        static void Main(string[] args)
        {
            if (args.Length < 3)
            {
                Console.WriteLine("Usage: CommandLineBinary.exe <login> <recipient> <message>");
                return;
            }

            string login = args[0];
            string recipient = args[1];
            string message = args[2];

            // Your logic to process the parameters and send the message
            Console.WriteLine($"Login: {login}");
            Console.WriteLine($"Recipient: {recipient}");
            Console.WriteLine($"Message: {message}");

            // Call your function or method to send the message using the provided parameters

            Console.WriteLine("Message sent successfully!");
        }
    }
}

To compile and build this code into a command-line binary, follow these steps:

  • Create a new C# console application project in your preferred IDE (e.g., Visual Studio).
  • Replace the default Program.cs file content with the code provided above.
  • Build the project to generate the binary executable.

Once you have the compiled binary, you can run it from the command line, passing the necessary parameters:

CommandLineBinary.exe <login> <recipient> <message>

Replace login, recipient & messagewith the actual values you want to provide.

For example:

CommandLineBinary.exe [email protected] [email protected] "Hello, Jane! How are you?"

The code will retrieve the command-line parameters and process them accordingly. In this example, it displays the login, recipient, and message values. You can replace the logic inside the Main method with your own implementation to send the message using the provided parameters.

Please note that this is a basic example, and you would need to customize the logic to fit your specific requirements and integration with Skype for Business or any other messaging platform.

Here’s an example of a C# console application code that accepts command-line parameters for login, recipient, and message and sends an instant message using the Microsoft Teams Graph API:

using System;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;

namespace CommandLineBinary
{
    class Program
    {
        static async Task Main(string[] args)
        {
            if (args.Length < 3)
            {
                Console.WriteLine("Usage: CommandLineBinary.exe <login> <recipient> <message>");
                return;
            }

            string login = args[0];
            string recipient = args[1];
            string message = args[2];

            try
            {
                // Your logic to send the message using the provided parameters
                await SendInstantMessage(login, recipient, message);

                Console.WriteLine("Message sent successfully!");
            }
            catch (Exception ex)
            {
                Console.WriteLine($"Error sending message: {ex.Message}");
            }
        }

        static async Task SendInstantMessage(string login, string recipient, string message)
        {
            string accessToken = "YOUR_ACCESS_TOKEN"; // Replace with your access token

            using (var httpClient = new HttpClient())
            {
                httpClient.DefaultRequestHeaders.Add("Authorization", $"Bearer {accessToken}");

                var payload = new
                {
                    body = new
                    {
                        content = message
                    },
                    to = new[] { new { emailAddress = new { address = recipient } } }
                };

                var json = Newtonsoft.Json.JsonConvert.SerializeObject(payload);
                var content = new StringContent(json, Encoding.UTF8, "application/json");

                string endpoint = $"https://graph.microsoft.com/v1.0/me/messages";
                HttpResponseMessage response = await httpClient.PostAsync(endpoint, content);

                if (!response.IsSuccessStatusCode)
                {
                    string errorMessage = await response.Content.ReadAsStringAsync();
                    throw new Exception($"Failed to send message: {response.StatusCode} - {errorMessage}");
                }
            }
        }
    }
}

Make sure to replace ‘YOUR_ACCESS_TOKEN’ with the actual access token obtained through the authentication process for the Microsoft Graph API.

This code uses the HttpClient class to send a POST request to the Microsoft Graph API’s /me/messages endpoint, which sends an instant message to the specified recipient. The message content and recipient are provided as parameters from the command line.

Please note that this is a simplified example, and you will need to handle the authentication process to obtain the access token and ensure you have the necessary permissions to send messages on behalf of the user. Refer to the Microsoft Graph API documentation for detailed information on authentication and working with messages.

Once you have updated the code with your access token and customized the logic, you can compile and build the project into a command-line binary using your preferred C# development environment.

Find an SMTP Relay in a Domain

To find and check the SMTP relay for a given domain name, you can perform a DNS MX lookup to retrieve the mail exchanger (MX) records for the domain and then attempt to establish a connection to the SMTP server. Here’s an example of how you can achieve this in Python:

import smtplib
import dns.resolver

def find_smtp_relay(domain):
    try:
        # Perform DNS MX lookup to retrieve mail exchanger (MX) records
        mx_records = dns.resolver.query(domain, 'MX')

        # Sort the MX records by preference (priority)
        sorted_mx_records = sorted(mx_records, key=lambda x: x.preference)

        for mx_record in sorted_mx_records:
            smtp_server = str(mx_record.exchange)
            
            # Try to establish a connection to the SMTP server
            try:
                with smtplib.SMTP(smtp_server) as server:
                    print(f"SMTP relay found: {smtp_server}")
                    return

            except smtplib.SMTPConnectError as e:
                print(f"Failed to connect to SMTP server {smtp_server}: {e}")
                continue

        print("No SMTP relay found for the domain.")

    except dns.resolver.NXDOMAIN:
        print("Invalid domain name.")

# Example usage
domain_name = "example.com"
find_smtp_relay(domain_name)

In this code, we use the dns.resolver module from the dnspython library to perform the DNS MX lookup. We retrieve the MX records for the specified domain and sort them by preference (priority). Then, we iterate over the sorted MX records and attempt to establish a connection to the SMTP server using the smtplib.SMTP class from the smtplib module.

If a connection is successfully established to an SMTP server, it means we have found the SMTP relay for the domain. Otherwise, we continue trying with the next MX record until all records are exhausted or a connection is established.

Please make sure you have the dnspython library installed (pip install dnspython) to run this code successfully.

Let me know if you have any further questions!

Simple Mail Form

Here’s an example of a Python script using the tkinter library to create a simple form that inputs the “To”, “CC”, “Subject”, “Body”, and “Attachment” fields, and has a “Send” button to send the email:

import tkinter as tk
from tkinter import filedialog
import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.mime.base import MIMEBase
from email import encoders

def send_email():
    # Get input values
    to_address = to_entry.get()
    cc_address = cc_entry.get()
    subject = subject_entry.get()
    body = body_text.get("1.0", tk.END)

    # Create email message
    message = MIMEMultipart()
    message["From"] = "[email protected]"
    message["To"] = to_address
    message["Cc"] = cc_address
    message["Subject"] = subject

    # Attach body
    message.attach(MIMEText(body, "plain"))

    # Attach file if selected
    if attachment_path.get():
        attachment = open(attachment_path.get(), "rb")
        part = MIMEBase("application", "octet-stream")
        part.set_payload((attachment).read())
        encoders.encode_base64(part)
        part.add_header("Content-Disposition", "attachment; filename=attachment")
        message.attach(part)

    # Send the email
    smtp_server = "smtp.example.com"
    smtp_port = 587
    username = "[email protected]"
    password = "your_password"

    with smtplib.SMTP(smtp_server, smtp_port) as server:
        server.starttls()
        server.login(username, password)
        server.send_message(message)

    # Clear form fields
    to_entry.delete(0, tk.END)
    cc_entry.delete(0, tk.END)
    subject_entry.delete(0, tk.END)
    body_text.delete("1.0", tk.END)
    attachment_path.set("")

def attach_file():
    file_path = filedialog.askopenfilename()
    attachment_path.set(file_path)

# Create the main window
window = tk.Tk()
window.title("Email Form")

# Create form elements
tk.Label(window, text="To:").grid(row=0, sticky=tk.W)
tk.Label(window, text="CC:").grid(row=1, sticky=tk.W)
tk.Label(window, text="Subject:").grid(row=2, sticky=tk.W)
tk.Label(window, text="Body:").grid(row=3, sticky=tk.W)

to_entry = tk.Entry(window)
to_entry.grid(row=0, column=1)
cc_entry = tk.Entry(window)
cc_entry.grid(row=1, column=1)
subject_entry = tk.Entry(window)
subject_entry.grid(row=2, column=1)
body_text = tk.Text(window, height=10, width=40)
body_text.grid(row=3, column=1)

attachment_path = tk.StringVar()
attachment_button = tk.Button(window, text="Attach File", command=attach_file)
attachment_button.grid(row=4, column=0, sticky=tk.W)
attachment_label = tk.Label(window, textvariable=attachment_path)
attachment_label.grid(row=4, column=1, sticky=tk.W)

send_button = tk.Button(window, text="Send", command=send_email)
send_button.grid(row=5, column=0, columnspan=2)

# Run the application
window.mainloop()

Make sure you have the tkinter library installed (pip install tkinter) to run this code successfully.

When you run this script, a GUI window will appear with the form fields. You can enter the recipient’s email address in the “To” field, carbon copy (CC) email address in the “CC” field, subject in the “Subject” field, body text in the “Body” field, and attach a file by clicking the “Attach File” button. Finally, click the “Send” button to send the email.

Please note that you need to replace the placeholders in the code with your actual email address, SMTP server details, and authentication credentials. Also, ensure that you have the necessary permissions and correct SMTP server configuration to send emails.

Once you have updated the code with the required information, you can run the script, and the GUI window will allow you to input the email details and send the email when you click the “Send” button.

Detect URL in Text

Here’s a regular expression pattern that can help you detect URLs in a given text:

\b((?:https?|ftp):\/\/[^\s/$.?#].[^\s]*)\b

This regex pattern will match URLs that start with either “http://”, “https://”, or “ftp://” and continue until a whitespace character or special characters such as “/”, “?”, “#”, or “.” are encountered.

Here’s an example of how you could use this pattern in Python to detect URLs in a text string:

import re

text = "This is a sample text with a URL: https://www.example.com. Another URL: ftp://ftp.example.com/files"
pattern = r'\b((?:https?|ftp):\/\/[^\s/$.?#].[^\s]*)\b'

urls = re.findall(pattern, text)
print(urls)

Output:

['https://www.example.com', 'ftp://ftp.example.com/files']

Please note that this regex pattern might not capture all possible URL variations, as URL formats can be quite complex.

If you’re looking to extract SMB or NFS links from a text, you can use the following regular expression pattern:

import re

text = "This is a sample text with SMB links like smb://server/share and NFS links like nfs://server:/path/to/share"
pattern = r'\b(?:smb|nfs):\/\/[\w.-]+(?:\/[\w.-]+)*\b'

links = re.findall(pattern, text)
print(links)

Output:

['smb://server/share', 'nfs://server:/path/to/share']

This regex pattern matches strings starting with either “smb://” or “nfs://” and followed by alphanumeric characters, dots (.), or hyphens (-) in the server and path sections. The path can have multiple segments separated by slashes (/).

Please note that this pattern assumes a basic structure for SMB and NFS links and may not account for all possible variations or edge cases.

Detect PKI in Text

To find PKI (Public Key Infrastructure) blocks in text, you can use the following regular expression pattern:

import re

text = """
-----BEGIN CERTIFICATE-----
MIICzzCCAbegAwIBAgIJAI7hxvUDQ2B9MA0GCSqGSIb3DQEBCwUAMEwxCzAJBgNV
...
-----END CERTIFICATE-----
Some random text
-----BEGIN PRIVATE KEY-----
MIIJKQIBADANBgkqhkiG9w0BAQEFAASCCSwwggkoAgEAAoICAQC4y8Tgo2gflOuCm
...
-----END PRIVATE KEY-----
"""

pattern = r'-----BEGIN (?:CERTIFICATE|PRIVATE KEY)-----\n.*?\n-----END (?:CERTIFICATE|PRIVATE KEY)-----'

blocks = re.findall(pattern, text, re.DOTALL)
print(blocks)

Output:

['-----BEGIN CERTIFICATE-----\nMIICzzCCAbegAwIBAgIJAI7hxvUDQ2B9MA0GCSqGSIb3DQEBCwUAMEwxCzAJBgNV\n...\n-----END CERTIFICATE-----', '-----BEGIN PRIVATE KEY-----\nMIIJKQIBADANBgkqhkiG9w0BAQEFAASCCSwwggkoAgEAAoICAQC4y8Tgo2gflOuCm\n...\n-----END PRIVATE KEY-----']

This regex pattern will match blocks starting with “—–BEGIN CERTIFICATE—–” or “—–BEGIN PRIVATE KEY—–” and ending with “—–END CERTIFICATE—–” or “—–END PRIVATE KEY—–“. The re.DOTALL flag is used to ensure that the pattern matches across multiple lines, allowing for matching the contents between the begin and end markers.

Please note that the pattern assumes that the PKI blocks are surrounded by the specified markers and that there are no other blocks present in the text that match the pattern. Adjustments may be necessary depending on the specific structure and formatting of the PKI blocks you are dealing with.

Detect SIP in text

To find SIP addresses in text, you can use the following regular expression pattern:

import re

text = "This is a sample text with SIP addresses like sip:[email protected] and sip:[email protected]"
pattern = r'\bsip:[\w.-]+@[\w.-]+\b'

addresses = re.findall(pattern, text)
print(addresses)

Output:

This regex pattern matches strings starting with “sip:” followed by one or more alphanumeric characters, dots (.), or hyphens (-) in the username portion, and then an “@” symbol. After the “@” symbol, it matches one or more alphanumeric characters, dots (.), or hyphens (-) in the domain portion.

Please note that this pattern assumes a basic structure for SIP addresses and may not account for all possible variations or edge cases. Adjustments may be required based on your specific requirements or the format of SIP addresses in your text.

Find SIP and Send Skype for Business

Skype for Business does provide APIs for integration with other services or applications, but these APIs are typically used for building custom solutions within the Skype for Business ecosystem rather than sending direct instant messages to SIP addresses. If you are looking to send instant messages to SIP addresses, you might consider using other messaging platforms or APIs that support such functionality, like Microsoft Teams, which offers APIs for sending messages.

If you are using a private Skype for Business deployment within your company, you may have access to the Skype for Business Server SDK (Software Development Kit). With the SDK, you can interact with the Skype for Business Server and perform various operations, including retrieving user information and validating SIP addresses.

Here’s an example of how you can use the Skype for Business Server SDK in C# to check for company SIP addresses:

using System;
using Microsoft.Rtc.Signaling;

namespace SkypeForBusinessSIPValidation
{
    class Program
    {
        static void Main(string[] args)
        {
            // Configure the connection settings
            string serverUri = "sip:sipserver.company.com";
            string username = "yourusername";
            string password = "yourpassword";

            try
            {
                // Establish a connection to the Skype for Business Server
                using (var platform = new CollaborationPlatform())
                {
                    platform.BeginStartup(serverUri, ar =>
                    {
                        platform.EndStartup(ar);

                        // Sign in to the Skype for Business Server
                        var endpointSettings = new UserEndpointSettings(username, password);
                        var userEndpoint = new UserEndpoint(platform, endpointSettings);
                        userEndpoint.BeginEstablish(ar2 =>
                        {
                            userEndpoint.EndEstablish(ar2);

                            // Validate SIP addresses
                            string[] sipAddresses = { "sip:[email protected]", "sip:[email protected]" };
                            foreach (var sipAddress in sipAddresses)
                            {
                                var result = userEndpoint.IsValidSipUri(sipAddress);
                                Console.WriteLine($"{sipAddress}: {result}");
                            }

                            // Sign out and shut down the connection
                            userEndpoint.BeginShutdown(ar3 =>
                            {
                                userEndpoint.EndShutdown(ar3);
                            }, null);
                        }, null);
                    }, null);

                    Console.ReadLine();
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine($"Error: {ex.Message}");
            }
        }
    }
}

In this example, you need to replace ‘sipserver.company.com’ with the appropriate server URI of your private Skype for Business deployment. Set ‘yourusername’ and ‘yourpassword’ to your valid Skype for Business credentials.

The code establishes a connection to the Skype for Business Server using the provided credentials. It then validates a list of SIP addresses by calling the IsValidSipUri method on the user endpoint. Finally, it signs out and shuts down the connection.

Please note that the availability and usage of the Skype for Business Server SDK may vary depending on your specific deployment and licensing. Make sure you have the necessary permissions and access rights to interact with the Skype for Business Server using the SDK.

It’s recommended to refer to the official documentation and resources provided by Microsoft for further details on working with the Skype for Business Server SDK in your specific environment.

If you are looking to interact with Skype for Business in Python, an alternative approach would be to use the Skype Web SDK or Microsoft Teams Graph API, both of which provide RESTful APIs for integrating with Skype for Business or Microsoft Teams.

The Microsoft Teams Graph API allows you to access various features and functionality of Microsoft Teams, including sending messages and interacting with users. The Microsoft Graph API provides a broader set of capabilities that encompass multiple Microsoft services, including Microsoft Teams.

To get started with using the Microsoft Teams Graph API or the Microsoft Graph API in Python, you will need to authenticate your application and make HTTP requests to the corresponding endpoints. You can use libraries such as requests or msal (Microsoft Authentication Library) in Python to facilitate the authentication and HTTP requests.

Here’s a high-level example of how you might use the Microsoft Teams Graph API to send a message to a user:

import requests

# Microsoft Teams Graph API endpoint
api_url = 'https://graph.microsoft.com/v1.0/teams/{teamId}/channels/{channelId}/messages'

# Authentication headers
access_token = 'YOUR_ACCESS_TOKEN'
headers = {
    'Authorization': 'Bearer ' + access_token,
    'Content-Type': 'application/json'
}

# Message payload
payload = {
    'body': {
        'content': 'Hello, this is a test message!'
    }
}

# Send the message
response = requests.post(api_url, headers=headers, json=payload)

# Check the response status
if response.status_code == 201:
    print('Message sent successfully!')
else:
    print('Failed to send the message:', response.text)

Please note that this is a simplified example, and you would need to obtain an access token and provide the appropriate team and channel IDs as per your specific requirements. You would also need to handle the authentication process and obtain the access token using the Microsoft identity platform.

It’s recommended to refer to the official Microsoft Teams Graph API documentation for detailed information on the available endpoints, authentication process, and how to use the API effectively.

Some code for credential handling

Using GetPass

The getpass module in Python provides a secure way to handle user input for sensitive information, such as passwords or other credentials, without displaying the input on the screen. It is commonly used for command-line interfaces or scripts where user interaction is required.

The primary function provided by the getpass module is getpass.getpass(prompt=’Prompt’). It displays the prompt message to the user and then waits for input. Unlike the standard input() function, getpass.getpass() obscures the input, usually by replacing each character with an asterisk (*), preventing it from being displayed on the screen.

Here’s a simple example of how to use getpass:

import getpass

# Prompt the user for a password
password = getpass.getpass("Enter your password: ")

# Access the password securely
print("Password:", password)

When you run this code, the prompt message “Enter your password:” will be displayed, and the user can type their password securely without the characters being echoed on the screen. The entered password will be stored in the password variable and can be used further in your program.

By using getpass, you can enhance the security of your applications or scripts by ensuring that sensitive user input, like passwords, remains hidden from onlookers or potential security risks.

To read the user ID from a Windows session using Python, you can utilize the getpass module to prompt the user for their username and securely retrieve the input. Here’s an example code snippet:

import getpass

# Read the user ID from Windows session
user_id = getpass.getuser()

# Display the user ID
print("User ID:", user_id)

When you run this code, it will prompt you to enter your Windows username. Once you provide the input, it will retrieve and display your user ID.

Please note that this code will only work if you are running it on a Windows operating system.

Passing credentials from a Windows Session

To execute code with your current Windows username and password, using them to connect through a company proxy server and access a web service with Single Sign-On (SSO), you can utilize the requests library in Python, which supports handling proxies and authentication. Here’s an example code snippet:

import requests
from requests_ntlm import HttpNtlmAuth

# Proxy server details
proxy_server = 'http://proxy-server-url:port'  # Replace with the actual proxy server URL and port

# Web service URL
web_service_url = 'https://web-service-url'  # Replace with the actual web service URL

# Web service endpoint
web_service_endpoint = '/api/endpoint'  # Replace with the actual endpoint of the web service

# Windows credentials
username = 'your-username'  # Replace with your Windows username
password = 'your-password'  # Replace with your Windows password

# Create a session with proxy settings
session = requests.Session()
session.proxies = {
    'http': proxy_server,
    'https': proxy_server,
}

# Authenticate with Windows credentials
session.auth = HttpNtlmAuth(username, password)

# Make a request to the web service
response = session.get(web_service_url + web_service_endpoint)

# Check the response status code
if response.status_code == 200:
    print('Request successful.')
    print('Response:', response.text)
else:
    print('Error making the request.')
    print('Response:', response.text)

Make sure to replace ‘http://proxy-server-url:port’ with the actual URL and port of your company’s proxy server, ‘https://web-service-url‘ with the actual URL of the web service you are accessing, and ‘your-username’ and ‘your-password’ with your Windows username and password, respectively.

The code sets up a session with the proxy server and uses NTLM authentication to pass your Windows credentials. It then makes a request to the web service using the session. If the request is successful (status code 200), it prints the response text. Otherwise, it prints an error message along with the response text.

Please note that the exact authentication mechanism and proxy configuration may vary depending on your company’s setup. Make sure to adapt the code accordingly to match your specific requirements.

Reading Proxy Settings from Windows

To read the proxy settings from Windows using Python, you can utilize the winreg module to access the Windows Registry and retrieve the proxy configuration. Here’s an example code snippet:

import winreg

# Registry key for Internet Settings
internet_settings_key = winreg.OpenKey(
    winreg.HKEY_CURRENT_USER,
    r'Software\Microsoft\Windows\CurrentVersion\Internet Settings'
)

# Read the ProxyEnable value (0 or 1)
proxy_enabled = winreg.QueryValueEx(internet_settings_key, 'ProxyEnable')[0]

if proxy_enabled:
    # Read the ProxyServer value (proxy server address and port)
    proxy_server = winreg.QueryValueEx(internet_settings_key, 'ProxyServer')[0]

    # Read the ProxyOverride value (addresses that bypass the proxy)
    proxy_override = winreg.QueryValueEx(internet_settings_key, 'ProxyOverride')[0]

    # Display the proxy settings
    print("Proxy Server:", proxy_server)
    print("Proxy Override:", proxy_override)
else:
    print("Proxy is not enabled.")

# Close the registry key
winreg.CloseKey(internet_settings_key)

This code opens the Windows Registry key for Internet Settings (Software\Microsoft\Windows\CurrentVersion\Internet Settings). It then reads the ProxyEnable value to determine if the proxy is enabled. If it is enabled, it reads the ProxyServer value, which contains the proxy server address and port, and the ProxyOverride value, which specifies addresses that bypass the proxy. Finally, it displays the proxy settings.

Please note that this code retrieves the proxy settings for the current user from the Windows Registry. Keep in mind that the exact registry keys and values may vary depending on the Windows version or the configuration of the system.

Retrieving Session information from a Browser

Retrieving the username and session identity from the current web browser session is not directly possible using Python alone. The web browser session is managed by the browser itself and is typically not directly accessible by external programs or scripts.

However, if you’re working with web applications, you can implement functionality within the web application itself to retrieve the username and session identity. The web application can store user session information and provide an API or endpoint that allows external programs to retrieve session details.

Alternatively, if you have control over the web browser session and the website you are accessing, you can utilize browser automation tools like Selenium to interact with the browser programmatically and extract information such as the username and session identity. Selenium provides a Python API that allows you to automate browser actions and retrieve various data from the web page.

Here’s an example of using Selenium WebDriver to extract information from a web page:

from selenium import webdriver

# Initialize the Selenium WebDriver
driver = webdriver.Chrome()  # Provide the appropriate WebDriver for your browser (e.g., Chrome, Firefox)

# Open a web page
driver.get('https://example.com')  # Replace with the URL of the web page you want to access

# Extract the username from the web page
username_element = driver.find_element_by_id('username')  # Replace 'username' with the appropriate element ID
username = username_element.get_attribute('value')
print('Username:', username)

# Extract the session identity from the web page
session_element = driver.find_element_by_id('session')  # Replace 'session' with the appropriate element ID
session_identity = session_element.get_attribute('value')
print('Session Identity:', session_identity)

# Close the browser
driver.quit()

In this example, we are using the Selenium WebDriver to open a web page and extract the username and session identity from specific HTML elements on the page. You need to replace ‘https://example.com‘ with the actual URL of the web page you want to access and ‘username’ and ‘session’ with the appropriate element IDs or other selectors that can uniquely identify the desired information on the page.

Please note that Selenium requires an appropriate WebDriver to interface with the web browser. You need to download and configure the WebDriver for the specific browser you are using (e.g., Chrome WebDriver for Chrome browser).

Raising a Request in ServiceNow

To raise a request in ServiceNow, you would typically use their API to interact with the platform programmatically. Below is an example code snippet in Python that demonstrates how you can make a POST request to create a new incident in ServiceNow using the requests library:

import requests

# ServiceNow instance details
instance = 'your-instance.service-now.com'
username = 'your-username'
password = 'your-password'

# Request endpoint URL
url = f'https://{instance}/api/now/table/incident'

# Request headers
headers = {
    'Content-Type': 'application/json',
    'Accept': 'application/json'
}

# Request payload (incident data)
payload = {
    'short_description': 'Issue description',
    'description': 'Detailed issue description',
    'caller_id': 'John Doe'  # replace with appropriate caller ID or user
}

# Make the POST request
response = requests.post(url, auth=(username, password), headers=headers, json=payload)

# Check the response status code
if response.status_code == 201:
    print('Request created successfully.')
else:
    print('Error creating request.')
    print('Response:', response.text)

Make sure to replace ‘your-instance.service-now.com’, ‘your-username’, ‘your-password’, and the payload data with the appropriate values for your ServiceNow instance. Additionally, you may need to adjust the payload structure based on the specific fields you want to include in your incident request.