invalid syntax for Python Telnet command - python

I am coding a script interpreter. It should generate a Telnet session to send AT commands.
Here is the script which it generated:
telentHandle = None
if telentHandle == None:
import telnetlib
telentHandle = telnetlib.Telnet(10.49.188.187, 23456)
telentHandle.read_until("login: ")
telentHandle.write(userName + "\n")
telentHandle.read_until("Password: ")
telentHandle.write(password + "\n")
telentHandle.write(AT + "\n")
When I run it, I get
File "H:/code/testgen/test_script.txt.py", line 10
telentHandle = telnetlib.Telnet(10.49.188.187, 23456)
^
SyntaxError: invalid syntax
What am I doing wrongly?

10.49.188.187 isn't a valid identifier in Python (or any language). You presumably need a string: "10.49.188.187".

Related

Trying to connect to a wifi network with a list of simple passwords with python

I have writen a script in python to attempt to connect to a defined wifi network with a list of simple passwords.
import wifimangement_linux as wifi
import os
wifiname = input("Enter Wifi SSID: ")
print("Wifi not stored, testing password...")
file = open('/home/asive/Documents/seclists/wordlists/passwords.lst', 'r')
line = file.readlines()
for i in line:
try:
print("Testing Key " + i.strip())
stream = os.popen("iwconfig wlan0 essid " + wifiname() + " key s:" + i.strip())
except:
print (i.strip() + " failed, trying next.")
else:
print ("Success Key==" + i.strip())
The script only runs through the program seemingly without trying to connect.
i've tried using subprocess instead of os and running in root, including adding sudo to the bash command.
What am i doing wrong?

What could be my invalid syntax error in my code?

I am having problems with a simple program I wrote but do not know where the problem is, and it is giving me a Syntax error.
This is my code:
username = {}
temp = True
while temp:
name = input("Please input your username: ")
response = input("What is the place you want to visit? ")
username[name] = response
end = input("Do you want to end the program? Yes/No ")
if end == 'Yes':
temp = False
print("These are the results of the poll: ")
for names, responses in username.items():
print(names + " wants to go to " + responses)
This is my error:
File "<stdin>", line 1
/usr/local/bin/python3 "/Users/eric/Python Notes/Notes.py"
^
SyntaxError: invalid syntax
Check out the accepted answer here:
syntax error when using command line in python
Looks like your problem is that you are trying to run python test.py from within the Python interpreter, which is why you're seeing that traceback.
Make sure you're out of the interpreter, then run the python test.py command from bash or command prompt or whatever.
There are also some VSCode-specific tips here:
Invalid Syntax error when running python from inside Visual Studio Code

Python Error: print subprocess.check_output - invalid syntax

Working on a CasperJS tutorial and I'm getting an error with my syntax. Using Python 3.5.1.
File: scrape.py
import os
import subprocess
APP_ROOT = os.path.dirname(os.path.realpath(__file__))
CASPER = '/projects/casperjs/bin/casperjs'
SCRIPT = os.path.join(APP_ROOT, 'test.js')
params = CASPER + ' ' + SCRIPT
print subprocess.check_output(params, shell=True)
Error:
File "scrape.py", line 10
print subprocess.check_output(params, shell=True)
^
SyntaxError: invalid syntax
YouTube Video tutorial: Learning to Scrape...
print subprocess.check_output(params, shell=True) is Python 2 syntax. print is a keyword in Python 2 and a function in Python 3. For the latter, you need to write:
print(subprocess.check_output(params, shell=True))

Python 2.7 having multiple say commands on mac

I have tried many things to try to get text to speech to work in python 2.7 on a mac. I managed to write some simple codes using the system os such as:
from os import system
system('say Hello world')
This works alone:
from os import system
string2 = 'test'
string1 = 'hello world' + string2 + '.'
system("say %s" %(string1))
But if I do multiple say commands, like this:
system('say Please tell me your name.')
name = raw_input()
st = "Hello. Want pie" + name + "?"
system("say " + st)
I get this error after I enter my name:
sh: -c: line 0: unexpected EOF while looking for matching `''
sh: -c: line 1: syntax error: unexpected end of file
Am I currently making a mistake in concept, or does having two say commands not work? If having two say commands do not work this way, then how can I use text to speech multiple times in python 2.7 with macintosh?
Trying changing the format and see if you have any better luck.
system('say Please tell me your name.')
name = raw_input()
st = "Hello. Want pie" + name + "?"
system('say %s' %(st))

Open a Python port from Erlang: no reply messages

Based on Chapter 12 of the OTP in Action book and Cesarini's book I wrote this Erlang code:
Erlang:
p(Param) ->
?DBG("Starting~n", []),
Cmd = "python test.py",
Port = open_port({spawn,Cmd}, [stream,{line, 1024}, exit_status]),
?DBG("Opened the port: ~w~n", [Port]),
Payload = term_to_binary(list_to_binary(integer_to_list(Param))),
erlang:port_command(Port, Payload),
?DBG("Sent command to port: ~w~n", [Payload]),
?DBG("Ready to receive results for command: ~w~n", [Payload]),
receive
{Port, {data, Data}} ->
?DBG("Received data: ~w~n", [Data]),
{result, Text} = binary_to_term(Data),
Blah = binary_to_list(Text),
io:format("~p~n", [Blah]);
Other ->
io:format("Unexpected data: ~p~n", [Other])
end.
Python:
import sys
def main():
while True:
line = sys.stdin.readline().strip()
if line == "stop-good":
return 0
elif line == "stop-bad":
return 1
sys.stdout.write("Python got ")
sys.stdout.write(line)
sys.stdout.write("\n")
sys.stdout.flush()
if __name__ == "__main__":
sys.exit(main())
The Erlang code suspends at the recieve clause - it never gets any message.
I have also checked Python from a regular Linux shell - it prints out every user input (1 - "Python got 1").
Where is the mistake here? Why doesn't my Erlang code get anything back?
There are two points:
make sure that Python does not buffer your output, try running python -u in open_port
using term_to_binary/1 and binary_to_term/1 won't work, since they assume that Python is able to encode/decode Erlang External Term Format, which does not seem to be the case. If you want to go this route, check out ErlPort
Does your Param contain the command limiter for Python? (in this case I assume newline, "\n"). Also, list_to_binary/1 and then a term_to_binary/1 feels kinda wrong. term_to_binary/1 directly (including the newline) should be sufficient.

Categories

Resources