IP address and file manipulation python 2.6 - python-2.6

I am new in python and I need your help to improve part of my script to be more sophisticate.
==================================== a.py
devices = ['x.x.x.x', 'x.x.x.x', 'x.x.x.x']
============================= MAIN CODE
from a import devices
for ip in devices:
ssh connection
value = +1;
if value == 1:
open a specific file
run the commands from this file.
elif value == 2:
open a specific file
run the commands from this file.
elif value == 3:
open a specific file
run the commands from this file.
=============================================
Issue: For each IP address that it stored in a.py file, I need to run specific pack of commands. With the IF statement it is working fine, but if I add more IP address on a.py file need to append my code.
Thank you all.

I hope I'm interpreting your question correctly. Do correct me if I'm not.
You currently have a list with 3 ip addresses, and you handle them with three if-statements.
The problem is, that if you add more entries to your list, you will manually have to add more if-statements to your code?
If this assumption of your question is correct, then indeed. There is a better way of doing this:
devices = ['x.x.x.x', 'x.x.x.x', 'x.x.x.x']
for dev in devices:
do_something(dev)
This will loop over all devices in your list, and execute the same code on each device.

Related

Creating a production ready binary from Julia code

I have a Julia program that inputs a csv and transforms the data via a bunch of functions, and outputs a csv file. I want to turn this into a binary so that I can run on different machines without having the source code on different machines.
I am looking at PackageCompiler.jl, but I can't find any understandable documentation for creating a binary app. I am trying:
using PackageCompiler
#time create_app("JuliaPrograms", "test"; precompile_execution_file="script.jl")
The file that contains all my code is script.jl and it lives in the dir JuliaPrograms, and I want the compiled binary to be named test.
When I run julia script.jl it performs as I want. I want to be able to run ./test with the same result.
However, I get this error:
ERROR: could not find project at "/Users/userx/JuliaPrograms/"
What am I doing wrong? Do I need some special project directory?
Per the docs here: https://julialang.github.io/PackageCompiler.jl/dev/apps.html#Creating-an-app-1 you need to make sure you define:
function julia_main()::Cint
# do something based on ARGS?
return 0 # if things finished successfully
end
a function called julia_main as the entry point to the app. You can find an example app here: https://github.com/JuliaLang/PackageCompiler.jl/tree/master/examples/MyApp
You may also want to check the location of the code itself. Is it being saved at "/Users/userx/JuliaPrograms/"? You can switch your directory in the Julia Reply by typing ; which will enter you into shell mode and then you can cd into the directory where your code is.

Bash script to run through multiple log files and extract client IP from which URL was accessed

I want to create a bash script, that will run through multiple log files and print out client IP addresses, from which certain URL was accessed (for example https://test123.com). Every log file has content like:
"client_ip":"192.168.100.100"
I know that I need a loop, and I found way to extract any IP address using grep:
grep -o '[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}' $log1.txt
My question is how I can extract exactly this IP, which comes after "client_ip", and from which certain URL was accessed?
Also, string "client_ip":"192.168.100.100" is in the same line as requested URL. So, I need to extract line at the begin where my URL shows up and then extract from that line IP address, which comes after "client_ip".
I am not so familiar with bash.
Could anyone help with that?

Programming a "Not - Gate" situation into a batch file?

Brand new to coding of any form. Simply put: I have 2 batch files written. 1 loads one configuration into a program (MultiMonitor), the other loads a 2nd configuration. Id like a 3rd file to act as a "light switch" Or a "NOT-Gate." Effectively, if last time it was ran, it launched the bat controlling config 1, this time it should load config 2.
Currently I just launch the appropriate .bat; but id like to simplify this so I only have one file to launch and then it chooses the one not chosen last time.
This is easy:
In option1.bat (in addition to the real work):
echo call option2 > autoselect.bat
In option2.bat
echo call option1 > autoselect.bat
There are a few ways you could do this:
Use a permanent environmental variable using setx - NOT RECOMMENDED
Test which config is currently being used by your program - I am unfamiliar with your program so I do not know if this is even possible.
Use a temporary file
This example shows how to do this with a temp file:
if not exist file.ext (
rem load config #1
echo.>file.ext
) else (
rem load config #2
del file.ext
)
The file can be named whatever you want.

Reinstalling packages from a list generated by command: ado dir

I am recovering Stata following a Windows upgrade. I have a list of my packages generated from ado dir in the following format:
[1] package mdesc from http://fmwww.bc.edu/RePEc/bocode/m
'MDESC': module to tabulate prevalence of missing values
[2] package univar from http://fmwww.bc.edu/RePEc/bocode/u
'UNIVAR': module to generate univariate summary with box-and-whiskers plot
[3] package tabmiss from http://www.ats.ucla.edu/stat/stata/ado/analysis
tabmiss. Shows tabulation of number of missing and non-missing values
I have many packages and would like to reinstall them without having to designate each directory/url via net cd. While using net cd along with net install or ssc install along with package names in a loop is trivial (as below), it would seem that an automated method for this task might be available.
net cd http://www.ats.ucla.edu/stat/stata/ado/analysis
local ucla tabmiss csgof powerlog ldfbeta
foreach x of local ucla {
net install `x'
}
To my knowledge, there is no built-in or automated method of tracking and managing your installed packages outside of what is available through ado or net.
I would also tend to agree with #Nick Cox that this task seems strange and I can't imagine how a new Stata install or reinstall could know what was installed previously, but I find the question interesting for other reasons.
The main reason being for users who have Stata installed on multiple machines who need the same packages on both machines. I faced a similar issue when I purchased a new computer and installed Stata but wanted all of the packages I use to be available as well. Outside of moving the ado directory or selected contents I'm not aware of any quick solution.
Here it would be possible to use the output of ado dir on one machine to determine what you need to install on a second machine with a new Stata install.
The method you propose using a foreach loop could save you time from having to type in or copy/paste a lot of packages and URLs. At the same time however, this is only beneficial if you have many packages from only a few repositories because you will need to net cd to the URL each time as you show in your example.
An alternative solution is the programmatic solution. As you know, ado dir will list each installed package, the URL and a short description of the package. Using this, a log file, and the built in I/O functionality, a short program could be written to automate the process and dynamically build a do file that contains the commands to install the already installed packages.
The code below generates a do file containing commands (in this case, net describe package, from(url)) for each package I have installed on my computer.
clear *
tempfile log1
log using "`log1'", text name(mylog)
ado dir
log close mylog
tempname logfile
file open `logfile' using "`log1'", read
file read `logfile' line
file open dfh using "path/to/your/dofile.do", write replace
local pckage "package"
while r(eof) == 0 {
if `: list pckage in line' {
local packageName : word 3 of `line'
local dirName : word 5 of `line'
di "`packageName' `dirName'"
file write dfh "net describe `packageName', from(`dirName')"
file write dfh _newline
}
file read `logfile' line
}
file close `logfile'
file close dfh
In the above code, I create a temp file to write a .txt log file to and store the contents of ado dir in that file.
Then, I open the log file using file open and read it line by line in the while loop.
Above the loop, I'm creating a do file at /path/to/your/dofile.do to hold the output of the loop - the dynamically created commands relating to the installed packages on my machine.
The loop will iterate so long as r(eof) = 0, where r(eof) is an end of file marker. I use an if statement to sort out lines of the log file which contain the word package, as I'm only interested in those lines with the package name and URL in them.
Inside of the if block, I parse the local macro line to pull the package name and the URL/directory name.
this is important: this section of code assumes that the 3rd and 5th words in the macro will always be the package name and URL respectively - Confirm this from the output of ado dir before executing.
You will also need to change the command that is being written to the file handle dfh inside of the loop to what you want (net install, etc) when you are ready to execute.
For more help on using file, locals, and tempfiles execute any of the following in Stata:
help file
help extended_fcn
help macrolists
There may be nicer ways to parse the contents of ado dir but this has worked for me. And of course I'd always advise that you take the time to understand what the code is doing so that you can make any necessary tweaks to fit your particular situation.

I need to create a logon script that adds two registry keys with the following values (P+IPAddress) & (S+IPAddress)

I need to create a logon script that lists the following values for each PC
(P+IPAddress) & (S+IPAddress), I managed to create the following .reg file:
HKEY_CURRENT_USER\Serial Numbers
"Printer"="P236"
"Scanner"="S236"
But I have entered these values my self, I need a script that creates a combined value (Letter + The last 3digits of the IP Address of the PC where this script runs).
I highly appreciate it if someone can help me on this.
If you want a single command, use Powershell like this:
powershell -Command "& {[system.net.dns]::gethostaddresses([system.net.dns]::gethostname()) | where { $_.addressfamily -eq 'InterNetwork' } | foreach { [microsoft.win32.registry]::setvalue('hkey_current_user\serial numbers', 'Printer', [string]::concat('P', $_.tostring().split('.')[3])) } }"
Not the most efficient and may have problems with multiple addresses (as it will always set to the last one) but it can be executed as a single command.
Should be easy enough to modify that command to add the Scanner bit too.

Resources