Advanced Linux Shell Techniques (with sample commands)

in linux •  5 years ago 

change one character in a file: tr "\r" "\n" < in > out

execute arguments from a file: find /home -name 'weblog_config.pl' -exec {} ; -print

execute commands from arguments of a file: awk -F : '{print "chown -R " $1 " " $6}' < /etc/passwd | /bin/sh

building a file from another file:
#! /bin/sh
(for i in cat domainlist; do
echo "zone "$i" {"
echo
done) > $1 slave.conf

booting up single user rescue: kernelname single root=/dev/hdc1 init=/bin/sh

remounting a broken partition read-only: mount -o ro,remount /dev/hdc1 /

iterative command with delay: while true; do ps; sleep 1; done

mass text replace within files: perl -pi -e 's/foo/bar/g;' filenames*

test a webserver after telnet to port 80:
GET / HTTP/1.1
HOST: localhost
\n
\n b

manual query of an SQL server:
/c databasename
/d
/d tablename
select * from tablename:
pg-dump databasename

port-forward an SSH tunnel from a remote host:
ssh -f [email protected] 2222:localhost:80 -N
lynx http://localhost:2222
this forwards remote hosts httpd port (80) to local host on port 2222 for web viewing
-f == go into background, -N == don't execute a command on the remote host

reverse port-forward an SSH tunnel to a remote host:
ssh -R 10000:localhost:22 sysadmin@remotehost
anyone@anyhost# ssh sysadmin@remotehost(then->)sysadmin@remotehost# ssh -p 10000 (you come back to localhost from outside)

full IP forwarding over SSH using tunnel device* (as opposed to forward a single port):
add the line "PermitTunnel Yes" to /etc/ssh/sshd_config (must install openssh-server)
root@localhost# ssh -w0:0 gatewayhost
root@localhost# ifconfig tun0 10.0.2.1 netmask 255.255.255.252
root@gatewayhost# ifconfig tun0 10.0.2.2 netmask 255.255.255.252
(each box should ping each other now respectively at these ip's)
root@localhost# route add -host gatewayhost dev eth0

  • to access entire subnet of gatewayhost's locally connected hosts (add to above):
    root@localhost# route add -net gatewayhostipblock/24 dev tun0
    root@gatewayhost# echo 1 > /proc/sys/net/ipv4/ip_forward
    root@gatewayhost#/sbin/iptables-t nat -A POSTROUTING -o eth0 -j MASQUERADE

use PPP to direct connect via any serial port or chat string (example- modem on /dev/ttyS0, setup string == at&fdt, phone number == 678.445.4143 that answers CONNECT then expects a login: and password: prompt, with login sysadmin, password Test):
root@localhost# pppd connect 'chat -v "" at&fdt*70,6784454143 CONNECT "" ogin: sysadmin word: Test' /dev/ttyS0 57600 debug modem crtscts defaultroute

open up a pipe on udp socket 53 and chat to another host on it to test in a terminal:
machine1: # netcat -lu 53
machine2: # netcat -u machine1 53

setting up a VPN when there is no tun0 device and you only have root on client:

client# /usr/sbin/pppd updetach noauth silent nodeflate pty "/usr/bin/ssh root@remote-gw/usr/sbin/pppd nodetach notty noauth" ipparam vpn 10.0.0.1:10.0.0.254

Authors get paid when people like you upvote their post.
If you enjoyed what you read here, create your account today and start earning FREE STEEM!
Sort Order:  

Congratulations @harekrishnarama! You have completed the following achievement on the Steem blockchain and have been rewarded with new badge(s) :

You published your First Post
You got a First Vote

You can view your badges on your Steem Board and compare to others on the Steem Ranking
If you no longer want to receive notifications, reply to this comment with the word STOP

Vote for @Steemitboard as a witness to get one more award and increased upvotes!