Kohomada me python progrem ek

hecker_thama

Well-known member
  • Dec 27, 2022
    6,764
    6,977
    113
    1692099540809.png

    Python:
    import paramiko
    import getpass
    
    def ssh_command(ip, port, user, passwd, cmd):
        client = paramiko.SSHClient()
        client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
        client.connect(ip, port=port, username=user, password=passwd)
    
        _, stdout, stderr = client.exec_command(cmd)
        output = stdout.readlines() + stderr.readlines()
        if output:
            print("------------ output ------------")
            for line in output:
                print(line.strip())
        client.close()
    
    if __name__ == '__main__':
        user = input("Username: ")
        password = getpass.getpass()
    
        ip = input("Enter Server IP: ")
        port = int(input("Enter port: "))
        cmd = input("Enter your command: ")
    
        ssh_command(ip, port, user, password, cmd)
     

    MBKESCM

    Well-known member
  • Jun 5, 2023
    701
    1,878
    93
    This script allows a user to execute SSH commands on a remote server. It makes use of the paramiko library to handle SSH connections and the getpass library to securely prompt for a password. Below are some comments and suggestions for possible improvements:

    1. Handling Invalid Input: Currently, the script does not check for invalid user input (e.g., invalid IP address format, non-integer port number, etc.). You could add error handling to account for these scenarios.
    2. Logging and Debugging: Consider adding logging to the script to help with debugging. This can be done with Python's built-in logging module.
    3. Security: While paramiko.AutoAddPolicy() is useful for accepting any host key (i.e., it will automatically add the host key for the server), it can be insecure as it does not verify the server's identity. You might want to consider using paramiko.WarningPolicy() to at least warn the user if the host key is not already known, or better yet, handle host keys more securely.
    4. Output Formatting: The current script simply prints the output directly to the console. Consider providing options for saving the output to a file or formatting it differently.
    5. Exception Handling: It is a good idea to add exception handling for cases where the connection fails, the credentials are incorrect, or the command cannot be executed. This way, you can provide a more user-friendly error message.
    6. Using Context Managers: Instead of manually closing the SSH client, you could use it as a context manager to ensure that it is properly closed even if an exception occurs.
    7. Additional Functionality: You could consider extending the script to handle more advanced use cases, such as executing multiple commands, uploading or downloading files via SFTP, or managing multiple servers.
    Overall, this script serves as a good starting point for executing commands on a remote server via SSH. By adding some of the suggested improvements, it could be made more robust and user-friendly.
     
    • Like
    Reactions: Asmodeus

    hecker_thama

    Well-known member
  • Dec 27, 2022
    6,764
    6,977
    113
    This script is straightforward and functional for executing a single command on a remote server via SSH.
    ow single command thama

    This script allows a user to execute SSH commands on a remote server. It makes use of the paramiko library to handle SSH connections and the getpass library to securely prompt for a password. Below are some comments and suggestions for possible improvements:

    1. Handling Invalid Input: Currently, the script does not check for invalid user input (e.g., invalid IP address format, non-integer port number, etc.). You could add error handling to account for these scenarios.
    2. Logging and Debugging: Consider adding logging to the script to help with debugging. This can be done with Python's built-in logging module.
    3. Security: While paramiko.AutoAddPolicy() is useful for accepting any host key (i.e., it will automatically add the host key for the server), it can be insecure as it does not verify the server's identity. You might want to consider using paramiko.WarningPolicy() to at least warn the user if the host key is not already known, or better yet, handle host keys more securely.
    4. Output Formatting: The current script simply prints the output directly to the console. Consider providing options for saving the output to a file or formatting it differently.
    5. Exception Handling: It is a good idea to add exception handling for cases where the connection fails, the credentials are incorrect, or the command cannot be executed. This way, you can provide a more user-friendly error message.
    6. Using Context Managers: Instead of manually closing the SSH client, you could use it as a context manager to ensure that it is properly closed even if an exception occurs.
    7. Additional Functionality: You could consider extending the script to handle more advanced use cases, such as executing multiple commands, uploading or downloading files via SFTP, or managing multiple servers.
    Overall, this script serves as a good starting point for executing commands on a remote server via SSH. By adding some of the suggested improvements, it could be made more robust and user-friendly.
    black hat python book eka study krnne
    ------ Post added on Aug 15, 2023 at 5:30 PM