Search code examples
pythonremote-accessremote-serverwmic

Create directory in remote host using Python


I need to create directory in remote host using Python? Can I use wmic commands to do that or any suggestions to do this job ?

I am running Python script in Windows pc. I need to create a directory in remote Windows pc. How can I do this?


Solution

  • I will recommend using Fabric, it's a powerful python tool with a suite of operations for executing local or remote shell commands, as well as auxiliary functionality such as prompting the running user for input, or aborting execution:

    1. install fabric : pip install fabric
    2. write the following script named remote_mkdir.py:
    """
    Usage:
        python remote_mkdir.py ip_address username password folder_path
    """
    
    from sys import argv
    from fabric.api import run, env
    
    
    def set_host_config(ip, user, password):
        env.host_string = ip
        env.user = user
        env.password = password
    
    def mkdir(folder_absolute_path):
        """
        creates new folder
        """
        run('mkdir {0}'.format(folder_absolute_path))
    
    
    def main():
        set_host_config(argv[1], argv[2], argv[3])
        mkdir(argv[4]))
    
    if __name__ == '__main__':
        main()
    

    Usage:

    python remote_mkdir.py ip_address username password folder_path