shell免交互登录

1.免交互自动化脚本 //SSH免交互登录方法
   (1)expect免交互
       sudo apt-get install expect
       vi expect.sh   //内容如下:
       #!/usr/bin/expect
       set ip 192.168.88.190
       set user test
       set pass "123456"
       spawn ssh $user@$ip
       expect "password:" {send "$pass\r"}
       expect "$user@*" {send "df -h\r"}
       expect "$user@*" {send "exit\r"}
       expect eof
   (2)sshpass免交互
       sudo apt-get install sshpass
       # 免密ssh远程
       sshpass -p '123456' ssh -p 22 test@192.168.88.190
       # 免密ssh远程,并执行shell语句
       sshpass -p '123456' ssh -p 22 test@192.168.88.190 "ls -trl"
       # 免密scp拉取文件到本地
       sshpass -p '123456' scp -P 22 test@192.168.88.190:/opt/file.txt /tmp/
       # 免密scp传输文件
       sshpass -p '123456' scp -P 22 /tmp/file.txt test@192.168.88.190:/opt/
2.多终端开启
       终端将一闪而过,为避免其一闪而过可以进行如下设置:
       在terminal点右键,选择Profiles->Profile Preferences然后找到Title and Command,里面 有一项When command exits,后面选择为Hold the terminal open
       例子:gnome-terminal --tab --title="test" -- bash -c --command="pwd.exec bash"
3.EOF用法(免交互输入)    //输入文本ls -trl  pwd
       cat << EOF
       ls -trl
       pwd
       EOF

4.脚本后台运行
     nohup : 结果默认会输出到nohup.out;使用Ctrl + C发送SIGINT信号,程序关闭;关闭session发送SIGHUP信号,程序免疫
     & : 结果会输出到终端; 使用Ctrl + C发送SIGINT信号,程序免疫; 关闭session发送SIGHUP信号程序关闭
    nohup &同时免疫SIGINT和SIGHUP信号

posted on 2023-11-25 21:43  枫飘过的天1  阅读(5)  评论(0编辑  收藏  举报