How do I send several commands to the same cmd instance in perl? - windows

My problem is that I need to build a script that will send commands to the single win32 CMD instance.
The "system #cmd" or "`` #cmd" won't help here, since they always create a new instance.
It should look like this in pseudo code:
my $consoleWindow = CreateConsole;
my $cmdargs1 = "foo";
my $cmdargs2 = "bar";
Send($cmdargs1, $consoleWindow);
sleep(1);
Send($cmdargs2, $consoleWindow); #send to the same instance
I believe, that I would have to create new cmd window, hook to it and then send my commands to it. How can I achieve this?

Related

How to run bash script on wake mac os x

Want to change Wi-Fi settings on wake (osx).
I have script for that but don't want to run it manually every time.
Is there any way to run it on wake?
This functionality of running script after waking up can be achieved with the powerful hammerspoon:
function printf(s,...) print(s:format(...)) end
wather = hs.caffeinate.watcher.new(function(eventType)
-- screensDidWake, systemDidWake, screensDidUnlock
if eventType == hs.caffeinate.watcher.systemDidWake then
local output = hs.execute("/bin/echo -n hello", false)
hs.notify.new({title="TestTitle", informativeText=output}):send()
printf("%s, world", output)
end
end)
wather:start()
Put those script to $HOME/.hammerspoon/init.lua and reload hammerspoon, and you can check the above /bin/echo output in the hammerspoon console.
There are several wake up events, including screensDidWake, systemDidWake, screensDidUnlock. See wather api doc for details.
Maybe this will help you out:
Running script upon login mac [closed]

Writing Automated scripts to configure device

My requirement is like this:
I need to log in to a remote device (say Router/switch) and execute following commands.
telnet xx.xx.xx.xx
//give password here
sys
interface g x/x/x
shut
desc free-port
exit
There are Hundreds of devices for which I cannot waste time doing above damn thing 100 times. I need to write a automated script which does it. so My questions are as follows:
I use Windows system, so What is the best scripting language to be used : Ruby / shell script / perl ? (I was formerly ROR Developer, so i know Ruby, Linux terminal. Now I am working in networking domain. )
What I thought was : Put all Devices into an array and using for loop, call devices one by one and execute above said commands.
I don't have knowledge of scripting, so please guide me further. I don't know where to start from.
Step 1: decide the file structure of your program.
For example, this is the simplest structure
if_admin/
|--config.yml
|--run.rb
Step 2: write a config file or a bunch of config files that contain the different parts of the commands you need to run on the targets.
For example, you can use a yaml file like this:
xx.xx.xx.xx:
password: s3cret
router-shelf: x
slot: x
port: x
yy.yy.yy.yy:
...
Step 3: implement what you want to do
require 'yaml'
require 'net/telnet'
config = YAML.load_file('./config.yml')
config.each do |host, conf|
telnet = Net::Telnet.new('Host' => host)
telnet.login(conf['password'])
telnet.puts <<-CMD
sys
interface g #{conf['router-shelf']}/#{conf['slot']}/#{conf['port']}
shut
desc free-port
CMD
telnet.close
end
If you can use expect script , you are in luck.
#!/usr/bin/expect
set timeout 60
set cmds [list "ssh host1 ..." "ssh host2 ..." "ssh host3 ..."]
foreach cmd $cmds {
spawn -noecho bash -c $cmd
expect {
-re "password" {
exp_send "$env(PASS_WORD)\"
exp_continue
}
eof { wait } ; # at this time the last spawn'ed process has exited
}
}
Here is the rough idea of above script :-
set cmds [list.... will be used as list to store set of commands.
foreach will iterate though those commands
spawn will spawn process for each of the command. you can write multiple command with single telnet in bash, just break down commands using \ (backslash) so it is easily readable and extendable.
expect block will pass password whenever it encounter certain regex.
eof will wait till all commands in spawn process are finish.
set timeout -1 will keep loop running. i think default time for expect script is 10secs.
You can create one more foreach loop for host-list.
I think this will be enough to get you started for your automation process.
As to the question of "What is the best scripting language to be used", I would say go with one that does what you need and one that you're comfortable with using.
If you want to go with Perl, one module that you could use is Net::Telnet. Of course, you'll need Perl itself. I'd recommend using Strawberry Perl, which should already have Net::Telnet installed.
Another possible route is to use putty, which is a SSH and telnet client. You could combine that with TTY Plus, which provides an interface that uses tabs for different putty sessions. And it lets you issue commands to multiple putty sessions. This is one possibility that wouldn't involve a lot of code writing.

how can i prevent a script from running while I'm launching the server?

I have a script that queries a third party API when the script is called from within my sinatra application, but I do not want it to run when I am launching my Thin server. Is there a way to prevent my code from running until I call it directly? for instance, can I run a check to see if my server is running from within ruby/sinatra to prevent my code from running?
# the following code is inside a class and gets initialized when my server launch script is executed
authToken = ENV['EVERNOTE_AUTH_TOKEN']
evernoteHost = "www.evernote.com"
userStoreUrl = "https://#{evernoteHost}/edam/user"
userStoreTransport = Thrift::HTTPClientTransport.new(userStoreUrl)
userStoreProtocol = Thrift::BinaryProtocol.new(userStoreTransport)
userStore = Evernote::EDAM::UserStore::UserStore::Client.new(userStoreProtocol)
# this line of code executes on server start
noteStoreUrl = userStore.getNoteStoreUrl(authToken)
It appears that ruby does a syntax check when running each file, and my API is being called against my will on the last line, i want to write an if statement that makes it un-callable unless my server is already running

Exporting data from expect script

so i've written a short expect script which logs into a APC Power Distribution Unit interface via telnet and polls the current ampage.
#!/usr/bin/expect
set ip "192.168.0.1"
set username "myusername"
set password "mypassword"
spawn "/bin/bash"
send "telnet $ip\r"
expect "*User Name*"
send "$username\r"
expect "*Password*"
send "$password\r"
expect "*APC*"
send -- "phReading all current\r"
expect "*Success*"
send "quit\r"
expect eof
The script does its job and I see the amps on screen, displayed like this:
apc>phReading all current
E000: Success
1: 7.5 A
apc>quit
What i need to do is 'export' that 7.5 figure, either to a text file or pass it to a bash script as a variable.
Any ideas on how i can do this?
Thank you!
Expect is an extension of TCL, so you have access to all of TCL's constructs.
If I were you I would have the expect script write directly to a file.
See the section "Writing a file" here: http://wiki.tcl.tk/367. It has a simple example for just that. In your case, you will want to open the file for append (a) instead of write (w).
open command documentation at: http://www.tcl.tk/man/tcl8.6/TclCmd/open.htm
Let me know how that works for you.

how to use expect in shell

Here is my shell script:
cd /home/debian/goagent
python ./server/uploader.zip
expect -c "
set timeout 5
expect {
timeout exit
\"APPID:\"
}
send \"haha\r\"
Why my script did not run?
The "haha" (my APPID) is not be input into the APPID, why?
if you download goagent from https://code.google.com/p/goagent/
you can see how to use goagent.
i want to input something automatically to run my script.
people use goagent to see more thing ,i just want to make my script run automatically

Resources