So I have this script I can't get working. I have verified it's the script, as I can cmd->telnet to localhost 4212 and are able to change vlc there(like next and stop).
But the script doesn't give any errors either. I have python installed:
#!/usr/bin/python
import sys
import telnetlib
cmd = " ".join(sys.argv[1:])
tn = telnetlib.Telnet("localhost", 4212)
tn.read_until("Password: ")
tn.write("admin\n")
tn.read_until("> ")
tn.write(cmd + "\n")
tn.close()
Related
I have some problems with the python libary telnetlib. Login works. Enable, ? and Exit works. But all commands with an space doesn't work.
import getpass
import sys
import telnetlib
HOST = "10.159.123.91"
user = "1234"
command = "show version"
tn = telnetlib.Telnet(HOST)
tn.read_until("Password: ")
tn.write(user + "\n")
tn.write("enable\n")
tn.write("5678\n")
tn.write("?\n")
tn.write("exit\n")
print tn.read_all()
tn.close()
This one is working. If I replace tn.write("?\n") with tn.write("show version\n") nothing happen.
Does anyone have any idea where my problem is?
I found an solution. I'm not sure if it is an good and clean solution. But it works. I add behind the command an sleep for 2 seconds.
import getpass
import sys
import time
import telnetlib
HOST = "10.159.123.91"
user = "1234"
command = "show version"
tn = telnetlib.Telnet(HOST)
tn.read_until("Password: ")
tn.write(user + "\n")
tn.write("enable\n")
tn.write("5678\n")
tn.write("show ip interface brief\n")
time.sleep(2)
tn.write("exit\n")
print tn.read_all()
tn.close()
Python script is designed to run with elevated credentials, unfortunately
it still prompts me for password
when I enter the correct password it doesn't work
Here is script1, which calls script2 with elevated credentials
import os
import sys, subprocess, socket, string
import wmi, win32api, win32con
import win32com.shell.shell as sh
ASADMIN = '/user:DOMAIN\username'
os.system('"runas /user:DOMAIN\username "D:/Python27/python.exe script2.py sender-ip=10.10.10.10 < password.txt""')
sys.exit(0)
if sys.argv[-1] != ASADMIN:
script = os.path.abspath(sys.argv[0])
params = ''.join([ASADMIN] + ['D:\Python27\python.exe',script] + sys.argv[1:])
sh.ShellExecuteEx(lpVerb='runas',lpFile=sys.executable,lpParameters=params)
sys.exit(0)
Here is script2
import sys, subprocess, socket, string
import wmi, win32api, win32con
for args in [item.strip('sender-ip=') for item in sys.argv[1:]]:
userIP = args
userloggedon = ""
# perform system lookup of IP address
userIP = "\\\\" + userIP
pst = subprocess.Popen(
["D:\pstools\psloggedon.exe", "-l", "-x", userIP],
stdout = subprocess.PIPE,
stderr = subprocess.PIPE
)
out, error = pst.communicate()
userLoggedOn = out.split('\n')[1].strip()
print 'userId={}'.format(userLoggedOn)
f = open('D:\SymantecDLP\Protect\plugins\output.txt', 'w')
f.write('userId={}'.format(userLoggedOn))
output.txt is not created
Any ideas?
EDIT
I also read this thread, How to supply password to runas command when executing it from java
but no matter what I try I keep getting the error
Attempting to start c:\test.bat as user "DOMAIN\username" ...
RUNAS ERROR: Unable to run - c:\test.bat
1326: Logon failure: unknown user name or bad password.
Let's talk about your problems one at the time.
1. It still prompts me for password
In the line
os.system('"runas /user:DOMAIN\username "D:/Python27/python.exe script2.py sender-ip=10.10.10.10 < password.txt""')
you're providing the password to script2. runas command still need a password since is trying to run a program as another user.
2. When I enter the correct password it doesn't work
Well ... The code does'n work that's clear. But, you have to be more specific when asking a question. Right now a look to your code and I can see that you're trying to do ping on a remote machine.
Might the remote machine has a firewall?
Have you tryed doing ping manually?
Edit: The output.txt file is not created, and running the script don't tell you nothing about error writting the file, obviously your code is hitting one of the sys.exit() lines.
You can use PsExec
https://learn.microsoft.com/en-us/sysinternals/downloads/psexec
You can supply a username and password and executing does not need to be elevated to admin:
psexec [\computer[,computer2[,...] | #file]]\ [-u user [-p psswd] [-n s][-r servicename][-h][-l][-s|-e][-x][-i [session]][-c [-f|-v]][-w directory][-d][-][-a n,n,...] cmd [arguments]
Use the -e switch to give the same results as Runas /netonly:
-e Does not load the specified account’s profile.
I'm trying to use a PHP file on a server to transmit some variables into a Python script which will in turn start a raspistill timelapse on my Raspberry Pi.
I've so far managed to start taking pictures but I'd now like to have a button to kill the timelapse - i've tried many methods including .kill() and .terminate() but cant get it working.
Here is my current python code:
import sys, os, time, datetime
import subprocess
import signal
from time import sleep
tlfreq = int(sys.argv[1])
tltime = int(sys.argv[2])
dir = '/var/www/timelapse/' + sys.argv[3]
if not os.path.exists(dir):
os.makedirs(dir)
cmd = ('raspistill -t ' + str(tltime) + " -tl " + str(tlfreq) + " -o " + dir + "/photo_%04d.jpg")
pro = subprocess.Popen(cmd, stdout=subprocess.PIPE,
shell=True, preexec_fn=os.setsid)
print "Pictures are now being taken every" , tlfreq/1000 , "second/s for a total of", tltime/3600000 , "hours. These are being stored in", dir
Perhaps I need an "if variable = 1 then kill" command and then send the variable to python.
Any help would be greatly appreciated!
Many thanks,
Dan
You can create new python script kill_raspystill.py with this code
import os
os.system("pkill raspistill")
and call that script when you press a button.
I would suggest the signal library: http://docs.python.org/2/library/signal.html
import getpass
import sys
import telnetlib
tn = telnetlib.Telnet("xxxxxxxx")
tn.write("xxxxxx" + "\n")
tn.write("xxxxxx" + "\n")
tn.write("show version\n")
tn.write("exit\n")
print tn.read_all()
Trying to telnet to a router and the script is hanging.... Not sure whats up. Have tried a debug but cant make head or toe of the output.
telnetlib.py(511): if self.irawq >= len(self.rawq):
telnetlib.py(512): self.rawq = ''
telnetlib.py(513): self.irawq = 0
telnetlib.py(516): buf = self.sock.recv(50)
This is where it hangs in the debug.
Thanks
William
I can answer this one. Managed to get it fixed.
The python script was going off the rails when the router asked for user input to see more information on the command.
So when i asked for a "show version" it would give out a lot of information onto the command line and then ask you hit space to see more.
To solve this the first command i execute is "term len 0". Stops it asking for any user input and just fires it all out at once.
below is the final script. Changed quite a bit but its works.
import sys
import telnetlib
tn = telnetlib.Telnet("xxxxxxxx")
TELNET_PROMPT="xxxxxxxxxx"
TIMEOUT=1
tn.write("xxxxxxx"+"\n")
tn.write("xxxxxxx"+"\n")
print tn.read_until(TELNET_PROMPT, TIMEOUT)
tn.write("term len 0" + "\n")
print tn.read_until(TELNET_PROMPT, TIMEOUT)
tn.write("show version" + "\n")
print tn.read_until(TELNET_PROMPT, TIMEOUT)
tn.write("exit"+"\n")
tn.close()
So I'm trying this really simple example given by the python docs:
import getpass
import sys
import telnetlib
HOST = "<HOST_IP>"
user = raw_input("Enter your remote account: ")
password = getpass.getpass()
tn = telnetlib.Telnet(HOST)
tn.read_until("login: ")
tn.write(user + "\n")
if password:
tn.read_until("Password: ")
tn.write(password + "\n")
tn.write("ls\n")
tn.write("exit\n")
print tn.read_all()
My issue is that it hangs at the end of the read_all()... It doesn't print anything out. I've never used this module before so I'm trying to get this really basic example to work before continuing. BTW, I'm using python 2.4
Thank you.
Okay, I found a solution. Before I entered ls and exit, I needed to first specify the terminal type. Adding
tn.write("vt100\n")
before the "ls" fixed the problem for me.
If you're using Windows, be sure to add carriage return (\r) before the new line character:
tn.write(user.encode('ascii') + "\r\n".encode('ascii'))
I know this is late to post but may help others. I also struggled to get this right but here is my piece of code. My telnet would follow certain flow like it would ask for loginID and then Password and then you have to wait for a particular string to be displayed here,which for my case was "DB>" then you can proceed with the command and all. My output would be saved in "out" varible
import os,re,telnetlib
host = "10.xxx.xxx.xxx"
port = 23
telnet = telnetlib.Telnet()
telnet.open(host, port)
telnet.write('loginID\r\n')
telnet.write('Password\r\n')
out = telnet.read_until("DB>", 5)
telnet.write('show cable modem reg\r\n') #Mycommand
out = telnet.read_until("DB>", 5)
telnet.write('quit\r\n')
telnet.close()
For more variations and help, Check the website nullege
I don't have a telnet server to test against, but I think the issue is that you are not reading server responses up to the prompt, after each command you write.
PROMPT = ':~$'
tn = telnetlib.Telnet(HOST)
tn.read_until('login: ')
tn.write(user + '\n')
if password:
tn.read_until('Password: ')
tn.write(password + '\n')
tn.read_until(PROMPT)
tn.write('ls\n')
print tn.read_until(PROMPT)
tn.write('exit\n')
btw, telnetnetlib can be tricky and things varies depending on your FTP server and environment setup. you might be better off looking into something like pexpect to automate login and user interaction over telnet.
I struggled for a while trying to write to a SynAccess power strip. This is how I did it:
import sys
import telnetlib
HOST = < your SynAccess switch ip address >
user = < user name >
password = < password >
tn = telnetlib.Telnet(HOST, 23, 5)
tn.write("login\r\n")
tn.write(user + "\r\n")
tn.write(password + "\r\n")
tn.write("rb 3\r\n") # this reboots plug 3
tn.write("rb 1\r\n") # this reboots plug 1
tn.write("logout\r\n")
tn.close
use python 2.7 or use a higher version with"(" ,")" at last line
If none of these answers worked, you can also try:
tn.read_until(b"Password: ")
tn.write(password.encode('utf-8') + b"\n")
tn.read_until(b"Terminal type?", 5)
tn.write("vt100\n".encode('utf-8'))
tn.read_until(b">", 5)
Even though I never saw the actual request for the terminal type being printed, this write seemed to work.