How to access putty using Django - django

I am new to Django and i was wondering if i can do some backend testing using Django. I have already installed Django in windows. My work usually deals with putty(e.g. opening files, checking some data in a file, but this all is done in a putty box). So i was hoping if somehow i can take all the work from backend to front end by accessing putty from django(e.g creating a link to start a putty session, logging in and performing some operations). Please help me in providing the information if this can be done and if its possible, how to do it.
Thanks in advance,

For a start, you need to learn the difference between putty (a Windows application that emulates a remote terminal) and ssh, a protocol for logging into remote systems. So, if you want to do something remotely via Python, you should be thinking about how to start an SSH session, rather than how to automate putty particularly. You might, for example, look into the paramiko library, which does just that.
Secondly, as the other answerers have mentioned, this seems an odd requirement. Web frameworks are not ideally suited to doing things like this. If you just want to automate some remote operations, perhaps fabric is what you need.

Since putty is a different application you need to spawn a subprocess and communicate with the application through stdin and stdout. This can be done using the subprocess module.
But please remenber, that Django is a web framework. This means it is for creating web application. Your problem looks more like a general "I want to automate something" problem where you don't need a web framework - you can simply use pure python. Is there any reason why you are using Django?

Related

Run Linux command remotely from Window based application

I want to run Linux command remotely from Window based Qt C++ application programmatically. What is the simpliest way to do it?
You need some sort of server on the Linux machine and your Windows machine will be a client. I'd say the easiest way would be just make a php script to run your command and drop it in your www root and have your Windows machine fetch that URL.
At the end of the day, without knowing what your requirements are with regard to security and with regard to what kind of commands you'll be running is, it's very difficult to give a definitive answer to this question.
Simply connect to telnet server on the linux using sockets, and send the commands.
This actually requires very little code. Check the Java version here:
Sending telnet commands and reading the response with Java
You can do similarly with Qt/C++ as well.
Simple server-side program witch will handle the requests and then using ex system() function will be this "remotely" part of solution.
And on client-side simple text field handled by function witch will be able to connect to server to send command run request.
The most important thing in this solution will be to take care about security.
One way to do it is, to have a client-server model, the server resides in linux and client can be your computer. That way you can send commands to the server and have its output thrown at you. That's one way I think of this problem.
Use UPnP to get past the firewall(or use NAT traversal or UDP/TCP hole punching). Otherwise (without forwarding the port) it would be impossible to reach the server.
The second is to write your own RSH and SSH utility. (or use putty or other pre-existing software)
You could use Plink if you are on Windows whatever version. If you can run PuTTY, then you can run PLink. PuTTY Plink Documentation. Using that you could use the executable, and automate things. Otherwise, if you're looking for a specific programming language, they'd still be dependent upon some SSH Library. If you're writing your own installer, you could include the PLink.exe in your installer, and distribute it with your application.
From the documentation page:
Z:\sysosd>plink login.example.com 'echo "Hello World"'

Web interface for a C++ program

I am developing a project for the end of my studies. This project is basically acting as a server, is cross-platform and developed in C++.
I was wondering if it was possible to make a web interface that could be used like for instance the listener design pattern to log what the program does. This would be cross platform, and is ideal since the program is supposed to run on a distant server.
My question is: is there any web technology to could let me update my web page live when the program logs something. I know this is something unusual and I'm not an expert in web technos, that's why I am asking.
Would Erlang do it ?
Thanks for your help
EDIT: To give a more concrete example, I would like to be able to follow the execution of my program live and see the logs of my program appear on the page. The idea would be to use a web page like I would use WPF on Windows or GTK on Linux for instance. Like someone said, it would be some kind on monitor for my application.
It's much easier than you might think. A web server basically gets a request as a path name, and returns a page. If you set it up correctly, it will invoke a program to create the content. This is called "CGI".
If you can do it without live updating, it's then super-easy: just refresh the page and your program can be called again.
If you want live updating, you'll need to do a little more. The easiest way is with a little lightweight javascript. The magic word here is AJAX. There are a number of tutorials on line for both of these, just google.
The main thing is to start with a very very simple example and add to it. Javascript in particular is a little peculiar; follow the tutorials, though, and you'll get it.
You can embed a web server such as http://code.google.com/p/mongoose and poll it using xhr or better use websockets.
Or use a monitoring solution such as Nagios (Nagios Core is free).

C++ code on server, running on client machine

Is it possible to write C++ code to interface with a server, but to be executed on client side, but on the browser instead of native?
Like, for example, imagine using open source classes so that you produce a file.
But because you don't want all this work to be done on the server, you run it on the browser.
So that the client gives a file or two or more as inputs, then the code runs on his machine, the final result is produced, then this file is uploaded to database on the server.
please see google native client project. http://code.google.com/p/nativeclient/
This is strange question.
You can prepare binaries that do task that you want done on client side and make server send proper binary to client when asked for it. Client then runs this binary and returns results to server.
It is possible if you know configurations of client machines (binaries must work on them). Also it have to be some security layer implemented - you don't want to allow every binary run on client (imagine man-in-the-middle attack when some malicious code is run on client).
I think your request contradicts with the idea behind server-side programming. The main purpose in using server-side programs is to make use of infrastructural components like database, network, etc. in a controlled manner. (The most typical usage of server-side applications are web sites with server side coding like JSP and ASP.)
Since servers are machines that are to be kept secure, a remote application should not be permitted to make changes or access filesystem freely. If you want to do changes on a server like doing database operations or reading/writing files, you should use applications that run on the server or provide interfaces like web services or web sites to remote client applications.
So there are a couple solutions when if you want to do work on the browser, then have the results posted in a server database.
First of all, you must set up your server ready for database work. I have done this using the MEAN stack, set up a MongoDB and interfaced it with the Mongoose API.
Now, for the meat of the question, there are many examples of browsers doing intensive work. The majority of these applications thought is not C++, but it is Javascript.
If you really want to focus on C++ (like i did in the past, in the time i asked this question, wanting to make something big for college), then you could do one of the following:
*Use Google Native Client (NaCl). This is a sandbox for running compiled C and C++ code in the browser efficiently and securely, independent of the user’s operating system.
*Maybe you should want to check out Emscripten, which is a framwork for translating C and C++ code to jaascript. This way, you can have your C or C++ binaries that worked, and have them translated to Javascript, in order to have them work in the browser too.

Realtime server push with Socket IO (or Strophe.js), XMPP and Django

I have a couple of Android and iOS native mobile application that I wrote which connect directly to an XMPP server that I host. They push and pull realtime data through XMPP. I also use some of the XMPP XEP extensions. For other operations, I have a django application running on the same server which all the mobile applications consume through an HTTP REST interface. I use Celery and Redis for the django side to do some operations asynchronously (like doing heavy batched writes to my db).
This all works fine and dandy. Yay.
But now I want to write a web front-end to all of this, so I started researching my options and well - there are so many ways to skin the cat that I wanted to check with the SO community first.
The idea to have a js library that gives me a unified API for socket communications (i.e try different implementations of web sockets or fall back to flash) appeals to me hence why I mention Socket IO. The idea of having to run a nodejs server, well, not so much (one more thing to learn), but if I have to, I definitely will. I know that some people use gevent as a replacement of the node server. Others, decide to write a small nodejs which they connect to the rest of their stack. I would probably do this.
Another option, is to use an js XMPP library like Strophe which I don't think has a flash fallback. Also, I would need to research what this means for my server.
I have read several of the Stackoverflow answer on how to do comet and django - hence why it seems that there are several options.
The question is:
If I want to have the advantage of Socket IO behavior (with the fallbacks) and I want to push realtime data to the web client (which is being fed to the server through XMPP), and use Django what is my best option?
Update: The XMPP server that I use is ejabberd, which also supports BOSH. I realize that I could use Strophe.js and thus my communication would go over a type of long polling http connection instead of websockets. As far as I can tell, there are some XMPP over Websockets open source library, but AFAIK the community is not as active as the SocketIO one.
Update 2: Browsers that I need to support are only modern browsers. I guess this means that Flash fallback will not be that important, which is leaning me towards strophe.js.
I think once you get your hands dirty with some node you'll find that straying from Node for socket.io is going to be much harder. There are very easy to use xmpp modules in node ready to go (see https://github.com/astro/node-xmpp). Remember, node is all javascript, so you're probably familiar with programming in it already.
Personally, I've had some memory leak issues using node 0.6 or higher. Node 0.4 worked without those issues. If you are new to github (as I was before playing with Node) here is how you would get going with a node server.
Getting Node
Login to your linux box and favorite directory (I'll assume /)
git clone https://github.com/joyent/node.git
cd /node
git tag -l (this will list all available version of node)
git checkout v0.6.16 (this will checkout 0.6.16 version of node, you could replace that with v0.4.12 for example if you have memory issues)
./configure
make
make install
You'll need certain development tools to build it such as g++, but at this point you'll have a working node command.
Installing Node Modules like xmpp
Node has a nice amount of modules where most things have already been written for you. There is a search facility at http://search.npmjs.org or you can access all modules directly from your shell by using the npm command. NPM is nodes tool for installing and managing node modules. You can type npm search xmpp to search for all xmpp modules, for instance. To install a basic xmpp library for node you would do npm install node-xmpp. By the way, most github node module pages will include instructions on the front page readme file.
Keeping Node Running in Production
This threw me when I first started out. If you have any errors that are not caught node will simply die. So, you can either
1. Make sure there are no errors whatsoever or they are all caught (unlikely because even Node itself will error)
2. Use the uncaughtException handler to catch these problems. You would use code like this in your program
process.addListener("uncaughtException", function (err) {
util.log("Uncaught exception: " + err);
console.log(err.stack);
console.log(typeof(this));
// maybe email me?
});
Be Extra Safe and Use Forever
Even with the uncaughtException issue your program in production might die. Memory running out, segfaults, who knows what. That's where it pays to use something like the wonderful Node module called "Forever" (see https://github.com/nodejitsu/forever). You can type npm install forever -g to install forever. Note the -g option which puts forever in the GLOBAL node module directory. Without -g it puts the node module in the current working directory. You'll then be able to type something like (assuming your node program was called my_program.js) forever start my_program.js and then the Forever program will make sure that if it dies it gets restarted.
Not sure why you'd need Flash fallback if you're going to do BOSH (XEP-0124, XEP-0206), which is what strophe.js does. If you don't need to support IE7, you can do CORS from strophe.js, and you don't even need a proxy for same-origin. IE6 will work because it's insecure, and IE8+ support a just-barely-working form of CORS.
To get information from django through XMPP to your client, make a component connection (XEP-0114) to your server using your favorite Python XMPP library, such as SleekXMPP from your Django app. Arrange for that connection to be relatively long-lived, for performance (i.e. don't create a new one for each client connection). Send protocol as needed.
You didn't mention what XMPP server you're using. XMPP servers that don't support BOSH are getting rare, but if you've got one, you might need Punjab as a BOSH-to-XMPP proxy, or you might want to switch to a newer server, such as Prosody.
First of all, full disclosure: I work for a company called PubNub, which I'm going to mention shortly.
There are a whole range of hosted bidirectional messaging services (sometimes called IaaS - Infrastructure as a Service) that I think are worth considering. They are Pusher, Firebase, Flotype, PubNub, and others. I'm reasonably confident you could use any of them for what you want to accomplish. Firebase has a built-in database that ties right into their service, which is a pretty cool feature, but probably not useful for your particular use case (I assume you already have a database on your backend).
I can't speak too heavily about our competitors, but as far as you wanting a JavaScript library on the frontend that communicates with your Python backend, we (PubNub) provide a very similar api in both languages and that communicate on the same databus in the cloud. So you can send messages with Python and catch them with JavaScript, or vice-versa. We even wrote a PubNub-hosted version of socket.io, which you could use instead of our vanilla JavaScript api, and would still tie into your Django backend in about 10 lines of code.
Finally, the nice thing about using an IaaS (or at least us; again I'm not certain about the others) is that we handle that tricky scaling problem for you. If you reach the point of a million simultaneous users and need to push something to them in real-time, you'll find that's no problem.
We are using real-time push as well with Django and Celery. When I first created the architecture, I also researched my options. Eventually, I decided that I'd rather focus on getting the app just right rather then on fiddling around with devops work. There are several services out there that offer hosted real-time push technology that can be easily integrated with any app.
I chose PubNub and I couldn't be happier. They support socket.io for the client side and have a Python lib I use from Django and Celery workers. They also have SDKs you could use from native mobile apps.
I know, you already have a working setup in place. But I'm betting that the time it will take you to replace your current setup with such a hosted solution would be less than the time it will take you to find a good solution for what you're looking for and implement it. Also keep in mind maintenance costs down the road (esp if you opt for a lib which is not well maintained).
True, you will be paying for the service, but they price is very reasonable and you will be getting a solid service with nice perks like colocation.
I'm not affiliated with that company, just a happy customer. There are other similar services out there.

how to read list of running processes on a remote computer in C++

What can be done to know and list all running processes on a remote computer?
One idea is to have a server listening to our request on the remote machine and the other one is to use ssh.
The problem is i dont know whether there will be such a server running on the remote machine and i cannot use ssh because it needs authentication.
Is there any other way out ?
If you
cannot install a server program on the remote machine
cannot use anything that requires authentication
then you should not be allowed to know the list of all running processes on a machine. That request would be a security nightmare!
You can do something much simpler without (as many) security problems: scan the publicly-available ports for programs that are running. Programs like nmap.org let you know a fair bit of information about the publicly-running programs on machines.
I have done something similar in the past using SNMP. I don't have the specifics in front of me, but something like "snmpwalk -v2 -c public hostname prTable" got me the process table. I recall later configuring SNMP to generate errors when the number of processes didn't meet our specified requirement, like httpd must have at least 1 and less than 50.
I suggest you look at the code for a remote login, rlogin. You could remotely login to an account that has the privileges that you need. Once logged in, you can fetch a list of processes.
This looks like a good application for a script rather than a C or C++ program.