Scripting For SSH
Scripting For SSH
Scripting For SSH
Presented by
Scott Klement
http://www.scottklement.com
2010-2011, Scott Klement
Setting up OpenSSH on i The OpenSSH tools: SSH, SFTP and SCP How do you use them? How do you automate them so they can be run from native programs (CL programs)
What is SSH
SSH is short for "Secure Shell." Created by: Tatu Ylnen (SSH Communications Corp) Bjrn Grnvall (OSSH short lived) OpenBSD team (led by Theo de Raadt) The term "SSH" can refer to a secured network protocol. It also can refer to the tools that run over that protocol. Secure replacement for "telnet" Secure replacement for "rcp" (copying files over a network) Secure replacement for "ftp" Secure replacement for "rexec" (RUNRMTCMD)
What is OpenSSH
OpenSSH is an open source (free) implementation of SSH. Developed by the OpenBSD team
but it's available for all major OSes
** in v5r3, had 5733-SC1 had to be ordered separately (no charge.) In v5r4 or later, it's shipped automatically. Starting with v6r1, its included on the B29xx_02 CD.
Install these with the CDs/DVDs that came with i For 5733-SC1:
RSTLICPGM LICPGM(5733SC1) DEV(OPTxx) OPTION(*BASE) RSTOBJ(*ALL) LNG(2924) RSTLICPGM LICPGM(5733SC1) DEV(OPTxx) OPTION(1) RSTOBJ(*PGM)
5
The Portable Application Solutions Environment (PASE) provides Unix compatibility on IBM i. Run AIX programs with minimal changes (or no changes) Use existing AIX compilers to generate the code Provides full Unix environment on i
6
This area is for text to scroll as the programs you run print output.
Commands are typed down here. (But wait until you see a prompt!) 7
Therefore, the preceding Unix command is equivalent to the following syntax at the traditional IBM i command-line:
CALL PGM(CD) PARM('/tmp')
By contrast, this is calling the program named 'mv', and passing two parameters:
mv test.key /home/klemscot/.ssh
8
This example works because the spaces and single quotes do not have special meanings when typed inside weak quotes:
More Info about PASE (as well as QShell) is found in the Information Center under Programming -> Shells and Utilities
11
ssh scp
Like telnet client (but secure) creates an interactive logon. also works as a 'remote command' tool. can create TCP 'tunnels' that are secured by SSH
Secure copy
Like Unix cp (copy) command, can copy stream files Copies securely over a network if prefixed by host name
sftp
Like ftp, but uses the SSH protocol (not FTP protocol) and is secure does not support ASCII/EBCDIC translation. Usually use CPYTOIMPF/CPYFRMIMPF with this tool.
sshd
Also: ssh-keygen ssh-agent
Acts as a server for all ssh tools (ssh, scp, sftp) Interactive logons will be PASE shell logons allowing true Unix ttys Can be chrooted (user is locked into a given area of the IFS)
for generating public and private keys allows you to load keys into memory for re-use
12
Unix commands are case-sensitive. Please match the upper/lower case exactly.
13
On i5/OS v5r3 or v5r4: The easiest way is to use QShell from the native environment:
STRQSH CMD('/QOpenSys/usr/sbin/sshd')
Note: Please don't try to start sshd via the QP2SHELL API. Strange results have been noted in that environment. Use QShell (STRQSH) instead.
14
Type the host name or IP address of the IBM i system where you started sshd
Verify that it's using port 22, and the ssh protocol 15
Sign in with your typical IBM i user-id, then press ENTER, then type your password and ENTER.
You are now in PASE. You can type Unix commands in a true Unix tty (not 5250!)
16
Sign in with your typical IBM i user-id, then press ENTER, then type your password and ENTER.
You now have a secure FTP session where you can 'get' or 'put' files. 17
SSH checks to see if host is in your known_hosts file Once you say 'yes', it remembers the digital key from that host. It verifies that it's always the same. The digital key is saved in the known_hosts file in your .ssh directory. 20
22
Digital Keys
Studies have shown passwords to be one of the weak links in security. A good password is long and random (and impossible to remember!) Most passwords are 8-15 characters long. (Easy to crack.) Subject to social-engineering attacks Subject to phishing attacks, man-in-the-middle attacks When coded into a script, a password is visible to anyone with access to source code or the ability to dump or debug the object. Studies have shown passwords to be one of the weak links in security. Bruce Schneier noted in 2006 that 55% of passwords on MySpace would be crackable in 8 hours with commercially available software. CERN analyzing an attack in 1998, it was found that the attacker (with help of software) had successfully guessed more than 47,000 passwords on a system with 186,000 accounts. This was done by taking common passwords from other sites.
Digital keys provide long, random, cryptographically verifiable "passwords" (authentication strings) that the user doesn't have to remember.
23
Never give the id_rsa file to anyone. Protect it with object-level security. The id_rsa.pub file will be given to other hosts for auto-logons.
24
For an interactive logon: ssh -l remote-user-id host.example.com without -l, assumes remote user name is same as local one. To run a remote command (without interactive logon): ssh -n -l klemscot host.example.com command-to-run -n disables input to the remote command (required in batch) -l klemscot is the userid I want to log in with. command-to-run is a command to run on the remote host. for sshd on IBM i, this is a PASE command to run a native command, you can use the 'system' tool.
26
Run a native command on an IBM i server (this would be entered as one long command. line wrapping added to make slide easier to read)
ssh -l scottk unix.example.com system \""sndmsg msg('Processing complete. Have a nice day!') tousr(klemscot)"\"
27
Another reason to use digital keys: passwords do work with ssh tool on a 5250 terminal passwords don't work with scp on 5250 passwords don't work with sftp on 5250
> sftp remote.example.com Connecting to remote.example.com... Host key verification failed. Connection closed $
This isn't true of a "real" Unix terminal, however. If you set up sshd and connect with Putty, you can use passwords with scp or sftp.
$ sftp remote.example.com Connecting to remote.example.com... Password: sftp>
28
SCP is much easier for automated file transfers than SFTP, because the whole process can be done in one line. QIBM_QSH_CMD_OUTPUT controls whether any messages are printed on the screen (or not) QIBM_QSH_CMD_ESCAPE_MSG causes an *ESCAPE message to be sent when a file transfer fails, MONMSG is used to capture that escape message.
30
31
You can set any ENVVAR you like with the ADDENVVAR command. ENVVARs can be inserted into a Unix command-line by preceding the variable name with $
CHGVAR VAR(&CMD) VALUE('PATH=$PATH:/QOpenSys/usr/bin && + sftp -b /tmp/example.sftp $SFTP_USER@$SFTP_HOST') ADDENVVAR ENVVAR(QIBM_QSH_CMD_OUTPUT) + VALUE('FILEAPPEND=/tmp/sftplog.txt') + REPLACE(*YES) ADDENVVAR ENVVAR(QIBM_QSH_CMD_ESCAPE_MSG) VALUE(Y) + REPLACE(*YES) QSH CMD(&CMD) MONMSG MSGID(QSH0000) EXEC(DO) SNDMSG MSG('File transfer failed! See /tmp/sftplog.txt') + TOUSR(KLEMSCOT) ENDDO ENDPGM
34
Passwords Do Work
Despite my earlier statement (as well as statements made in some IBM documents!) password authentication can be made to work with SCP and SFTP. They will work as long as the password seems to be typed from a valid Unix terminal (which means they can't come from a 5250 terminal or be supplied by a CL program or SFTP script.) Furthermore, passwords aren't a good idea. but what if you had an important cust who required password auth? The customer is always right! You don't really have a choice. There's a tool called Expect that's designed to automate any Unix tool.
35
36
37
CHGVAR VAR(&CMD) VALUE('PATH=$PATH:/QOpenSys/usr/bin+ :/QOpenSys/usr/local/bin && + expect -f myscript.exp') ADDENVVAR ENVVAR(SSH_USER) VALUE(&USER) REPLACE(*YES) ADDENVVAR ENVVAR(SSH_HOST) VALUE(&HOST) REPLACE(*YES) ADDENVVAR ENVVAR(SSH_PASS) VALUE(&PASS) REPLACE(*YES) ADDENVVAR ENVVAR(QIBM_QSH_CMD_OUTPUT) + VALUE('FILE=/tmp/expect.log') + REPLACE(*YES) ADDENVVAR ENVVAR(QIBM_QSH_CMD_ESCAPE_MSG) VALUE(Y) + REPLACE(*YES) QSH CMD(&CMD) MONMSG MSGID(QSH0000) EXEC(DO) SNDMSG MSG('File transfer failed! See /tmp/expect.log') + TOUSR(KLEMSCOT) ENDDO
FILE=/tmp/expect.log the 'FILE' part causes the file to be replaced each time.
38
More Infomation
To learn more about using OpenSSH on IBM i: IBM Porting Central (Official site of 5733-SC1) http://www-03.ibm.com/servers/enable/site/porting/tools/openssh.html IBM Technote: Using chroot to Restrict ssh to specific directories
http://www-01.ibm.com/support/docview.wss?uid=nas1eafce9e44f206348862575ce007c7619
OpenSSH information on ScottKlement.com Links to all of Scott's articles about SSH Manual pages for OpenSSH tools Additional links to IBM resources http://www.scottklement.com/openssh/ Expect (official site): http://expect.nist.gov Expect download for PASE: http://www.scottklement.com/expect/
39
This Presentation
You can download a PDF copy of this presentation from: http://www.scottklement.com/presentations/
Thank you!
40