34
Review Please hand in your homework and practicals Advanced Redirection >> 2> &> More VI Scripting

Review Please hand in your homework and practicals Advanced Redirection >> 2> &> More VI Scripting

Embed Size (px)

Citation preview

Page 1: Review Please hand in your homework and practicals Advanced Redirection >> 2> &> More VI Scripting

Review

Please hand in your homework and practicals Advanced Redirection

>> 2> &>

More VI Scripting

Page 2: Review Please hand in your homework and practicals Advanced Redirection >> 2> &> More VI Scripting

Today

Folders /dev

More vi Cheatsheets!

Processes

Page 3: Review Please hand in your homework and practicals Advanced Redirection >> 2> &> More VI Scripting

Folders - /dev /dev – holds the raw devices used by the

system Hard drives (HDDs) referenced by connection type IDE - /dev/hda SAS/SATA - /dev/sda CDROM - /dev/cdrom DVD - /dev/dvd (sometimes /dev/cdrom) Printer - /dev/lp RAM - /dev/ram TTY’s - /dev/tty Random - /dev/random

Page 4: Review Please hand in your homework and practicals Advanced Redirection >> 2> &> More VI Scripting

Using /dev Most things will be automatically used by the

system RAM/TTY/RAID

HP’s RAID is /dev/cciss – this is probably different between each RAID card vendor (LSI, Dell, HP, etc…)

They will have documentation telling you where to go

Sometimes you’ll need to manually “mount” a new HDD or a CDROM mount <device> <target> You can’t access /dev directly, you have to “mount”

it on the system to access it

Page 5: Review Please hand in your homework and practicals Advanced Redirection >> 2> &> More VI Scripting

Using /dev - /mnt /mnt – location where extra devices are to be

“mounted” or set for access on the system Changing to /media, either works, our VM’s will

use /media for auto-discovered CD’s mkdir /mnt/usb mount <device> <target> mount /dev/sdc /mntusb So if we were going to “mount” a floppy drive –

fd1 – what would the command be?

Page 6: Review Please hand in your homework and practicals Advanced Redirection >> 2> &> More VI Scripting

/dev We’ll get to adding hdd’s – that’s it’s own

process When you want to attach a “hot plug” device

To update, recover, move files, etc… If the system is up, it will print out the new hdd

is found If it doesn’t print out, there is a special log

called the dmesg that will show you

Page 7: Review Please hand in your homework and practicals Advanced Redirection >> 2> &> More VI Scripting

dmesg dmesg – the kernel message buffer (also a

command) Typing in dmesg will show you the kernel info

since boot Usually used to capture boot information or

errors (dmesg | grep) Earliest at the beginning, most recent at the end Typing in dmesg (and looking for a new hard

drive) will usually be right at the bottom unless there’s a lot of activity on the system

Page 8: Review Please hand in your homework and practicals Advanced Redirection >> 2> &> More VI Scripting

Quick Review - Vi vi <filename> Opens file for editing Inside vi you always start in command mode i, a, A to get into insert mode, escape key to get

back out While there, you can do things like copying,

pasting, and jumping around New: yy copies p pastes

Page 9: Review Please hand in your homework and practicals Advanced Redirection >> 2> &> More VI Scripting

Vi Review, part 2

Move around through the line number When you open a file, vi prints the filename,

lines, and characters in the bottom left “/home/student/script.sh” 19L, 383C So to jump to the end

Assuming you’re already in vi in command mode :19 <enter>

To jump to line ten, :10 <enter>

Page 10: Review Please hand in your homework and practicals Advanced Redirection >> 2> &> More VI Scripting

More vi Fun• Can open to a specific line number, or jump to a specific

line when inside a file

• For instance, vi’s configuration file /etc/vimrc

• We want to do ‘syntax highlighting’ in vi to make it easier to read

• [student@it136centos65vm ~]# grep -in ‘syntax’ /etc/vimrc

• 47:”Switch syntax highlighting on, when the terminal has colors

• 50: syntax off

• Quick glance shows line 47 as a descriptor, and line 50 as the setting

Page 11: Review Please hand in your homework and practicals Advanced Redirection >> 2> &> More VI Scripting

vi Jump to Line

• We can open vi to a specific line:

• Notice ‘etc’ in the current directory below - we’re in /etc so we can use the “relative path” of vimrc

• [student@it136rhel65vm conf]$ vi +50 vimrc

• Or inside vi we can jump to a specific line

• (in command mode)

• :50

Page 12: Review Please hand in your homework and practicals Advanced Redirection >> 2> &> More VI Scripting

Searching vim

• Inside vi we use the / to denote a search string

• /syntax

• It is case-sensitive: /Syntax would not show us anything

• Takes us to line 47

• Switch to next instance by using the n key

• Go back with the ? key

Page 13: Review Please hand in your homework and practicals Advanced Redirection >> 2> &> More VI Scripting

Other Useful Options

• y is the ‘yank’ character, actually copies

• yy will copy the line

• p will ‘put’ it into wherever the cursor is

• dd deletes the line

• u is undo

• set <option> allows configuration of vi - this is not saved, these should go into /etc/virc or /etc/vimrc files

• r replaces a single character (puts you into insert mode for that single character)

• R replaces until you leave ‘insert’ mode (esc key)

Page 14: Review Please hand in your homework and practicals Advanced Redirection >> 2> &> More VI Scripting

Scripts

So we have seen commands for management useradd, usermod, sudo cat But what happens when you have a task you

need to do several hundred times over? We use a script A script is simply a collection of commands

Page 15: Review Please hand in your homework and practicals Advanced Redirection >> 2> &> More VI Scripting

Every Script We went over ! (called bang) Well, # is called ‘she’ (many refer to it as hash) The first line of EVERY script must have a

‘shebang’ The shebang is the script telling the shell which

shell to use to execute the script We use #!/bin/bash #!/bin/zsh #!/bin/csh…

Page 16: Review Please hand in your homework and practicals Advanced Redirection >> 2> &> More VI Scripting

Now we get interesting All we need to do is add commands There are a few constructs to help us, though We can make comments

Comments must have # in front of them They only go from where # starts to the end of the

line We can use variables

‘Best practice’ is to use all caps FLAGS = “-alh” ls $FLAGS So once we set it, we reference it with $

Page 17: Review Please hand in your homework and practicals Advanced Redirection >> 2> &> More VI Scripting

Finally We can take arguments $1 is the variable inside a script for whatever

was passed as the first argument You run a script (or a command not set in /bin

or /sbin) by running ./script Sometimes you need to ‘build from source’ and

you’ll use ./configure So if we pass an argument ./script argument $1 = argument

Page 18: Review Please hand in your homework and practicals Advanced Redirection >> 2> &> More VI Scripting

Extrapolation We used $FLAGS as a variable, if we’re

debugging a script, how do we do a quick ‘sanity check’ (or log check) to print out to STDIN what value is in $FLAGS?

What might $2 stand for?

Page 19: Review Please hand in your homework and practicals Advanced Redirection >> 2> &> More VI Scripting

Putting it all together#!/bin/bash

# script to review the basic parts of a script

# takes path as arg, flags are set as a variable

# lists the path

FLAGS=“-alh”

echo $1 # show what was passed as argument

ls $FLAGS $1

#done

[student@it136centos58vm ~]$ ./ls_script.sh /tmp

Page 20: Review Please hand in your homework and practicals Advanced Redirection >> 2> &> More VI Scripting

Questions on Scripting?

Any command in Linux is available in a script You can use ‘loops’ or ‘control structures’ You can create functions You can include libraries You can do lots of fun stuff Even more useful after we hit regular

expressions

Page 21: Review Please hand in your homework and practicals Advanced Redirection >> 2> &> More VI Scripting

Questions on Scripting? What is the one thing that is needed in a Linux

script? What’s it called and where is it located?

Once we have that one thing, what else do we put inside the script?

Page 22: Review Please hand in your homework and practicals Advanced Redirection >> 2> &> More VI Scripting

Communication Between Systems

From the command line, we can open up communications to other systems

The SSH command allows us to open a remote shell

ssh 192.168.1.100 Replace 192.168.1.100 with any IP you want You are unable to do this, you don’t have

another VM set up at the above IP address

Page 23: Review Please hand in your homework and practicals Advanced Redirection >> 2> &> More VI Scripting

Not So Quick Aside

● Computers talk to each other over 'interfaces'● NIC = Network Interface Card - adds a

connection to a system● Onboard/embedded – means it's built into the

motherboard● Speeds usually 1G (negotiated with switch)● Linux has lo, ppp, wlan0, wlan1, etc..., eth0,

eth1, etc...

Page 24: Review Please hand in your homework and practicals Advanced Redirection >> 2> &> More VI Scripting

“Corporate” Networks Several overarching types

Not topologies – that’s for another class Now mostly traditional and “services oriented” Traditional is broken down into 3 ‘subnets’:

Development (Dev), Test, and Production (Prod) Development is random, little security,

considered unstable, not a priority Test is a ‘mirror’ (or as close to as possible) of

Production and is used to ensure a patch or update will not crash a network

Page 25: Review Please hand in your homework and practicals Advanced Redirection >> 2> &> More VI Scripting

“Corporate” Networks, 2 Production is your corporate network

Hp.com is public-facing web server If that went down, customers couldn’t contact

us online – very bad! Also, internal “mission critical” applications

Payroll, networks, firewalls, etc… In HP’s case we have tools to help us build

systems, track quotes, etc… At BlueCross, the system that processes insurance

claims are ‘production’ systems (term covers hardware and software)

Page 26: Review Please hand in your homework and practicals Advanced Redirection >> 2> &> More VI Scripting

New Networks Google, Amazon, Facebook, etc… a new type Services Oriented Architecture (SOA) Each module is a ‘service’

You connect to Amazon.com server Amazon.com server “aggregates” search,

recommendations, current product list index, current deal of the day, current ads on one display page

Then displays them Facebook staggers updates

Will test on a subset of users Then either push to all or rollback

Page 27: Review Please hand in your homework and practicals Advanced Redirection >> 2> &> More VI Scripting

And IPs

I used 192.168.1.100 as an example This is a “Class C” range of IP’s = 192.x.x.x Used heavily on home wifi devices These ranges will depend on who implemented

your network Most businesses with an IT staff of over 50

people have a “class A” IP address range (10.x.x.x)

Small offices will use class C’s

Page 28: Review Please hand in your homework and practicals Advanced Redirection >> 2> &> More VI Scripting

Back to SSH/SCP ssh 192.168.1.100 will connect me with the

system that has that IP address, I have to know a system is there

Prompts for username and password, all of the sudden:

[ndillon@testvm ~]# A shell! We can use that! We can specify a user too ssh [email protected] will login a

student user

Page 29: Review Please hand in your homework and practicals Advanced Redirection >> 2> &> More VI Scripting

Drawn Out

ssh 192.168.1.100 # from 192.168.1.50

Page 30: Review Please hand in your homework and practicals Advanced Redirection >> 2> &> More VI Scripting

SCP

What if we want to transfer files between computers?

We use scp (cp command over SSH) scp <path1> <path2> Simple right? Not so fast Our file is on our system, we want to put it on

the other system scp /tmp/file.backup 192.168.1.100:/tmp

Page 31: Review Please hand in your homework and practicals Advanced Redirection >> 2> &> More VI Scripting

SCP

scp <path1> <path2> Our file is on the other system scp 192.168.1.100:/tmp/install.rpm /tmp So IPAddress:/file/path is a common structure

on Linux (you’ll see it elsewhere too) I want to copy the /etc/profile file from my ‘dev’

system at 10.12.12.100 to my system – command?

Page 32: Review Please hand in your homework and practicals Advanced Redirection >> 2> &> More VI Scripting

Questions on SSH/SCP?

? Useful programs: Putty, WinSCP, Cygwin These allow SSH and SCP connections from

Windows Widely used in IT WinSCP allows FTP, SFTP, and SCP so it’s

very useful Also SecureCRT (licensed product)

Page 33: Review Please hand in your homework and practicals Advanced Redirection >> 2> &> More VI Scripting

Quick Demo

Scripting SSH SCP Practical

Page 34: Review Please hand in your homework and practicals Advanced Redirection >> 2> &> More VI Scripting

Own Study

• VI –

• Scripting

• Device Communication

• SSH

• SCP