how to escape characters in xdotool - xdotool

I have this script to open a new console, ssh into a server, run a deploy command.
I pass to the script the version of the deploy
xdotool key ctrl+alt+t
sleep 3
xdotool type "ssh myserver"
xdotool key Return
sleep 10
xdotool type "password"
xdotool key Return
xdotool type "sh path-to-script/deploy.sh $1"
xdotool key Return
I have several problems with this and I allready tried to google for a solution without success.
the character / its transformed to an &. when I run the script
copy&pasting in the console it works, but no if I run it as a sh file
the $1 is not evaluated
Can you give me any pointer in making this work. xdotool is not mandatory, I would use whatever it works
note: I can't by pass the ssh with a command becouse the security politics of the company and also don't know how to do it if I can't the settings in myserver

Similar problem here:
http://code.google.com/p/semicomplete/issues/detail?id=13#c29
Try:
xdotool type setxkbmap de
xdotool key Return
xdotool type "sh path-to-script/deploy.sh $1"
xdotool key Return
Maybe it works better if you write a script:
#!/bin/bash
xterm -e "ssh myserver \"sh path-to-script/deploy.sh $1\"; sleep 5"
and invoke it like this:
./theAboveScript.sh &
sleep 10
xdotool type "password"
xdotool key Return

"expect" could be a good solution.
You can start following as a script with your parameter.
#!/usr/bin/expect -f
spawn ssh myserver;
expect "Password:";
send "password\n";
expect "me#myserver:~$";
send "sh path-to-script/deploy.sh $argv\n";
interact

Related

If Statements in xdotool & bash

I need make a small script in bash using xdotool. And I have problem with if statements. How to write correctly this instruction.
if [xdotool click 1]
then
./myScript.sh
fi
% If I make mouseclick I want to execute bashscript
Please help.
#/bin/bash
xdotool click 1
EXIT_CODE=$?
if [ $EXIT_CODE -eq 0 ]; then
./myScript.sh
fi
exit code 0 - means that you program executed successfully
UPD:
xdotool click 1 && ./myScript.sh
There is a feature in xdotool called behave to take an action based on some event like mouse-click But...
The specific mouse-click option is not working (apparently a bug),which is:
xdotool search . behave %# mouse-click exec 'myscript.sh'
Although these commands work with mouse-move / mouse-leave option, which are:
xdotool search . behave %# mouse-move exec 'myscript.sh'
xdotool search . behave %# mouse-leave exec 'myscript.sh'
Another solution would be using echo -e like this:
echo -e "\e[?1000h"
while read -n 12; do myscript.sh; done

create shortcut in cmd prompt using autohotkey

I am using python and virutal environment in windows7. Every time I need to go to the project folder, shift+right click to open command prompt and activate virtualenv.
Instead I can hit win+R them type cmd to open a command prompt.
Then type
C:\cd D:\path\to\project
D:
workon projectEnvironment
Can this be done to create a shortchut like 'work' from autohotkey ??
Your windows-R shortcut would work like this:
Start a cmd window and wait until it is active.
Then begin to send your commands.
#r::
Run, %comspec% /c cmd.exe
SetTitleMatchMode, 2
WinWaitActive, cmd.exe
SendInput, cd D:\projects\folder{enter}
SendInput, D:{enter}
SendInput, workon projectEnvironment{enter}
return
Although I was unable to create shortcut key combination in cmd, I created a shortcut key, i.e. windows button + space
#space::Send cd D:\projects\folder{enter} D:{enter} workon projectEnvironment {enter}
Edit
here's the real solution
::work::cd D:\projects\folder{enter} D:{enter} workon projectEnvironment {enter}
After an hour of researching why my script would write " instead of \ when Sending a path in cmd prompt, I realized that sometimes the cmd prompt will interpret some keys differently for AutoHotkey.
I used to write Send, cd C:\File\Path {Enter} and it returned:
C:"File"Path
You can use ASC codes for / = {ASC 47} and \ = {ASC 92}, which in the end you write:
Send, cd C:{ASC 92}File{ASC 92}Path {Enter}

PuTTY scripting to log onto host

I'm using PuTTY to remotely log onto my school's host. Upon logging in, we are required to do these steps:
enter username
enter password
command "add oracle"
command "sqlplus"
enter username
enter password
I will be logging into this host a lot over the course of this semester and I was hoping to create a script that would eliminate the redundancy of the above steps. Ignoring the obvious security oversights of having my password in the script, how would I achieve this? I have zero experience with scripting, so your feedback is greatly appreciated. Thanks!
Edit: I played around with the command-line options for Putty and I was able to bypass steps 1-2 using:
putty -load "host" -l username -pw password
I've also created a shell file that looks like so:
#!/bin/bash
add oracle10g
sqlplus username password
When I try to add this option to the command-line using the -m option, it looks like PuTTY logs into the host and then immediately exits. Is there a way to keep my session open after running the shell file or am I using the -m option wrongly? Here is a link to a PuTTY guide that I have been following: http://the.earth.li/~sgtatham/putty/0.60/htmldoc/Chapter3.html.
Here is the total command that I am trying to run from the command-line:
putty -load "host" -l username -pw password -m c:\test.sh
Figured this out with the help of a friend. The -m PuTTY option will end your session immediately after it executes the shell file. What I've done instead is I've created a batch script called putty.bat with these contents on my Windows machine:
#echo off
putty -load "host" -l username -pw password
This logs me in remotely to the Linux host. On the host side, I created a shell file called sql with these contents:
#!/bin/tcsh
add oracle10g
sqlplus username password
My host's Linux build used tcsh. Other Linux builds might use bash, so simply replace tcsh with bash and you should be fine.
To summarize, automating these steps are now done in two easy steps:
Double-click putty.bat. This opens PuTTY and logs me into the host.
Run command tcsh sql. This adds the oracle tool to my host, and logs me into the sql database.
I'm not sure why previous answers haven't suggested that the original poster set up a shell profile (bashrc, .tcshrc, etc.) that executed their commands automatically every time they log in on the server side.
The quest that brought me to this page for help was a bit different -- I wanted multiple PuTTY shortcuts for the same host that would execute different startup commands.
I came up with two solutions, both of which worked:
(background) I have a folder with a variety of PuTTY shortcuts, each with the "target" property in the shortcut tab looking something like:
"C:\Program Files (x86)\PuTTY\putty.exe" -load host01
with each load corresponding to a PuTTY profile I'd saved (with different hosts in the "Session" tab). (Mostly they only differ in color schemes -- I like to have each group of related tasks share a color scheme in the terminal window, with critical tasks, like logging in as root on a production system, performed only in distinctly colored windows.)
The folder's Windows properties are set to very clean and stripped down -- it functions as a small console with shortcut icons for each of my frequent remote PuTTY and RDP connections.
(solution 1)
As mentioned in other answers the -m switch is used to configure a script on the Windows side to run, the -t switch is used to stay connected, but I found that it was order-sensitive if I wanted to get it to run without exiting
What I finally got to work after a lot of trial and error was:
(shortcut target field):
"C:\Program Files (x86)\PuTTY\putty.exe" -t -load "SSH Proxy" -m "C:\Users\[me]\Documents\hello-world-bash.txt"
where the file being executed looked like
echo "Hello, World!"
echo ""
export PUTTYVAR=PROXY
/usr/local/bin/bash
(no semicolons needed)
This runs the scripted command (in my case just printing "Hello, world" on the terminal) and sets a variable that my remote session can interact with.
Note for debugging: when you run PuTTY it loads the -m script, if you edit the script you need to re-launch PuTTY instead of just restarting the session.
(solution 2)
This method feels a lot cleaner, as the brains are on the remote Unix side instead of the local Windows side:
From Putty master session (not "edit settings" from existing session) load a saved config and in the SSH tab set remote command to:
export PUTTYVAR=GREEN; bash -l
Then, in my .bashrc, I have a section that performs different actions based on that variable:
case ${PUTTYVAR} in
"")
echo ""
;;
"PROXY")
# this is the session config with all the SSH tunnels defined in it
echo "";
echo "Special window just for holding tunnels open." ;
echo "";
PROMPT_COMMAND='echo -ne "\033]0;Proxy Session #master01\$\007"'
alias temppass="ssh keyholder.example.com makeonetimepassword"
alias | grep temppass
;;
"GREEN")
echo "";
echo "It's not easy being green"
;;
"GRAY")
echo ""
echo "The gray ghost"
;;
*)
echo "";
echo "Unknown PUTTYVAR setting ${PUTTYVAR}"
;;
esac
(solution 3, untried)
It should also be possible to have bash skip my .bashrc and execute a different startup script, by putting this in the PuTTY SSH command field:
bash --rcfile .bashrc_variant -l
When you use the -m option putty does not allocate a tty, it runs the command and quits. If you want to run an interactive script (such as a sql client), you need to tell it to allocate a tty with -t, see 3.8.3.12 -t and -T: control pseudo-terminal allocation. You'll avoid keeping a script on the server, as well as having to invoke it once you're connected.
Here's what I'm using to connect to mysql from a batch file:
#mysql.bat
start putty -t -load "sessionname" -l username -pw password -m c:\mysql.sh
#mysql.sh
mysql -h localhost -u username --password="foo" mydb
https://superuser.com/questions/587629/putty-run-a-remote-command-after-login-keep-the-shell-running
I want to suggest a common solution for those requirements, maybe it is a use for you: AutoIt. With that program, you can write scripts on top of any window like Putty and execute all commands you want to (like button pressing or mouse clicking in textboxes or buttons).
This way you can emulate all steps you are always doing with Putty.
entering a command after you logged in can be done by going through SSH section at the bottom of putty and you should have an option Remote command (data to send to the server) separate the two commands with ;
mputty can do that but it does not seem to work always. (if that wait period is too slow)
mputty uses putty and it extends putty.
There is an option to run a script.
If it does not work, make sure that wait period before typing is a high value or increase that value. See putty sessions , then name of session, right mouse button,properties/script page.
For me it works this way:
putty -ssh root#1.1.1.1 22 -pw password
putty, protocol, user name # ip address port and password. To connect in less than a second.
You can use the -i privatekeyfilelocation in case you are using a private key instead of password based.

Is it possible to open a file in an running instance of Matlab from the command line?

If I already have an instance of Matlab running is it possible to tell open a file in the Matlab editor from outside the Matlab application? I'm wondering if there it is possible do something like this.
Launch an instance of Matlab
$ ./matlab
Open a file for editing using an already running instance of Matlab:
$ matlab_open_file.sh theFile.m
The GUI variant is dragging a file from a folder and then dropping it onto Matlab icon (this actually works under OS X)
Note I know that you can launch Matlab and have it immediately execute a command (you could use this to start the editor on launch). This is not what I want.
I scripted a workaround for Linux (functional on Mint 17.1 with R2014a and R2014b), which I then associated with the .fig and .m file extensions. Note that this requires xdotool to be installed, and the keystrokes are set for Windows shortcuts (by default, MATLAB ships with Emacs shortcuts on Linux, but virtually everyone changes them in my experience). This has the limitation that any text currently on the command line is erased, and there is a small window of time where MATLAB must not lose focus. But in the absence of a more robust solution, it works well enough for me.
#!/bin/bash
# Hacky way to open a MATLAB figure in an existing instance if there is
# one, and start a new instance if not.
# What are we trying to open?
FILENAME="$#";
# Try to identify the main MATLAB window.
MLWINDOW=$( comm -12\
<(xdotool search --name MATLAB\ R | sort)\
<(xdotool search --class "com-mathworks-util-PostVMInit" | sort) )
if [ -z "$MLWINDOW" ]; then
# MATLAB isn't open; we have to open it to proceed.
matlab -desktop -r "open('$FILENAME')"
else
# We use the first existing instance since MATLAB is open
set -- $MLWINDOW
# Jump to the command line and erase it
xdotool windowactivate --sync $1 key --delay 0 "control+0" Escape
# Put the filename on the command line
xdotool type --delay 0 "$FILENAME"
# Select the filename and press ctrl-D to open, then clean up command line
xdotool key --delay 0 "shift+Home" "control+d" Escape
fi
You can type the path+filename into the command line and if a matlab session is open it will open this file in the current matlab session.
Note that this only works if you make sure matlab is the default program to open this kind of file. (Tested with .m file)
I modified Aoeuid's approach because
it did not work for me, as I had reassigned Ctrl+0 which jumps to the command line (and I don't see where I could set this to another value) → I replaced it with the “open file” dialog (Ctrl+O).
I might want to open scripts that are not on the current matlab path → I use $PWD/$filename instead of $filename. You could modify his version by using open($PWD/$FILENAME) and KP_Enter instead of $FILENAME and shift+Home/control+d.
This is the result:
#!/bin/bash
filename="$1"
# Try to identify the main MATLAB window.
MLWINDOW=$( comm -12\
<(xdotool search --name MATLAB\ R | sort)\
<(xdotool search --class "com-mathworks-util-PostVMInit" | sort) )
if [ -z "$MLWINDOW" ]; then
# MATLAB isn't open; we have to open it to proceed.
matlab -desktop -r "open('$PWD/$filename')"
else
## Activate window:
xdotool windowactivate --sync $MLWINDOW && \
## Press Ctrl+O to open the "open" dialog:
xdotool key --delay 0 "control+o" && \
## Wait for the file dialog to open:
sleep 0.5 && \
## Type the file name including the current directory
xdotool type --delay 0 "$PWD/$filename" && \
## Press enter:
xdotool key "KP_Enter"
fi
However, using key presses for an automated process might cause unwanted results.
Make sure that you added you folder to path.
Then you go to folder you need.
and just type in Matlab terminal
your_program_name
Then your program will run.

How does PUTTY/PLINK determine a command has returned?

Is it newline? prompt? What exactly?
Trying to run powershell over plink and the command executes but plink doesn't recognise its finished and the session hangs. Most curiously though, the command executes successfully when sent through the shell (via Putty). However, when sent via plink, the same command hangs...
Any ideas?
Telnet is nearly a raw TCP connection. All Putty needs back is a response from the server. The rest is controlled by the shell and SSH/Telnet server.
While your task is running, it's not going to return a command prompt.
On Linux, Unix, and Mac OS X you could put a & after the command to run it in the background and return to the command prompt.
Try running it in the local terminal/command shell. You should basically see the same thing.
Ok, well I'm still not quite sure what the problem is, but I've found a workaround via the TeamCity forums.
Basically you want to echo some abitrary string and pipe that output into your powershell executable, like thus:
echo 'executing powershell...' | C:\windows\system32\windowspowershell \v1.0\powershell.exe exit 1
So then your full plink command becomes:
plink.exe user#someIp -i key.ppk -P 22 -batch -v "echo 'executing powershell...' | C:\windows\system32\windowspowershell\v1.0\powershell.exe exit 1"
Nb. Plink will still pass through return codes and console output using this method.
Link to TeamCity forum:
http://youtrack.jetbrains.net/issue/TW-6021
Hope this helps
I had the same problem with an other program. I used the >&2 (redirect output to std err) after the last command, this worked fine for me.
Just add "return XX" into remote shell script, it will be return value to local console. After plink has been finished, type echo %errorlevel% to see return code XX.