putty and pscp, are they the same? - putty

Is there away to combine this into one batchfile? This is a hand me down task, but I think I should be able to combine the two. Why open Putty once and then open another tool pscp? I just don't know the difference between putty and pscp? Any help or insite would be appreciated. Thank you.
C:\putty.exe, 192.168.1.1 -pw xxxx
killall /bob/bin/myfile.out
Close putty
pscp.exe myfile.out.2.3.4 root#192.168.1.1:/bob/bin/myfile.out
pw
Reboot target

No, putty is analagous to ssh and pscp to scp.
PuTTY provides the overall functionality as does SSH. However, ssh and putty (non-capitalized) are interactive (as well as non-interactive by supplying shell commands as arguments) command line interfaces within the greater context of what PuTTY and SSH can do. scp and pscp are secure versions of the ftp Linux/Unix command (with similar, but not identical, command sets) which use the SSH protocol to securely authenticate and transfer files.
So essentially, there is not a way to combine them. The Unix philosophy is that one tool does one thing and does it really well.

Related

Shell Script program to download files from linux remote server

I am very new in shell scripting , i want to download some files from linux remote server ,so how can i proceed for that.That remote server is ssh based .
first of all, ftp service is better choice to get files from remote server.
If only sshd service is available, then you may use ssh based command sftp or scp.
However, using sftp or scp commands will invoke an interactive password prompt, which is a problem in shell script --> You have to ask for help to expect command. see Automate scp file transfer using a shell script .
Besides expect, you may also set up trust relationship between two servers, then you may use scp without password. See http://www.linuxproblem.org/art_9.html

single line telnet commands using terminal

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

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 to create SSH tunnel using PuTTY in Windows?

I need to create SSH tunnel with PuTTY in Windows, that would do the same as this command in Linux:
ssh -fN -L 2000:SomeIp:2000 myusername#myLinuxBox
I tried many options in PuTTY, including setting source port in GUI to "2000" and destination to "SomeIp:2000". Destination is set to local (as the -L switch suggests).
I successfully login to my SSH box but port forward is not made.
Is this even possible in Windows, so that all the connections made by programs that use this port (2000) will go through this tunnel?
With the PuTTY suite, you can set up a tunnel either using the PuTTY itself (GUI) or using the command-line tool plink.exe.
With the plink.exe, you use the same arguments as with the OpenSSH ssh, except for the -f, which does not have an equivalent in Windows.
plink.exe -N -L 2000:SomeIp:2000 myusername#myLinuxBox
Reference: Using the command-line connection tool Plink
With the PuTTY, the -L 2000:SomeIp:2000 translates to:
So it's actually, what you claim to have tried. If you have any problems, use the PuTTY event log to investigate:
The -N translates to the option "Don't start a shell or command at all".
But it probably does not make sense with a GUI client to enable it, as you get a window anyway, you just cannot do anything with it. See also the PuTTY wish no-terminal-window.
If you are going to use the tunnel to connect with PuTTY to another server, you can actually set up the tunnel as a part of the session settings with use of plink as a proxy, see: PuTTY configuration equivalent to OpenSSH ProxyCommand.
You probably want to use plink.exe instead of the GUI client. The command line syntax is compatible iirc.
Or you can wade through the putty GUI, which also allows this. See Connection > SSH > Tunnels on the left side with the option tree.
The answers above mention two different ways of resolving the problem:
using plink
using putty GUI
I don't have plink and I can't download the plink.exe file (it is a closely monitored restricted environment) so used a different way to script the solution with a one-line script:
start putty -ssh myusername#myLinuxBox -pw my_pw -L 2000:localhost:2000
Store this in a batch file (file extension ".bat", e.g. "tunnel.bat"). So every time you double-click on this file the command is run and putty creates the ssh tunnel. If you want more tunnels just repeat this part
-L 2000:localhost:2000
by changing the ports accordingly.
"Source" port is a port number on the same machine from which you are running putty (eg. open this in your browser: 127.0.0.1:source). "Destination" is your remote port that you want to connect to from your local machine. It started to work after I realized 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