Search code examples
pythonamazon-web-servicesoperating-systemsystem

Control virtual machine from script


I am trying to create a script to access the ec2 of aws from a script. At first, I used:

os.system('ssh -i "projectKey.pem" ubuntu@' + IP)
os.system("sudo bash")
os.system("apt-get update")

when it accesses the remote server, it does not run anything. When I exit, it tries to update in my own computer, is there any way to control the remote server from the script?


Solution

  • a simple way to do what you want would be saving your apt-get update and other commands if you need as a script file e.g. script.sh and run it as one-liner:

    os.system('ssh -i "projectKey.pem" ubuntu@' +  IP + " bash /dev/stdin < script.sh")
    

    don't forget sudo.