Showing posts with label paramiko. Show all posts
Showing posts with label paramiko. Show all posts

Friday, March 3, 2017

Python CGI with paramiko

Leave a Comment

I am trying to write a Python CGI script where user can enter Name(hostname) and Choose Memory from form and then via using paramiko module it will execute free -m command to the given node

import cgi import paramiko   print "Content-type:text/html\r\n\r\n"  print '<html>'  print '<head><title>My First CGI Program</title></head>'  print '<body>'  print '<h1>Hello Program!</h1>'  form = cgi.FieldStorage()  if form.getvalue("name"):  name = form.getvalue("name")  if form.getvalue("memory"):  ssh = paramiko.SSHClient()  ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())  ssh.connect(name, username='testuser', password='test12')     stdin, stdout, stderr=ssh.exec_command("free -m")   for line in stdout.readlines():         print line.strip()  ssh.close()  print '<form method="post" action="final.py">' print '<p>Name: <input type="text" name="name"/></p>' print '<input type="checkbox" name="memory" /> Memory' print '<input type="submit" value="Submit" />' print '</form>' print '</body>' print '</html>' 

It's not throwing error but at the same time it's not giving any output, Not sure What I am doing wrong

1 Answers

Answers 1

form = cgi.FieldStorage() hostname = form.getvalue("name") or None if hostname and form.getvalue("memory"):    ssh = paramiko.SSHClient()     #ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())    #This, of course, is not in the interest of the inventor and is infinitely unsafe,    #so should only be used in tests in secure networks.    #The correct way is to have Paramiko load the host keys,    #so that it can check them as intended like:     client.load_host_keys(os.path.expanduser('~/.ssh/known_hosts'))    client.connect(hostname, username="testuser")    stdin, stdout, stderr = client.exec_command('free -m')    #for test print all std's    for line in stdin, stdout, stderr:        print line.strip('\n')    client.close() 
Read More

Friday, April 8, 2016

Python send control + Q then control + A (special keys)

Leave a Comment

I need to send some special keystrokes and am unsure of how to do it.

I need to send Ctrl + Q followed by Ctrl + A to a terminal (I'm using Paramiko).

i have tried

shell = client.invoke_shell()  shell.send(chr(10)) time.sleep(5) shell.send(chr(13))  shell.send('\x11') shell.send('\x01')  print 'i tried' 

I can see the two returns go in successfully, but then nothing, it doesnt quit the picocom (also to note i have it the wrong way round, its expecting ctrl+a, then ctrl+q)

if it helps this is the device http://www.cisco.com/c/en/us/td/docs/routers/access/interfaces/eesm/software/configuration/guide/4451_config.html#pgfId-1069760

as you can see at step 2

Step 2 Exit the session from the switch, press Ctrl-a and Ctrl-q from your keyboard:  Switch# <type ^a^q> Thanks for using picocom Router# 

2 Answers

Answers 1

Just as assumption: maybe pseudoterminal would help

import paramiko client = paramiko.SSHClient() client.set_missing_host_key_policy(paramiko.AutoAddPolicy()) client.connect(...) channel = сlient.get_transport().open_session() channel.get_pty() channel.settimeout(5) channel.exec_command('\x11\x01')  

Answers 2

Pressing Ctrl + key is actually a "user-friendly" way to enter ASCII control characters. The combination Ctrl + H, for example, is equivalent to entering a backspace. This site lists the ASCII control characters associated with their key combinations, so Ctrl+Q, Ctrl+A is equivalent to sending the string "\x11\x01" through the paramiko Channel:

channel = client.invoke_shell() channel.send('\x11\x01') 
Read More