single line telnet commands using terminal - bash

I need to pull something along the lines of "telnet root#192.168.2.99: irinject BACK"
however this refuses to work. There is no password required.
What is the correct syntax to perform this task using the terminal on Ubuntu 11.10?

If you absolutely must do it this way, use echo or etc. to pipe commands to the telnet session — and be ready to reinstall machines as they get hacked.
Strongly preferred is to use ssh with key access; you can even include the command that way.
ssh -i path/to/root-key root#host command

Related

Here document is cutting off commands

I'm trying to connect to my server via SSH and issue some commands to it. For some reason it seems like the commands are getting cut "off".
Here is the code that does the putty connection as well as issuing the SSH commands:
./plink.exe ${USER}#${HOSTNAME} -pw ${PASS}<<SSH
cd /some/foo/bar
deploy_artifact.sh --instance development1 some_artifact.ear
APP_development1.sh restart
exit
SSH
For me it works, but on the machine of my colleague the issued SSH commands are getting cut off and thus are not interpreted correctly. For example deploy_artifact.sh is getting turned into ploy_artifact.sh (See the following the screenshot).
How can i prevent this? And what is causing this?
Thanks in advance for any help!
It appears the problem is with the plink and how it is used. The given example sends commands as a standard input, however I did not find in the plink manual any mention that it reads commands from the STDIN. It is better to avoid undocumented features, since they may not work correctly or the author may remove them without any notice. Instead, if you want to pass commands inline you should provide them as an argument, ie you either have to use a quoted text, or you can wrap heredoc in the "$(cat *heredoc* )" code, eg:
./plink.exe ${USER}#${HOSTNAME} -pw ${PASS} "$(cat <<SSH
cd /some/foo/bar
deploy_artifact.sh --instance development1 some_artifact.ear
APP_development1.sh restart
exit
SSH
)"
Or, you can keep the commands in a file and run the plink with the -m commands_file option.

ssh concatenating keyboard characters

I am sshing into a mac running zsh but each character that I type is remembered and repeated. For example, if it type exit, I see eexexiexit on the screen but the command exit is executed.
If I type directly on the remote machine, there is not a problem so it is the communication via ssh that is the issue. I have no idea how to solve. Any suggestions?
I realised that I just needed to ssh into the remove server with the following command: 'TERM=screen-256color ssh ...

Injecting bash prompt to remote host via ssh

I have a fancy prompt working well on my local machine. However, I'm logging to multiple machines, on different accounts via ssh. I would love to have my prompt synchronized everywhere by ssh command itself.
Any idea how to get that? In many cases I'm accessing machines using root account and I can't change permanently any settings there. I want the prompt synchronized.
In principle this is just setting variable PS1.
Try this :
ssh -l root host -t "bash --rcfile /path/to/special/bashrc"
maybe /path/to/special/bashrc can be /tmp/myrc by example

How to automate password entry?

I want to install a software library (SWIG) on a list of computers (Jenkins nodes). I'm using the following script to automate this somewhat:
NODES="10.8.255.70 10.8.255.85 10.8.255.88 10.8.255.86 10.8.255.65 10.8.255.64 10.8.255.97 10.8.255.69"
for node in $NODES; do
scp InstallSWIG.sh root#$node:/root/InstallSWIG.sh
ssh root#$node sh InstallSWIG.sh
done
This way it's automated, except for the password request that occur for both the scp and ssh commands.
Is there a way to enter the passwords programmatically?
Security is not an issue. I’m looking for solutions that don’t involve SSH keys.
Here’s an expect example that sshs in to Stripe’s Capture The Flag server and enters the password automatically.
expect <<< 'spawn ssh level01#ctf.stri.pe; expect "password:"; send "e9gx26YEb2\r";'
With SSH the right way to do it is to use keys instead.
# ssh-keygen
and then copy the *~/.ssh/id_rsa.pub* file to the remote machine (root#$node) into the remote user's .ssh/authorized_keys file.
You can perform the task using empty, a small utility from sourceforge. It's similar to expect but probably more convenient in this case. Once you have installed it, your first scp will be accomplished by following two commands:
./empty -f scp InstallSWIG.sh root#$node:/root/InstallSWIG.sh
echo YOUR_SECRET_PASSWORD | ./empty -s -c
The first one starts your command in the background, tricking it into thinking it's running in interactive mode on a terminal. The other one sends it data from stdin. Of course, putting your password anywhere on command line is risky due to shell history being preserved, users being able to see it in ps results etc. Not secure either, but a bit better thing would be to store the password in a file and redirect the second command's input from that file instead of using echo and a pipe.
After copying to the server, you can run the script in a similar manner:
./empty -f ssh root#$node sh InstallSWIG.sh
echo YOUR_SECRET_PASSWORD | ./empty -s -c
You could look into setting up passwordless ssh keys for that. Establishing Batch Mode Connections between OpenSSH and SSH2 is a starting point, you'll find lots of information on this topic on the web.
Wes' answer is the correct one but if you're keen on something dirty and slow, you can use expect to automate this.

How do you use ssh in a shell script?

When I try to use an ssh command in a shell script, the command just sits there. Do you have an example of how to use ssh in a shell script?
Depends on what you want to do, and how you use it. If you just want to execute a command remotely and safely on another machine, just use
ssh user#host command
for example
ssh user#host ls
In order to do this safely you need to either ask the user for the password during runtime, or set up keys on the remote host.
First, you need to make sure you've set up password-less (public key login). There are at least two flavors of ssh with slightly different configuration file formats. Check the ssh manpage on your system, consult you local sysadmin or head over to How do I setup Public-Key Authentication?.
To run ssh in batch mode (such as within a shell script), you need to pass a command you want to be run. The syntax is:
ssh host command
If you want to run more than one command at the same time, use quotes and semicolons:
ssh host "command1; command2"
The quotes are needed to protect the semicolons from the shell interpreter. If you left them out, only the first command would be run remotely and all the rest would be run on the local machine.
You need to put your SSH public key into the ~/.ssh/authorized_keys file on the remote host. Then you'll be able to SSH to that host password-less.
Alternatively you can use ssh-agent. I would recommend against storing the password in the script.
You can use expect command to populate the username/password info.
The easiest way is using a certificate for the user that runs the script.
A more complex one implies adding to stdin the password when the shell command asks for it. Expect, perl libraries, show to the user the prompt asking the password (if is interactive, at least), there are a lot of choices.

Resources