Unix/Linux Go Back    


Shell Programming and Scripting BSD, Linux, and UNIX shell scripting — Post awk, bash, csh, ksh, perl, php, python, sed, sh, shell scripts, and other shell scripting languages questions here.

Python: Redirecting to tty and reading from tty

Shell Programming and Scripting


Reply    
 
Thread Tools Search this Thread Display Modes
    #1  
Old Unix and Linux 1 Week Ago   -   Original Discussion by SkySmart
SkySmart's Unix or Linux Image
SkySmart SkySmart is offline
Registered User
 
Join Date: Dec 2006
Last Activity: 19 May 2018, 4:32 PM EDT
Posts: 915
Thanks: 753
Thanked 3 Times in 3 Posts
Python: Redirecting to tty and reading from tty

In bash, you can do something like this:



Code:
#!/bin/bash
echo -n "What is your name? " > /dev/tty
read thename < /dev/tty

How can I do the same in python?

I have a python script that has the following content:



Code:
#!/usr/bin/python2.7

import getpass
import sys
import telnetlib
import time
print ""
uip = raw_input("Enter your IP Address: ")
print ""
uname = raw_input("Enter your username: ")
print ""
upass = getpass.getpass("Enter your password: ")
tn = telnetlib.Telnet(uip)
print ""
print "Successfully Connected to", uip
print ""

The bash script works even when it is run through a pipe as in:



Code:
cat bashscript.sh | bash

Whereas the python script doesnt work when run like this:



Code:
cat pythonscript.py | python

I need to be able to read from tty when running a script through a pipe.
Sponsored Links
    #2  
Old Unix and Linux 1 Week Ago   -   Original Discussion by SkySmart
wisecracker's Unix or Linux Image
wisecracker wisecracker is offline
Registered User
 
Join Date: Jan 2013
Last Activity: 19 May 2018, 3:23 PM EDT
Location: Loughborough
Posts: 1,261
Thanks: 377
Thanked 338 Times in 265 Posts
Hi...

I am missing something, why do you need a python script to be piped into python?
You are already calling python in your shebang...
Longhand OSX 10.13.4, default bash terminal.


Code:
Last login: Wed May 16 17:56:35 on ttys000
AMIGA:barrywalker~> cd Desktop/Code/Python
AMIGA:barrywalker~/Desktop/Code/Python> chmod 755 test.py
AMIGA:barrywalker~/Desktop/Code/Python> cat test.py
#!/usr/bin/python
import sys
print("This is a test line.")
text = raw_input("Enter your name: ")
print(text)
sys.exit()
AMIGA:barrywalker~/Desktop/Code/Python> ./test.py
This is a test line.
Enter your name: My name is Bazza...
My name is Bazza...
AMIGA:barrywalker~/Desktop/Code/Python> _

Sponsored Links
    #3  
Old Unix and Linux 1 Week Ago   -   Original Discussion by SkySmart
Corona688's Unix or Linux Image
Corona688 Corona688 is offline Forum Staff  
Mead Rotor
 
Join Date: Aug 2005
Last Activity: 25 May 2018, 3:04 PM EDT
Location: Saskatchewan
Posts: 22,680
Thanks: 1,179
Thanked 4,324 Times in 3,987 Posts
Quote:
Originally Posted by wisecracker View Post
I am missing something, why do you need a python script to be piped into python?
Security through obscurity. Skysmart runs programs through a long train of generators and mutators in the theory that this will prevent people from wanting to use it anywhere else.
The Following User Says Thank You to Corona688 For This Useful Post:
wisecracker (1 Week Ago)
Sponsored Links
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Linux More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Reading password from /dev/tty pandeesh UNIX for Dummies Questions & Answers 3 01-30-2012 04:13 PM
tty changes? cokedude UNIX for Advanced & Expert Users 6 11-11-2010 05:39 AM
tty hack xxmasrawy Solaris 1 10-11-2010 01:44 PM
What does this TTY info mean? mojoman UNIX for Dummies Questions & Answers 1 04-18-2009 05:36 AM
tty magasem AIX 1 11-28-2006 02:58 AM



All times are GMT -4. The time now is 11:31 AM.