Python script not able to read user input when run remotely -
i trying remotely execute simple python script userinfo.py present in remotehost.
below sourcecode of userinfo.py [ using python 2.7.10 ]
############# print "userinfo :" name=raw_input("enter name") age=raw_input("enter age") print "name"+name+"\nage"+age #############
but script working abnormally when run remotely.
[user@localhost]# ssh remotehost python /home/userinfo.py
userinfo :
enter nameenter agename
age
[user@localhost]#
execution summary ::
during execution, doesn't print anything, directly waits user input , pressed enter key display output above.
would know why not behaving expected when raw_input used.
when values passed arguments, works fine.
[user@localhost]# ssh remotehost python userinfo.py xyz 20
userinfo :
name xyz
age 20
[user@localhost]#
below changed code.
########### import sys print "userinfo :" name=sys.argv[1] age=sys.argv[2] print "name "+name+"\nage "+age ############
would know why interactive way not working expected , may fix.
in regular terminal, raw_input prompt flushed immediately, meaning see prompt "enter name".
if run script through ssh, saves output until script finished , prints in buffer.
what need run python unbuffered, force stdout flush after every output, , display ssh session. can accomplished several ways.
ssh user@remotehost python -u script.py
or make file executable and unbuffered adding following top of .py script. sure use actual python path here:
#! /usr/bin/python - u
and in make executable
sudo chmod +x script.py
then
ssh user@remotehost ./script.py
Comments
Post a Comment