Home Man
Search
Today's Posts
Register

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

Tags
shell scripts

Login to Reply

 
Thread Tools Search this Thread
# 1  
Old 05-16-2018
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.
# 2  
Old 05-16-2018
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> _

# 3  
Old 05-16-2018
Quote:
Originally Posted by wisecracker
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)
Login to Reply

« Previous Thread | Next Thread »
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
Decimals reading Python Board27 Programming 2 09-05-2015 10:04 PM
**python** unable to read the background color in python giridhar276 Shell Programming and Scripting 1 08-05-2014 02:53 AM
Python Reading Individual files and Regex through them metallica1973 Programming 4 11-19-2013 05:38 PM
Python reading from a file jaldo0805 Programming 0 06-26-2013 04:22 PM
Python reading from a file jaldo0805 Programming 0 06-26-2013 04:13 PM
"ssh suse-server 'python -V' > python-version.out" not redirecting Druonysus SuSE 3 02-13-2013 08:59 PM
Reading UNIX commands from file and redirecting output to a file rahulparo Shell Programming and Scripting 8 07-13-2011 08:09 AM
Help Reading XML files in Python: Urgent dcfivesixfive Shell Programming and Scripting 2 05-12-2009 07:25 AM
reading reading data from webpage phani_sree Programming 3 11-01-2007 01:28 PM


All times are GMT -4. The time now is 09:00 PM.

Unix & Linux Forums Content Copyright©1993-2018. All Rights Reserved.
UNIX.COM Login
Username:
Password:  
Show Password