Okay, can someone tell what I'm doing wrong with this simple request to change the time? I'm on a win 7 machine, trying to change the time on a linux box. I can login, search logs and run other commands, of course adjusting my code below. But this simple command is not changing the date/time. I must be overlooking something?
datetime_string = raw_input("Enter date and time in format 11/1/2011 1600")
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect(iP_address, username='root', password='******')
apath = '/'
apattern = datetime_string
rawcommand = 'date -s' + datetime_string
command1 = rawcommand.format(pattern=apattern)
stdin, stdout, stderr = ssh.exec_command(command1)
dateresult = stdout.read().splitlines()
Try changing this:
rawcommand = 'date -s' + datetime_string
To this:
rawcommand = 'date -s "%s"' % datetime_string
And im not positive, but I dont think rawcommand.format(pattern=apattern) is necessary:
datetime_string = raw_input("Enter date and time in format 11/1/2011 1600")
command1 = 'date -s "%s"' % datetime_string
stdin, stdout, stderr = ssh.exec_command(command1)
dateresult = stdout.read().splitlines()
You should validate user input. Especially if it might be passed unescaped to the shell.
#!/usr/bin/env python
from datetime import datetime
import paramiko
# read new date from stdin
datetime_format = "%m/%d/%Y %H%M"
newdate_string = raw_input("Enter date and time in format 11/1/2011 1600")
# validate that newdate string is in datetime_format
newdate = datetime.strptime(newdate_string, datetime_format)
# print date (change it to `-s` to set the date)
command = "date -d '%s'" % newdate.strftime(datetime_format)
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect("localhost") # use ssh keys to authenticate
# run it
stdin, stdout, stderr = ssh.exec_command(command)
stdin.close()
# get output of the command
print
print "stdout: %r" % (stdout.read(),)
print '*'*79
print "stderr: %r" % (stderr.read(),)
Output
$ echo 1/11/2011 1600 | python set-date.py
Enter date and time in format 11/1/2011 1600
stdout: 'Tue Jan 11 16:00:00 EST 2011\n'
*******************************************************************************
stderr: ''
Related
I am trying to do the port scanner from the Violent Python and I ran into following problem. This post will be similar to this post ("https://stackoverflow.com/questions/17807992/violent-python-port-inputs-not-being-seperated") but it is a different problem. I want to run multiple port like this
python PortScanner.py -H www.google.com -p 21, 22, 80
but it scanned only the initial first port (21) then exited the program So I want to know how can I fix this code to run multiple ports.
Note: It also said that args in (option, args) = parser.parse_args() is not accessible by Pylance so is it concern to it or how can I fix it as well.
import optparse
import socket
from socket import *
def connscan(tgtHost,tgtPorts):
try:
connSkt= socket(AF_INET,SOCK_STREAM)
connSkt.connect((tgtHost, tgtPorts))
connSkt.send('Violent Python\r\n')
results = connSkt.recv(100)
print ('[+]%d/tcp open'% tgtPorts)
print ('[+]' + str(results))
connSkt.close()
except:
print ('[-]%d/tcp closed'% tgtPorts)
def PortScan(tgtHost,tgtPorts):
try:
tgtIP=gethostbyname(tgtHost)
except:
print ("[-] Cannot resolve '%s': Unkown host"%tgtHost)
return
try:
tgtName= gethostbyaddr(tgtIP)
print ("\n[+] Scan Result for: "+ tgtName[0])
except:
print ("\n[+] Scan Result for: " + tgtIP)
setdefaulttimeout(1)
for tgtPort in tgtPorts:
print ("Scanning Port " + tgtPort)
connscan(tgtHost,int(tgtPort))
def main():
parser = optparse.OptionParser('Usage: %prog -H ' +\
'<target host> -p <target port>')
parser.add_option('-H', dest = 'tgtHost', type = 'string', \
help = 'specify target host')
parser.add_option('-p', dest = 'tgtPort', type = 'int', \
help = 'Specify target port' )
(options,args) = parser.parse_args()
tgtHost = options.tgtHost
tgtPorts = str(options.tgtPort).split(',')
if ( tgtHost == None) | (tgtPorts[0] == None):
print(parser.usage)
exit (0)
print(*tgtPorts, sep=", ")
PortScan(tgtHost,tgtPorts)
if __name__ == '__main__':
main()
I managed to solve the problem by changing the type of tgtPort from int to string and use quote as following at the command line python PortScanner.py -H www.google.com -p "21, 22, 80".
i have some question related to telnet in python3. It will occur
TypeError: cannot use a string pattern on a bytes-like object.
Do anyone know how to solve this issue? If use python2 will not have problem.
The code is just telnet to cisco server and print the mac's ipv6 address.
def Telnet_Check_reachability(ip):
ping_count=3
process = subprocess.Popen(['ping', ip, '-n', str(ping_count)],
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT)
process.wait()
stdout = process.stdout.read()
#print stdout
if "TTL=" in stdout:
#print "Server reachable"
successful = 1
else:
#print "Server unreachable"
successful = 0
return successful
def telnet_To_CMTS(Client_IP, Client_Name, Client_Pwd, MAC):
tn =Login_Telnet(Client_IP, Client_Name, Client_Pwd)
if "telnetlib" in str(tn):
time.sleep(1)
value = tn.read_until(b"Router#")
command = "scm " + MAC + " ipv6\n"
tn.write(command.encode('ascii') + b"\n")
#tn.write(command)
value = tn.read_until(b"Router#")
#print value
tn.close()
time.sleep(1)
info = "2001"
#value=str(value)
matchObj = re.match(r'.*'+ info + '(.*)\n',value, re.M|re.DOTALL)
if matchObj:
Ipv6_address = info + matchObj.group(1)
Ipv6 = Ipv6_address.replace("\n", "")
return Ipv6
else:
print ("No match!!")
else:
print ("Telnet failed")
ip ="192.168.1.252"
username = "guest"
password = "guest"
mac = "xxxx.bbbb.cccc"
new_IPv6 = telnet_To_CMTS(ip, username, password, mac)
#print (new_IPv6)
The telnet instance's read_until method returns bytes, not str, so you need to decode value before passing it to the regex:
value_bytes = tn.read_until(b"Router#")
# Decode value_bytes to str. The encoding is *probably* ASCII
value = value_bytes.decode('ascii')
tn.close()
time.sleep(1)
info = "2001"
matchObj = re.match(r'.*'+ info + '(.*)\n',value, re.M|re.DOTALL)
Alternatively, you could leave value as bytes and encode info and the regex pattern as bytes instead, but it's usual to convert bytes to str and vice-versa at the boundaries of your application, unless you are working purely with bytes.
After using value=value.decode('utf8') it can work now.
value = tn.read_until(b"Router#")
#print value
tn.close()
time.sleep(1)
info = "2001"
#value=str(value)
value=value.decode('utf8')
matchObj = re.match(r'.*'+ info + '(.*)\n',value, re.M|re.DOTALL)
hosts=['10.101.x.x'] username='root' password=''
from datetime import datetime date = datetime.now() dates
=date.strftime('%d%b%Y') print dates CheckStr = "Log-" + dates print CheckStr
cmd1='cd /usr/local/element/log/global/log/;ls -ltr' cmd2='/usr/local/element/rel/RAN_RCS_1.0.0.31/bin/linux-x86_pcm64/execCmdCli' cmd3='element add filter rule SPNLOGS enable class SPNAPP severity Informational,Major,Debug,Critical,Minor target log'
def work_comp():
for host in hosts:
print host
ssh = paramiko.SSHClient()
global ssh
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect(host, username=username, password=password, timeout=5.0 )
exe_comm(cmd1)
exe_comm(cmd2)
ssh.close()
# cmd='cd /usr/local/element/log/global/log/;ls -ltr'
# cmd2='pwd'
# #cmd2='/usr/local/element/rel/RAN_RCS_1.0.0.31/bin/linux-x86_pcm64/execCmdCli'
# cmd3='element add filter rule SPNLOGS enable class SPNAPP severity Informational,Major,Debug,Critical,Minor target log'
# #cmd5="date |awk '{print $2,$3,$6}'"
def exe_comm(cmd):
stdin, stdout, stderr = ssh.exec_command(cmd)
for line in stdout:
print (line)
print "Done"
work_comp()
There is a parallel SSH client (uses paramiko) that can do this which is also asynchronous.
from pssh import ParallelSSHClient
client = ParallelSSHClient(['10.101.x.x'], user='root', password='')
cmd1='cd /usr/local/element/log/global/log/;ls -ltr'
cmd2='/usr/local/element/rel/RAN_RCS_1.0.0.31/bin/linux-x86_pcm64/execCmdCli'
cmd3='element add filter rule SPNLOGS enable class SPNAPP severity Informational,Major,Debug,Critical,Minor target log'
for cmd in [cmd1, cmd2, cmd3]:
output = client.run_command(cmd)
for host, host_out in output.items():
for line in host_out.stdout:
print("[%s] - %s" % (host, line,))
print("done")
Im trying to get the model no of a switch from show inventory then set a integer to the no of ports the switch has. ive tried to make the result go onto one line and then search that with regex (the regex works i tested it on http://regexr.com/)
It doesn't look like my function is returning the full inventory, its getting cut off. it should return the below
Switch#sh inventory
NAME: "1", DESCR: "WS-C2960X-24PS-L"
PID: WS-C2960X-24PS-L , VID: V01 , SN: XXXXX
This is the output im getting
Switch#
Switc
Object is: terminal length 0
Switch#sh inventory
NAME:
Inventory is:
Port Count is: 0
and this is the script
#!/usr/bin/env python
import paramiko
import time
import sys
import re
# For debugging only
#paramiko.common.logging.basicConfig(level=paramiko.common.DEBUG)
#
interface_regex = "interface GigabitEthernet[1-5]\/0\/"
def Send_Command_and_Get_Response(command, reponse, result):
# Send the su command
shell.send(command)
# Create a new receive buffer
receive_buffer = ""
while not reponse in receive_buffer:
# Flush the receive buffer
receive_buffer += shell.recv(1024)
# Print the receive buffer, if necessary
if result:
print receive_buffer
return receive_buffer
# VARIABLES THAT NEED CHANGED
ip = '10.X.X.X'
username = 'root'
password = 'XXXX'
port = 3010
# Create instance of SSHClient object
client = paramiko.SSHClient()
# Make sure that we add the remote server's SSH key automatically
client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
# initiate SSH connection
client.connect(ip, username=username, password=password,port=port, look_for_keys=False, allow_agent=False)
print "SSH connection established to %s" % ip
# Use invoke_shell to establish an 'interactive session'
shell = client.invoke_shell()
print "Interactive SSH session established"
time.sleep(1)
shell.send("\r\n")
output = shell.recv(1000)
print output
# Disable more
Send_Command_and_Get_Response("terminal length 0\n", "#", False)
objInv = Send_Command_and_Get_Response("sh inventory\n", "#", False)
strInv =""
strInv.join(objInv.splitlines())
intPort = 0
if (re.match("WS-C.*24", strInv)):
intPort = 24
elif (re.match("WS-C.*48", strInv)):
intPort = 48
print "Object is: " + objInv
print "Inventory is: " + strInv
print "Port Count is: " + str(intPort)
# Close the SSH connection
client.close()
replaced new lines and breaks and used find instead of regex and its fixex!
objInv = Send_Command_and_Get_Response("sh inventory\n", "#", False)
print "Object is: " + objInv
strInv = str(objInv)
strInv = strInv.replace('\n','').replace('\r','')
intPort = 0
if (strInv.find('WS-C') >-1 and strInv.find('-24') >-1):
intPort = 24
if (strInv.find('WS-C') >-1 and strInv.find('-48') >-1):
intPort = 48
I am running Hadoop MapReduce and other SSH commands from a Python script using the paramiko module (code can be seen here). Once the MapReduce job is complete, I run the getmerge step to get the output into a text file.
The problem is, I then have to open a cmd window and run PSCP to download the output.txt file from the HDFS environment to my computer. For example:
pscp xxxx#xx.xx.xx.xx:/nfs_home/appers/cnielsen/MROutput_121815_0.txt C:\Users\cnielsen\Desktop\MR_Test
How can I incorporate this pscp step into my script so that I don't have to open a cmd window to run this after my MapReduce and getmerge tasks are complete? I would like my script to be able to run the MR task, getmerge task, and then automatically save the MR output to my computer.
Here is my code.
I have solved this problem with the following code. The trick was to use the scp module and import SCPClient. See the scp_download(ssh) function below.
When the MapReduce job completes the getmerge command is run, followed by the scp_download step.
import paramiko
from scp import SCPClient
import time
# Define connection info
host_ip = 'xx.xx.xx.xx'
user = 'xxxxxxxx'
pw = 'xxxxxxxx'
port = 22
# Paths
input_loc = '/nfs_home/appers/extracts/*/*.xml'
output_loc = '/user/lcmsprod/output/cnielsen/'
python_path = "/usr/lib/python_2.7.3/bin/python"
hdfs_home = '/nfs_home/appers/cnielsen/'
output_log = r'C:\Users\cnielsen\Desktop\MR_Test\MRtest011316_0.txt'
# File names
xml_lookup_file = 'product_lookups.xml'
mapper = 'Mapper.py'
reducer = 'Reducer.py'
helper_script = 'Process.py'
product_name = 'test1'
output_ref = 'test65'
target_file = 'test_011416_3.txt'
# ----------------------------------------------------
def createSSHClient(host_ip, port, user, pw):
client = paramiko.SSHClient()
client.load_system_host_keys()
client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
client.connect(host_ip, port, user, pw)
return client
# ----------------------------------------------------
def buildMRcommand(product_name):
space = " "
mr_command_list = [ 'hadoop', 'jar', '/share/hadoop/tools/lib/hadoop-streaming.jar',
'-files', hdfs_home+xml_lookup_file,
'-file', hdfs_home+mapper,
'-file', hdfs_home+reducer,
'-mapper', "'"+python_path, mapper, product_name+"'",
'-file', hdfs_home+helper_script,
'-reducer', "'"+python_path, reducer+"'",
'-input', input_loc,
'-output', output_loc+output_ref]
MR_command = space.join(mr_command_list)
print MR_command
return MR_command
# ----------------------------------------------------
def unbuffered_lines(f):
line_buf = ""
while not f.channel.exit_status_ready():
line_buf += f.read(1)
if line_buf.endswith('\n'):
yield line_buf
line_buf = ""
# ----------------------------------------------------
def stream_output(stdin, stdout, stderr):
writer = open(output_log, 'w')
# Using line_buffer function
for l in unbuffered_lines(stderr):
e = '[stderr] ' + l
print '[stderr] ' + l.strip('\n')
writer.write(e)
# gives full listing..
for line in stdout:
r = '[stdout]' + line
print '[stdout]' + line.strip('\n')
writer.write(r)
writer.close()
# ----------------------------------------------------
def run_MapReduce(ssh):
stdin, stdout, stderr = ssh.exec_command(buildMRcommand(product_name))
stream_output(stdin, stdout, stderr)
return 1
# ----------------------------------------------------
def run_list_dir(ssh):
list_dir = "ls "+hdfs_home+" -l"
stdin, stdout, stderr = ssh.exec_command(list_dir)
stream_output(stdin, stdout, stderr)
# ----------------------------------------------------
def run_getmerge(ssh):
getmerge = "hadoop fs -getmerge "+output_loc+output_ref+" "+hdfs_home+target_file
print getmerge
stdin, stdout, stderr = ssh.exec_command(getmerge)
for line in stdout:
print '[stdout]' + line.strip('\n')
time.sleep(1.5)
return 1
# ----------------------------------------------------
def scp_download(ssh):
scp = SCPClient(ssh.get_transport())
print "Fetching SCP data.."
scp.get(hdfs_home+target_file, local_dir)
print "File download complete."
# ----------------------------------------------------
def main():
# Get the ssh connection
global ssh
ssh = createSSHClient(host_ip, port, user, pw)
print "Executing command..."
# Command list
##run_list_dir(ssh)
##run_getmerge(ssh)
##scp_download(ssh)
# Run MapReduce
MR_status = 0
MR_status = run_MapReduce(ssh)
if MR_status == 1:
gs = 0
gs = run_getmerge(ssh)
if gs == 1:
scp_download(ssh)
# Close ssh connection
ssh.close()
if __name__ == '__main__':
main()