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

👤 Login to reply

    #1  
Old 05-16-2018
SkySmart SkySmart is offline
Registered User
 
Join Date: Dec 2006
Last Activity: 18 July 2018, 10:22 PM EDT
Posts: 919
Thanks: 757
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 05-16-2018
wisecracker's Unix or Linux Image
wisecracker wisecracker is offline
Registered User
 
Join Date: Jan 2013
Last Activity: 21 July 2018, 12:14 AM EDT
Location: Loughborough
Posts: 1,296
Thanks: 388
Thanked 353 Times in 278 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 05-16-2018
Corona688 Corona688 is offline Forum Staff  
Mead Rotor
 
Join Date: Aug 2005
Last Activity: 20 July 2018, 3:26 PM EDT
Location: Saskatchewan
Posts: 22,727
Thanks: 1,194
Thanked 4,351 Times in 4,006 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 (05-16-2018)
Sponsored Links
👤 Login to reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

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 01:15 PM.

Unix & Linux Forums Content Copyright1993-2018. All Rights Reserved.
×
UNIX.COM Login
Username:
Password:  
Show Password





Not a Forum Member?
Forgot Password?