I have a command to obtain the last logged in time of a certain ubuntu user and I need the output to store it elsewhere so when I run this in my python script, I get a syntax error but when I ssh
into remote server and execute the command, there's no problem.
# last logged in time of ubuntu user
user_login = os.popen('ssh -i /Users/abcxyz/keypair ubuntu@1#.###.##.# lastlog -u 'ubuntu' | grep -v Latest | awk '{$1="";$2="";$3="";print $0 }'').read()
print(user_output)
when I just run this in my terminal, it works fine and gives me the output:
ssh -i /Users/abcxyz/keypair ubuntu@1#.###.##.# lastlog -u 'ubuntu' | grep -v Latest | awk '{$1="";$2="";$3="";print $0 }'
Output: Sat Nov 17 16:32:10 +0000 2018
Since your string has both single and double quotes inside of it, I'd triple-quote the whole thing
user_login = os.popen("""ssh -i /Users/abcxyz/keypair ubuntu@1#.###.##.# lastlog -u 'ubuntu' | grep -v Latest | awk '{$1="";$2="";$3="";print $0 }'""").read()
Otherwise as written some of the single quotes inside your string are terminating the full length string in the middle