Ana səhifə Sistem Linux-da SSH komandaları(Misallarla)

Linux-da SSH komandaları(Misallarla)

1192

SSH ayarlar

-A  Enables forwarding of the authentication agent connection.

-i identity_file
    Selects a file from which the identity (private key) for RSA or DSA authentication is read.
    The default is ~/.ssh/identity for protocol version 1, and ~/.ssh/id_rsa and ~/.ssh/id_dsa for protocol version 2.

-n  Redirects stdin from /dev/null (actually, prevents reading from stdin).
    This must be used when ssh is run in the background.

-t  Force pseudo-tty allocation.  This can be used to execute arbitrary screen-based programs on a remote
    machine, which can be very useful, e.g., when implementing menu services.  Multiple -t options force tty
    allocation, even if ssh has no local tty

Kənardan qoşulma

ssh IP
ssh USER@IP
ssh -l USER IP          <- (l = login name)
ssh -i KEY  USER@IP     <- (i = identity = Key)

Uzaq hostda əmr göndərmə

Əgər siz sudo istifadə etmirsinizsə

‘-n’ Bir çox əməliyyatlar üçün vacibdir

ssh USER@IP COMMAND
ssh -l USER IP COMMAND
ssh -n USER@IP COMMAND    <- "-n" bu shell scriptdə loop yaratmaq üçün gərəklidir.
su - USER -c "ssh USER@IP COMMAND"

Example

ssh -n 127.0.0.1 hostname
ssh -n 192.168.0.10 /usr/sbin/ntpq -p
ssh -n 192.168.0.10 cat /etc/hosts |grep localhost

Əgər sudo istifadə edirsinizsə

ssh -n USER@IP sudo COMMAND

Örnək

ssh -n 192.168.0.10 sudo /sbin/reboot &
ssh -n 192.168.0.10 uptime
 
ssh -n 192.168.0.10 sudo cat /etc/hosts |grep localhost
ssh -n 192.168.0.10 "sudo su - -c '/tmp/tmp.sh >> /tmp/2013052.txt && diff /tmp/20130501.txt /tmp/20130502.txt'"
 
ssh -n $i  sudo "bash -c 'sed -i \"s/^rotate 14/rotate 30/\" /etc/logrotate.conf'"

Proses

ssh -n 127.0.0.1 sudo /etc/init.d/httpd restart
ssh -n 192.168.0.10 "hostname;sudo su - -c '/etc/init.d/httpd stop;/etc/init.d/httpd status'"

Şəbəkə

ssh-n 192.168.0.10  sudo"bash -c 'echo "\\# Comment" >> /etc/hosts' "
ssh-n 192.168.0.10  sudo"bash -c 'echo "x.x.x.x   test-server1" >> /etc/hosts' "
ssh-n 192.168.0.10  sudo"bash -c 'sed s/192.168.100.10/192.168.50.10/g /etc/hosts > /etc/hosts.20121111' "
ssh-n 192.168.0.10  sudo"bash -c 'sed -i \"s/192.168.100.10/192.168.50.10/\" /etc/hosts'"
ssh-n IP_ADDRESS sudo/sbin/routeadd -net 192.168.0.0 netmask 255.255.255.0 gw 192.168.0.1
ssh-n IP_ADDRESS sudo"cp -p /etc/sysconfig/static-routes /etc/sysconfig/static-routes.`date '+%Y%m%d'`"
ssh-n IP_ADDRESS sudo"bash -c 'echo \"any host 192.168.100.1 gw 192.168.0.5\" >> /etc/sysconfig/static-routes'"
ssh-n IP_ADDRESS sudo"bash -c 'echo \"any net 192.168.10.0 netmask 255.255.0.0 gw 192.168.0.5\" >> /etc/sysconfig/static-routes'"
ssh-n 192.168.0.10  sudo"bash -c 'sed -i \"/192.168.20.0/i any net 192.168.10.0 netmask 255.255.0.0 gw 192.168.0.5\" /etc/sysconfig/static-routes'"
ssh-n 192.168.0.10 "hostname; netstat -rn  |grep 10.110.0"

rpm qurulumu

ssh -n xx.xx.xx.xx sudo "bash -c 'ls /tmp/*.rpm | xargs ls -lh ; echo OK'"
ssh -n xx.xx.xx.xx sudo "bash -c 'ls /tmp/*.rpm | xargs rpm -ivh ; echo OK'"
ssh -n xx.xx.xx.xx  rpm -qa |grep XXX |wc -l

hostname

H=test-server1
ssh -n xx.xx.xx.xx sudo hostname $H
ssh -n xx.xx.xx.xx sudo "cp -p /etc/sysconfig/network /etc/sysconfig/network.`date '+%Y%m%d'`"
ssh -n xx.xx.xx.xx sudo "bash -c 'grep -v HOSTNAME /etc/sysconfig/network > /tmp/network ; cat /tmp/network > /etc/sysconfig/network ; echo HOSTNAME=$H >> /etc/sysconfig/network'"
ssh -n xx.xx.xx.xx cat /etc/sysconfig/network

İstifadəçi

ssh -n xx.xx.xx.xx sudo /usr/sbin/userdel -r test1
ssh -n xx.xx.xx.xx sudo "bash -c 'userdel -r test1; userdel -r test2; userdel -r test3'"

Paket

ssh -n xx.xx.xx.xx  sudo yum -y install nc

Proqram Tərtibatı(Compileing Software)

#Ruby
ssh -n xx.xx.xx.xx sudo "bash -c 'cd /tmp && tar xzf ruby-2.0.0-p647.tar.gz'"
ssh -n xx.xx.xx.xx sudo "bash -c 'cd /tmp/ruby-2.0.0-p647 && ./configure --prefix=/usr/local/ruby2.0.0-p647'"
ssh -n xx.xx.xx.xx sudo "bash -c 'cd /tmp/ruby-2.0.0-p647 && make;make install'"
ssh -n xx.xx.xx.xx sudo ln -s /usr/local/ruby2.0.0-p647/bin/* /usr/local/bin/

Kernel Parametri

ssh -n xxxxxx cat /etc/sysctl.conf
ssh -n xxxxxx sudo "cp -p /etc/sysctl.conf /etc/sysctl.conf.`date '+%Y%m%d'`"
ssh -n xxxxxx sudo "bash -c 'echo \"\" >> /etc/sysctl.conf ; echo \"net.core.rmem_max = 33554432\" >> /etc/sysctl.conf ; echo \"net.core.wmem_max = 33554432\" >> /etc/sysctl.conf'"
ssh -n xxxxxx cat /etc/sysctl.conf
ssh -n xxxxxx sudo "bash -c 'sysctl -p |grep net.core |grep mem_max'"

Password

ssh -n xx.xx.xx.xx sudo "bash -c 'echo user:xxxxxx | chpasswd'"
ssh -n xx.xx.xx.xx sudo "bash -c 'echo \"root:pass123\" > pass.tmp ; chpasswd < pass.tmp ; rm -f pass.tmp'"

MƏSLƏHƏTLƏR

Bir çox hostlar olduqda

for i in 192.168.0.10 192.168.0.11 ; do ssh -n $i "hostname" ; done
for i in 192.168.0.10 192.168.0.11 ; do ssh -n $i "netstat -rn |grep 0.0.0.0" ; done
for i in 192.168.0.10 192.168.0.11 ; do ssh -n $i "hostname ; sudo su - -c '/etc/init.d/httpd stop ; /etc/init.d/httpd status'" ; done
for i in $H ; do ssh -n $i  sudo "bash -c 'sed -i \"s/^rotate 14/rotate 30/\" /etc/logrotate.conf'"  ; done
H="192.168.0.1 192.168.0.2"
for i in $H ; do ssh -n $i "hostname" ; done
for i in $H ; do ssh -n $i "netstat -rn |grep 0.0.0.0" ; done
for i in $H ; do ssh -n $i "hostname ; sudo su - -c '/etc/init.d/ntpd restart ; /etc/init.d/ntpd status'" ; done
for s in $(cat servers.txt); do ssh -n $s service httpd graceful; done

Misal: Marşrutlaşdırmanı necə dəyişdirmək olar.(routing dəyişmə)

H="web1 web2 web3"
 
for i in $H ; do ssh -n $i "hostname"  ; done
for i in $H ; do ssh -n $i "uname -n ; sudo cp -p /etc/sysconfig/static-routes /etc/sysconfig/static-routes.`date '+%Y%m%d'`"  ; done
for i in $H ; do ssh -n $i "uname -n ; ls -lh /etc/sysconfig/static-routes*"  ; done
for i in $H ; do scp -n $i.new $i:/tmp/  ; done
for i in $H ; do ssh -n $i "uname -n ; sudo cp -f /tmp/$i.new /etc/sysconfig/static-routes"  ; done
for i in $H ; do ssh -n $i "uname -n ; diff /etc/sysconfig/static-routes /etc/sysconfig/static-routes.`date '+%Y%m%d'`"  ; done
for i in $H ; do ssh -n $i "uname -n ; ls -lh /etc/sysconfig/static-routes*"  ; done
 
for i in $H ; do ssh -n $i "uname -n ; netstat -rn > /tmp/route.20170421"  ; done
for i in $H ; do ssh -n $i "uname -n ; netstat -rn |grep 10.50."  ; done
for i in $H ; do ssh -n $i "uname -n ; sudo /sbin/route add -net 10.50.0.0 netmask 255.255.0.0 gw 10.50.1.1"  ; done
for i in $H ; do ssh -n $i "uname -n ; netstat -rn |grep 10.50."  ; done
for i in $H ; do ssh -n $i "uname -n ; sudo /sbin/route del -net 10.50.5.0 netmask 255.255.255.0 gw 10.50.1.1"  ; done
for i in $H ; do ssh -n $i "uname -n ; netstat -rn |grep 10.50."  ; done
for i in $H ; do ssh -n $i "uname -n ; netstat -rn > /tmp/route.20170422"  ; done
for i in $H ; do ssh -n $i "uname -n ; diff  /tmp/route.20170421  /tmp/route.20170422"  ; done
 
for i in $H ; do ssh -n $i "uname -n ; ping -c 1 10.50.1.22 ; ping -c 1 10.50.2.1  ; ping -c 1 10.50.3.1" ; done
# cat /tmp/list.lst
192.168.10.5
192.168.10.6
192.168.10.7
#
for host in `cat /tmp/list.lst`
do
scp tmp.sh ${host}:/tmp
ssh -n ${host} "sudo su - -c '/tmp/tmp.sh >> /tmp/2013052.txt && diff /tmp/20130501.txt /tmp/20130502.txt'"
done

Qeyd

# man bash
(abbr)
command1 && command2
       command2 is executed if, and only if, command1 returns an exit status of zero.
(abbr)
$ echo aaa ; echo bbb ; echo ccc
aaa
bbb
ccc
$
$ echo aaa && echo bbb && echo ccc
aaa
bbb
ccc
$
 
 
$ ls aaa ; echo bbb
ls: aaa: No such file or directory
bbb
$
$ ls aaa && echo bbb
ls: aaa: No such file or directory
$                                          <--------not found bbb

uzaq fayl və yerli fayl arasında fərq

ssh remotename cat /etc/hosts | diff /etc/hosts  - 

Agent forwarding

user01@mypc:~ $ ssh -A ladder-server
user01@ladder-server:~ $ ssh  target-server

SSH istifadə etməklə kənardan prosesi başlatma

ssh user@host "/script/to/run < /dev/null > /tmp/mylogfile 2>&1 &"