Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Automate MML Commands
#1
I need to extract files from U2020 server. For this purpose i need to run MML Commands.I have the commands with me.But each time instead of manually entering the MML commands from the CLI ,i want to automate it through python code.So in the code itself i need to login to u2020 and run the MML commands.How do i do this in python
Reply
#2
Do you log in using the ssh command?

Do MML commands need to be sequential?

Do you have and want to process some responses from those commands?
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#3
I am not logging in using ssh command.I am rather using :

telnet floating IP address of the U2020 server portnumber
MML commands need not be sequential

After running the MML commands,i will be able to extract some files from server.The operations i want to perform will be on those files only
Reply
#4
You can try something like this:

import concurrent.futures
import subprocess

commands = """
# command 1
# command 2
# ...
# command n
"""
# Or! You can get them from a file

# make a list of these commands and prepare them for subprocess.run
commands_list = [command.split() for command in commands.split('/n')] 

with concurrent.futures.ThreadPoolExecutor() as executor:
    executor.map(subprocess.run, commands_list)
This should execute the commands in parallel.
Getting the files can fallow the same code pattern.

subprocess.run executes any shell ( bash for example or Windows' cmd/PowerShell ) command.
The command is represented as a list of its parts. Your example becomes

['telnet', 'floating IP address of the U2020 server', 'portnumber']
I presume that 'floating IP address of the U2020 server' is something like 192.168.1.100 or some web address? This is why I put it as whole string in the list


Note that there is no code to handle any errors here
saisankalpj likes this post
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#5
Thanks for the quick solution
'floating IP address of the U2020 server' is like 192.168.1.100 and not web address.

What change should i make in the above code if commands needs to be executed sequentially?
Reply
#6
Instead of importing concurrent.futures and using ThreadPoolExecutor you just do it in a loop

for command in commands_list:
    subproces.run(command)
That way you get commands one by one from commands_list, assigning each to command in the for loop then run it. When first command returns ( finish execution ) next follows.
saisankalpj likes this post
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#7
Thanks soo much
Reply
#8
Hi @wavic,

There was initial plan to login using telnet.The plan is now changed and i am supposed to login using ssh(That was your first question).

Now will the code you suggested change when logging in using ssh?
Please let me know if any changes are required in the solution provided by you

Thanks
Reply
#9
The code should change. The previous one too.

I just showed you how you can loop over the commands and them. The moment I posted my answer though it crossed my mind that you have to connect successfully before anything else. But I was in a hurry to correct it.

So before the for loop, on a single line of code just use the subprocess module to connect to the server then you can loop.

REMOTE_ADDRESS = '192.168.1.113'

proc = subprocess.run(['ssh', REMOTE_ADDRESS])

if proc.returncode == 1:
    # loop over commands here
A return code 0 means that the command executed successfully with no errors - or we have 0 errors literally. You are connected.
saisankalpj likes this post
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#10
Hi @wavic

output=subprocess.run(['ssh', '[email protected]'])
print(output.returncode)
While i used the above code to login to server The return code i got is 255.
any reason why this occurs?
I personally feel that Running a ssh command through subprocess doesnt provide the option to enter the password,as is the case here and hence i am getting 255(Correct me if wrong)
In my condition after logging in to [email protected], i need to execute a set of MML commands.But right now i am stuck with this first step only.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  1. How can I automate this batch creation of documents? 2. How can I automate posting SamLearnsPython 2 3,457 Jul-02-2018, 11:36 AM
Last Post: buran

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020