Showing posts with label iptables. Show all posts
Showing posts with label iptables. Show all posts

Feb 5, 2015

Bash script to transfer files via SCP

#!/bin/bash  
REMOTE_IP="x.x.x.x"
SCP_PASSWORD="mypassword"
expect -c "  
 set timeout 1
 spawn scp -r /Local/SourceFolder uname@$REMOTE_IP:/Remote/DestFolder
 expect yes/no { send yes\r ; exp_continue }
 expect password: { send $SCP_PASSWORD\r }
 expect 100%
 sleep 1
 exit
"  
Save the above script to autoscp.sh and give the execute permission. The SourceFolder is the folder whose contents are sent to DestFolder on the remote host .
$ chmod +x file.sh
$ ./file.sh
Install expect if it's not on our machine, expect  is a program that "talks" to other interactive programs according to a script. We use this in this script to provide the password when asked for by the scp command.
sudo apt-get install expect

Bash script to sync local & remote folder via rsync

#!/bin/bash  
REMOTE_IP="x.x.x.x"
SCP_PASSWORD="mypassword"
#And now transfer the file over
expect -c "  
 set timeout 1
 spawn rsync -azvv -e ssh /Local/SrcFolder uname@$REMOTE_IP:/Remote/DstFolder
 expect yes/no { send yes\r ; exp_continue }
 expect password: { send $SCP_PASSWORD\r }
 expect 100%
 sleep 1
 exit
"  
-a preserves the date and times, and permissions of the files
-z compresses the data
-vv increases the verbosity of the reporting process
-e specifies remote shell to use

Save the above script to autorsync.sh file and give the execute permission.
Refer this Ubuntu documentation for more details on rsync. Both the bash scripts mainly automate the rsync and scp transfer, so that password don't need to be supplied.

Scheduling this rsync and scp scripts to run periodically

Use crontab to schedule these scripts to run periodically. To edit crontab use
crontab -e
Add the below lines
# m h  dom mon dow   command
0 * * * * cd /location/of/script;./autoscp.sh
30 * * * * cd /location/of/script;./autorsync.sh
m - minute
h - hour
dom - day of the month
mon - month
dow - day of the week
0 * * * *  will run the script at 0 minutes every hour, every day of all the months.
30 * * * * will run the script at every 30 minutes on every hour, every day of all the months.

Feb 9, 2014

My Sever
OS:Ubuntu 12.04
IP: 192.168.56.100

My Client
OS:Ubuntu 13.04
IP 192.168.56.101

Task
Block the ping request from client to server, so that when client ping the server, client should not get any reply.

Solution
Add the below rule in iptable of the server
iptables -A INPUT -i eth0 -p icmp --icmp-type echo-request -s 192.168.56.101 -j DROP

You have to specify the icmp-type as echo-request other wise the outgoing ping from server to client also will get blocked because when you ping client from server following happens
:-ICMP echo-request is send to client
:-ICMP echo-reply is send back from client to server(this get dropped if no icmp-type is indicated)

Save rules in iptables permanently
If you give iptables-save rules will be saved for the current session but will be gone once you reboot your machine. To save them permanently

  • Open '/etc/network/interfaces' file

vim /etc/network/interfaces

  • Append the below line along with your eth0 directives:

post-up /sbin/iptables-restore < /etc/iptables-up.rules

  • Now save the current iptable rules to '/etc/iptables-up.rules'

iptables-save > /etc/iptables-up.rules

About the flags used in the rule
-A: Append with the existing rules
-i: In interface name
-o: Out interface name
-p: protocol
-s: Source IP Address
-d: Destination IP Address
-j: Jump Target-> What to do when a packet that satisfy this rule comes (eg: ACCEPT, DROP, QUEUE, RETURN or name of a user specif chain)

Built in chain Names:
FORWARD:-For packets routed through the box
INPUT:-For packets coming into the box
OUTPUT:- For altering the locally generated packets before routing