Forum Thread: Python Stdout Assistance

Good evening,

After spending far too much time reading/researching over creating anything, Ive recently taken the plunge and started my own .py

The section of the code is as follows:

elif netopt == 4:
cmdopt = rawinput( "Pop a question\nChoice: ")
cmdopt = int(cmdopt)
stdin, stdout, stderr = ssh.exec
command(cmdopt)
for line in stdout.readlines():
print line
ssh.close()

The program works wonders and so far im thoroughly impressed with the progress. However, im coming to a bit of a standstill in regards to certain sections.

This section basically prompts for user input, which then executes the command on a raspberry pi on a different network and prints stdout of the command (ifconfig, dmesg, cat /path/to/file etcetera) one line at a time (solving my ls -l issue where everything was essentially printed to the same line)

I was wondering if anyone could point me in the general direction of any resources which will;

-Use this stdout, save it to a file at location on the raspberry pi server.

-(This could be done through prompt admittedly using '> /path/to' as expected)

However, this section either issues predefined commands or allows user to input their own command, so instead of writing '> /path/to.txt'

For every if/elif statement; Is there a way i can say;

"in stdout from (every)ssh.execcommand: write to file in this directory"

The idea at this point (technically only a few hours in still, and more than likely subject to change) is to

Issue Command to Pi
Save stdout to a file in date/time.txt format (to try time module)
Watch a directory, then ftp any new .txt file to alternate server.

I essentially wish to ssh into a box, write a file with the results of the command, such as an administrator using 'cat ~/path/to/log' , creating a new document with the echod response in a directory.

Then I can go back a page or two calling something (unsure as to what and how to do it, see further down) and run a section of code which with check new files in specified directors, to then sftp them to a different server, where i can view said log at my own pleasure.

Ive never really programmed, in depth anyway, and this is turning out to be a nice little project.

Final thing, if anyone could explain;

  • if _name_ == _main_
  • Looping classes/objects/something as to not write out if statements 100 times if you wish to revisit the same sub menu with the same options. Essentially looping an application, but def class() then calling class() after eg: rolling the mac address, to then i want to go back to main menu

It seems to leave the shell blank when it comes to that sections, essentially like a 'clear' command has been issued.

I would greatly appreciate the information. These two have been troubling me for some time now, and ive searched high and low (or at least attempted to)

Much Appreciated
Spawn

Be the First to Respond

Share Your Thoughts

  • Hot
  • Active