Scripting via PuTTY or ....? - putty

Long ago I used a program called Procomm Plus in Windows for scripting/automating interactions with modems, etc. Now I'm using PuTTY/ssh for similar things like dealing with Juniper Networks NetOS devices. Right now I'm pretty much stuck with pasting text which doesn't allow looping.
I can think of a couple of variations on this:
- use something other than PuTTY ... but what?
- launch a script through PuTTY ... but how?

You could try
-m, which will "read a remote command or script from a file"
Take a look at the Putty Documentation - Section 3.7 in particular.
Edit:
I remember now - I was using plink for this:
plink user#machine -m local_script.sh
plink is included with Putty.

Related

How to switch on a computer using a bash command

I want to be able to switch on a computer using Linux Bash. Is there a way?
I know to switch off by use of such command as $(init 0) within a bash script
$(init 0)
You can use a wake-on-lan tool, passing the MAC address of the machine to start as an argument.
See: Bash one-line command to send wake on LAN magic packet without specific tool
Its doable in Mac (below), but a bit tricky on Linux. Best bet is to enter into the BIOS by hitting "F2" after you have switched your machine on and find the "Auto Power On" command ... but no scripting.
In Mac OS X bash its part of the "sudo systemsetup" command and could be achieved via "-setharddisksleep minutes" and you can even schedule it via crontab -e, but only as root user. To be honest in Mac its easier to just go to "Energy Saver" and set "Schedule" ... but no scripting.

Is it not possible to use Windows Bash as shell in Emacs for ssh etc?

I tried but get error: Process shell exited abnormally with code 255.
Mainly want this for SSH, and avoid Cygwin or plink/Putty.
I have this in config:
(setq explicit-shell-file-name "C:\\Windows\\System32\\bash.exe")
(setq explicit-bash.exe-args '("--noediting" "--login" "-i"))
(setenv "SHELL" shell-file-name)
(add-hook 'comint-output-filter-functions 'comint-strip-ctrl-m)
Thank you
I use Bash for Windows with the latest Windows Creator's Update and Ubuntu 16.04 they have included. It runs quite well on Visual Studio Code and Cmder (a shell app like MobaXTerm etc). I also use ZSH instead of Bash (with oh-my-zsh and powerline9k) but i had some adjustments to do (and, it takes time to load, but i've read somewhere that Microsoft is working to fix this slow issue).
To be honest, it's a good way to replace Putty, but it has to grow a little. There's a lot of network tools that can't work on WSL for example.
I know the pain to prepare a Cygwin or use Putty, but you can take a look on MobaXterm, a really good ssh client that includes a package manager to allow you to do a lot of things from your Windows. I don't know if they use Cygwin like in the past ... But it's a ready-to-use solution with local bash shell.
To finish this and maybe help you, here is my startup line to run WSL / Bash for Windows in Cmder: bash -l -i -cur_console:p -c zsh. If you need any more information just ask :).

ipython: Can I provide input to a shell command

Can I execute a shell command that requires input in ipython and/or an ipython notebook?
When I execute such a command, I see it's prompt, but no apparent way to provide it with input from my keyboard.
An example could be an rsync command to a remote server (thus requiring a password). There are no doubt dangers security-wise here - these are somewhat reduced in my case as I'm running on localhost.
Reposting as an answer, with a bit more detail:
No, it's a long standing issue that's really difficult to resolve: github.com/ipython/ipython/issues/514
The issue is roughly that our architecture can't tell when a process is waiting for input, so it doesn't know to prompt the user for input.
You may be able to use pexpect and raw_input to simulate this for specific programs. I.e. if you say what the prompt looks like, it spots that in the output, and asks the user for input to send back to the process. E.g. for Python:
import pexpect
p = pexpect.spawn('python')
while True:
try:
p.expect('\n>>> ')
print(p.before)
p.sendline(raw_input('>>> '))
except pexpect.EOF:
break
I've just given this a brief test - it works, but it's fairly rough. The concept could be improved.
Sadly, no.
I couldn't find documentation on this, so I went source-diving. The ipython code that actually performs that transformation is https://github.com/ipython/ipython/blob/master/IPython/core/inputtransformer.py#L208 , specifically:
def _tr_system(line_info):
"Translate lines escaped with: !"
cmd = line_info.line.lstrip().lstrip(ESC_SHELL)
return '%sget_ipython().system(%r)' % (line_info.pre, cmd)
which, in other words, invokes the underlying shell with everything following the !.
ipython is probably not what you want -- check out this answer for a Python alternate to include in scripts.
Was just looking for this and my wee face dropped when I saw it was a bit of an issue. Thought I would just post my solution in case it is usefull to anyone else.
Basically I was looking for a way to send sudo commands through the notebook, probably not very wise but I needed it for what I was doing. And i couldn't get a prompt for the password. So decided to use a x-terminal and sending the command through to the terminal. You don't get any feed back but may be due to not hooking to the IO on the way back. Here is what i used in the notebook:
In [1] !xterm -e sudo mount -o loop system.img /system/
I'm using linux but i would expect !cmd for windows might do the trick too
Many programs that require a password provide a variety of ways to prompt the user for it so that jupyter doesn't have to do it for you. The sudo command has a -A option and the SUDO_ASKPASS environmental variable. I've used it like this to use sudo to get permissions for the /var/log/btmp.1 file:
In [1]: !SUDO_ASKPASS=/usr/bin/ssh-askpass sudo -A lastb -a -f /var/log/btmp.1 | head
Similarly, for your case, ssh has an SSH_ASKPASS environmental variable.
Note that for headless operation of ssh/rsync, you can avoid authentication prompts from a notebook entirely by directly setting up an ssh agent (if it isn't running already) and referring to it with your SSH_AUTH_SOCK variable, like ssh-add does.
Or you can use a different program via SSH_ASKPASS which handles authentication in a custom way. See questions like How to automate SSH login with password? - Server Fault for more details, but be careful to not compromise your security especially when trying to automate connections from one server to another.

Run a shell script on Windows - Drupal install using DAMP

i've installed DAMP on Windows Vista, and have created a profile in my drupal folder with a shell script that needs to run.
profiles/donor_rally/rebuild.sh
However, I don't know how to execute this shell script.
I am not familiar with shell script command language, would you be willing to provide me with a step-by-step instructions on how to execute this shell script?
Thanks.
It looks like you're trying to run a Bourne shell script, which would usually only run on a unix machine. Guessing from the name, this is supposed to rebuild something, likely the database, but that's just a guess.
The short answer is you can't run it, at least not easily. If you were to post the (edited) contents of the script you might get some answers about how you could port it to a Windows cscript or batch file.
For others who are uninitiated to shell scripting in Windows:
I did not find a straightforward solution for running shell scripts on AMP hosted on Windows.
Instead I got an account at webenabled.com
Then used PuTTy to tunnel in.
I found this guide to using UNIX: http://freeengineer.org/learnUNIXin10minutes.html
And lynda.com

Launching server emacs from shell

I'm in a class that uses an implementation of Emacs on a school server. I'm on a mac running snow leopard, and I have my own implementation of Emacs on it. To access the server-Emacs, I ssh into the server and launch Emacs from its location there.
I'm relativly new to emacs, and I have a particular problem whenever I try to access the server-emacs from my local-emacs' shell-mode, having ssh'd into the server. It gives me the error that "Screen size -1x80 is too small", and doesn't launch the server-emacs.
I've the separate issue that when I try to do this in Apple's terminal, it does launch the server-emacs, but I really, really dislike the interface when emacs is launched within a terminal.
I've tried a couple of times to launch the server-emacs within a new window, in both scenarios, but apparently I'm not doing it right.
I think it'd be useful to understand what you're trying to do.
Do you just want to edit files on the server? If that's the case, read the documentation for tramp, and try:
C-x C-f //user#server:/path/to/file
If you really want to use the emacs running on the server, try creating a frame on your
(if so, look up tramp) If you want to actually use the emacs from the server, but have the window display on your mac:
ssh server
setenv DISPLAY mymac:0
emacsclient file &
This does assume you're running X11, and know how to resolve the display for your Mac. You can get X11 for the Mac here.
It's a bit hard to tell what you are doing, but you probably want to ssh to the server with an X tunnel, then run emacs there which will pop up the window on your mac.
First, don't use Terminal.
On your mac, start up X11 (google for XQuartz if you don't already have it).
Start up an XTerm (it should do this by default). From that XTerm, ssh to your server with the -Y option:
ssh -Y me#server.something
This should get you a remote shell and setup the DISPLAY environment to tunnel right back to your Mac's X server. Test it by running an xterm from there. If that works, you can instead run emacs. If that works, you can combine it with the ssh invocation:
ssh -Y me#server.something /usr/bin/emacs # or whatever path you need
You should set up ssh to not require a password but that's more than you asked for.
I think that Trey Jackson's suggestion of tramp (or the more old-fashioned 'ange-ftp) is probably your best bet.
In general, running emacs inside an emacs is never a good idea. You either want to run emacs on the server (in -nw mode inside the terminal, or via some $DISPLAY magic) or run it on your mac (via tramp). There isn't really a good way to do both.

Resources