0

I want to invoke multiple commands in Python. I installed putty in on Windows.

  1. ssh to Linux machine

    ssh_command_string: plink -i yourppk.ppk ec2-user@instanceIp
    
  2. invoke a command from that machine which will generate a file

    export_project_command_string: ./AppManage -export -out /opt/notme-Facebook.xml -app "Silver Fabric/notme-FacebookPostsExtraction0118" -user username-pw password -domain ion
    
  3. download this file

    download_command_string: pscp -scp -i yourppk.ppk ec2-user@instanceIp:/opt/notme-Facebook.xml d:\
    

These commands are ok when I test them one by one, but I don't know how to invoke them in Python all at once.

I have tried the following code, but no luck:

LINE_BUFFERED = 1
    #NOTE: the first argument is a list
    p = Popen(['cat'], shell = True, bufsize=LINE_BUFFERED, stdin=PIPE, stdout = PIPE, stderr = PIPE)
    for cmd in [ssh_command_string, cd_command_string, export_project_command_string, download_command_string]:
        time.sleep(1) # a delay to see that the commands appear one by one
        p.stdin.write(cmd)
        p.stdin.flush()
    # even without .flush() it works as expected on my machine
    p.stdin.close()
John Rotenstein
  • 241,921
  • 22
  • 380
  • 470
  • This may be useful to you: [What is the simplest way to SSH using Python?](http://stackoverflow.com/q/1233655/222914) – Janne Karila Mar 21 '13 at 09:47
  • it is the wrong way to comunictate with a process. but i think u forget to use Popen.comunicate to send your command to the process... with your code, if the remote shell send you some text , your buffer will be filled and the process will wait to release it, but you don't read p.stdout/p.stderr, so the remote process wait forever – ornoone Mar 21 '13 at 10:17

1 Answers1

0

look at fabric to execute repetitive/automatic tasks on a server.

ornoone
  • 651
  • 5
  • 11