LINUX913 shell:set ip [lindex $argv 0],\r,send_user,spawn ssh root@ip “cat “

问题

获取公钥

bash 复制代码
[code@samba ~]$ cat pub.sh
#!/bin/usr/expect
set ip "$1"
set password 123456
set timeout 20
spawn ssh root@192.168.235.100:cat ~/.ssh/id_rsa.pub
expect {
"yes/no" {send "yes/r";exp_continue}
"password:" { send "$pass/r" }
timeout {
echo "id _rsa.pub not exsits"
expect "#"
send "ssh-keygen"
expect
}
}
}

[code@samba ~]$ vim
bash 复制代码
[code@samba ~]$ cat pub.sh
#!/bin/expect
set ip "$1"
set password 123456
set timeout 20
spawn ssh root@192.168.235.100:cat ~/.ssh/id_rsa.pub
expect {
"yes/no" {send "yes/r";exp_continue}
"password:" { send "$pass/r" }
timeout {
echo "id _rsa.pub not exsits"
expect "#"
send "ssh-keygen"
expect {
"rsa_pub):" { send "/r"}
"passphrase):" { send "/r" }
"again:" { send "/r" }
}
}
}


[code@samba ~]$ ./pub.sh
can't read "1": no such variable
    while executing
"set ip "$1""
    (file "./pub.sh" line 2)
[code@samba ~]$ ./pub.sh 192.168.235.12
can't read "1": no such variable
    while executing
"set ip "$1""
    (file "./pub.sh" line 2)
bash 复制代码
[code@samba ~]$ cat pub.sh
#!/bin/expect
set ip "lindex args0"
set password 123456
set timeout 20
spawn ssh root@192.168.235.100:"cat ~/.ssh/id_rsa.pub"
expect {
"yes/no" {send "yes/r";exp_continue}
"password:" { send "$pass/r" }
timeout {
echo "id _rsa.pub not exsits"
expect "#"
send "ssh-keygen"
expect {
"rsa_pub):" { send "/r"}
"passphrase):" { send "/r" }
"again:" { send "/r" }
}
}
}


[code@samba ~]$ ./pub.sh
spawn ssh root@192.168.235.100:"cat ~/.ssh/id_rsa.pub"
ssh: Could not resolve hostname 192.168.235.100:"cat: Name or service not known
[code@samba ~]$
bash 复制代码
[code@samba ~]$ cat pub.sh
#!/bin/expect
set ip [lindex $argv 0]
set pass 123456
set timeout 20
spawn ssh root@192.168.235.100 "cat ~/.ssh/id_rsa.pub"
expect {
"yes/no" {send "yes\r";exp_continue}
"password:" { send "$pass\r" }
timeout {
send_user "id _rsa.pub not exsits"
expect "#"
send "ssh-keygen"
expect {
"rsa_pub):" { send "\r"}
"passphrase):" { send "\r" }
"again:" { send "\r" }
}
send_user "id_rsa.pub has been created"
}
send_user "id_rsa.pub is existed"
}


[code@samba ~]$ ./pub.sh
spawn ssh root@192.168.235.100 cat ~/.ssh/id_rsa.pub
root@192.168.235.100's password: [code@samba ~]$

记录

bash 复制代码
    ┌─────────────────────────────────────────────────────────────────┐
    │                 • MobaXterm Personal Edition v23.2 •            │
    │               (SSH client, X server and network tools)          │
    │                                                                 │
    │ ⮞ SSH session to code@192.168.235.10                            │
    │   • Direct SSH      :  ✓                                        │
    │   • SSH compression :  ✓                                        │
    │   • SSH-browser     :  ✓                                        │
    │   • X11-forwarding  :  ✓  (remote display is forwarded th SSH)  │
    │                                                                 │
    │ ⮞ For more info, ctrl+click on help or visit our website.       │
    └─────────────────────────────────────────────────────────────────┘

Last login: Sun Aug 31 14:59:20 2025 from 192.168.235.1
[code@samba ~]$ vim pub.sh
[code@samba ~]$ ssh-keygen
Generating public/private rsa key pair.
Enter file in which to save the key (/home/code/.ssh/id_rsa):
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /home/code/.ssh/id_rsa.
Your public key has been saved in /home/code/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:ZdIv0j5weH79BKpjSw8Xt8kU/KyWP/CRP+vo8NKhly4 code@samba.w
The key's randomart image is:
+---[RSA 2048]----+
|                 |
|         .   .   |
|        . +   o  |
|         * .   + |
|        S = o + +|
|         B . X O |
|          *.* & +|
|         .oEo=.B.|
|         .ooB=.o*|
+----[SHA256]-----+
[code@samba ~]$ cat pub.sh
#!/bin/usr/expect
set ip "$1"
set password 123456
set timeout 20
spawn ssh root@192.168.235.100:cat ~/.ssh/id_rsa.pub
expect {
"yes/no" {send "yes/r";exp_continue}
"password:" { send "$pass/r" }
timeout {
echo "id _rsa.pub not exsits"
expect "#"
send "ssh-keygen"
expect
}
}
}

[code@samba ~]$ vim pub.sh
[code@samba ~]$ vim pub.sh
[code@samba ~]$ cat pub.sh
#!/bin/usr/expect
set ip "$1"
set password 123456
set timeout 20
spawn ssh root@192.168.235.100:cat ~/.ssh/id_rsa.pub
expect {
"yes/no" {send "yes/r";exp_continue}
"password:" { send "$pass/r" }
timeout {
echo "id _rsa.pub not exsits"
expect "#"
send "ssh-keygen"
expect {
"rsa_pub):" { send "/r"}
"phase):" { send "/r" }
"):" { send "/r" }
}
}
}


[code@samba ~]$ vim pub.sh
[code@samba ~]$ vim pub.sh
[code@samba ~]$ vim pub.sh
[code@samba ~]$ cat pub.sh
#!/bin/usr/expect
set ip "$1"
set password 123456
set timeout 20
spawn ssh root@192.168.235.100:cat ~/.ssh/id_rsa.pub
expect {
"yes/no" {send "yes/r";exp_continue}
"password:" { send "$pass/r" }
timeout {
echo "id _rsa.pub not exsits"
expect "#"
send "ssh-keygen"
expect {
"rsa_pub):" { send "/r"}
"passphrase):" { send "/r" }
"again:" { send "/r" }
}
}
}


[code@samba ~]$ chmod +x pub.sh
[code@samba ~]$ ./pub.sh
-bash: ./pub.sh: /bin/usr/expect: 坏的解释器: 没有那个文件或目录
[code@samba ~]$ vim pub.sh
[code@samba ~]$ which ecpect
/usr/bin/which: no ecpect in (/sbin:/bin:/usr/sbin:/usr/bin:/usr/local/mysql/bin:/home/code/.local/bin:/home/code/bin)
[code@samba ~]$ which expect
/bin/expect
[code@samba ~]$ vim pub.sh
[code@samba ~]$ cat pub.sh
#!/bin/expect
set ip "$1"
set password 123456
set timeout 20
spawn ssh root@192.168.235.100:cat ~/.ssh/id_rsa.pub
expect {
"yes/no" {send "yes/r";exp_continue}
"password:" { send "$pass/r" }
timeout {
echo "id _rsa.pub not exsits"
expect "#"
send "ssh-keygen"
expect {
"rsa_pub):" { send "/r"}
"passphrase):" { send "/r" }
"again:" { send "/r" }
}
}
}


[code@samba ~]$ ./pub.sh
can't read "1": no such variable
    while executing
"set ip "$1""
    (file "./pub.sh" line 2)
[code@samba ~]$ ./pub.sh 192.168.235.12
can't read "1": no such variable
    while executing
"set ip "$1""
    (file "./pub.sh" line 2)
[code@samba ~]$ vim pub.sh
[code@samba ~]$ cat pub.sh
#!/bin/expect
set ip "lindex args0"
set password 123456
set timeout 20
spawn ssh root@192.168.235.100:cat ~/.ssh/id_rsa.pub
expect {
"yes/no" {send "yes/r";exp_continue}
"password:" { send "$pass/r" }
timeout {
echo "id _rsa.pub not exsits"
expect "#"
send "ssh-keygen"
expect {
"rsa_pub):" { send "/r"}
"passphrase):" { send "/r" }
"again:" { send "/r" }
}
}
}


[code@samba ~]$ ./pub.sh
spawn ssh root@192.168.235.100:cat ~/.ssh/id_rsa.pub
ssh: Could not resolve hostname 192.168.235.100:cat: Name or service not known
[code@samba ~]$ vim pub.sh
[code@samba ~]$ cat pub.sh
#!/bin/expect
set ip "lindex args0"
set password 123456
set timeout 20
spawn ssh root@192.168.235.100:"cat ~/.ssh/id_rsa.pub"
expect {
"yes/no" {send "yes/r";exp_continue}
"password:" { send "$pass/r" }
timeout {
echo "id _rsa.pub not exsits"
expect "#"
send "ssh-keygen"
expect {
"rsa_pub):" { send "/r"}
"passphrase):" { send "/r" }
"again:" { send "/r" }
}
}
}


[code@samba ~]$ ./pub.sh
spawn ssh root@192.168.235.100:"cat ~/.ssh/id_rsa.pub"
ssh: Could not resolve hostname 192.168.235.100:"cat: Name or service not known
[code@samba ~]$ ./pub.sh 192.168.235.45
spawn ssh root@192.168.235.100:"cat ~/.ssh/id_rsa.pub"
ssh: Could not resolve hostname 192.168.235.100:"cat: Name or service not known
[code@samba ~]$ vim pub.sh
[code@samba ~]$ cat pub.sh
#!/bin/expect
set ip lindex $argv 0
set password 123456
set timeout 20
spawn ssh root@192.168.235.100:"cat ~/.ssh/id_rsa.pub"
expect {
"yes/no" {send "yes/r";exp_continue}
"password:" { send "$pass/r" }
timeout {
echo "id _rsa.pub not exsits"
expect "#"
send "ssh-keygen"
expect {
"rsa_pub):" { send "/r"}
"passphrase):" { send "/r" }
"again:" { send "/r" }
}
}
}


[code@samba ~]$ vim pub.sh
[code@samba ~]$ cat pub.sh
#!/bin/expect
set ip [lindex $argv 0]
set pass 123456
set timeout 20
spawn ssh root@192.168.235.100 "cat ~/.ssh/id_rsa.pub"
expect {
"yes/no" {send "yes\r";exp_continue}
"password:" { send "$pass\r" }
timeout {
send_user "id _rsa.pub not exsits"
expect "#"
send "ssh-keygen"
expect {
"rsa_pub):" { send "\r"}
"passphrase):" { send "\r" }
"again:" { send "\r" }
}
}
}


[code@samba ~]$ ./pub.sh
spawn ssh root@192.168.235.100 cat ~/.ssh/id_rsa.pub
root@192.168.235.100's password: [code@samba ~]$ vim pub.sh
[code@samba ~]$ cat pub.sh
#!/bin/expect
set ip [lindex $argv 0]
set pass 123456
set timeout 20
spawn ssh root@192.168.235.100 "cat ~/.ssh/id_rsa.pub"
expect {
"yes/no" {send "yes\r";exp_continue}
"password:" { send "$pass\r" }
timeout {
send_user "id _rsa.pub not exsits"
expect "#"
send "ssh-keygen"
expect {
"rsa_pub):" { send "\r"}
"passphrase):" { send "\r" }
"again:" { send "\r" }
}
send_user "id_rsa.pub has been created"
}
send_user "id_rsa.pub is existed"
}


[code@samba ~]$ ./pub.sh
spawn ssh root@192.168.235.100 cat ~/.ssh/id_rsa.pub
root@192.168.235.100's password: [code@samba ~]$
bash 复制代码
[code@samba ~]$ ./pub.sh
spawn ssh root@192.168.235.100 cat ~/.ssh/id_rsa.pub
root@192.168.235.100's password: [code@samba ~]$ vim pub.sh
[code@samba ~]$ cat pub.sh
#!/bin/expect
set ip [lindex $argv 0]
set pass 123456
set timeout 20
spawn ssh root@192.168.235.100 "cat ~/.ssh/id_rsa.pub"
expect {
"yes/no" {send "yes\r";exp_continue}
"password:" { send "$pass\r" }
timeout {
send_user "id _rsa.pub not exsits"
expect "#"
send "ssh-keygen"
expect {
"rsa_pub):" { send "\r"}
"passphrase):" { send "\r" }
"again:" { send "\r" }
}
send_user "id_rsa.pub has been created"
}
send_user "id_rsa.pub is existed"
}
expect eof

[code@samba ~]$ ./pub.sh
spawn ssh root@192.168.235.100 cat ~/.ssh/id_rsa.pub
root@192.168.235.100's password:
cat: /root/.ssh/id_rsa.pub: 没有那个文件或目录
[code@samba ~]$
相关推荐
FPGA_Linuxer6 小时前
vivado自定义IP显示只读解决办法
网络·网络协议·tcp/ip
梦想blog8 小时前
漏洞修复 CentOS x86_64 OpenSSH 升级操作文档
linux·运维·centos·ssh·漏洞修复
数模加油站19 小时前
最新R(4.4.1)及R-studio保姆级安装配置详细教程及常见问题解答
开发语言·windows·数学建模·r语言
wangjialelele1 天前
OSI模型、网络地址、与协议
linux·服务器·网络·tcp/ip
一只月月鸟呀1 天前
AI使用 Node.js modbus-serial 搭建一个可交互的 Modbus TCP 主站与从站 Demo
网络协议·tcp/ip·node.js
想不明白的过度思考者1 天前
JavaEE初阶——网络原理初探:从独立模式到TCP/IP五层模型
网络·tcp/ip·java-ee
绵绵细雨中的乡音1 天前
网络层协议—IP协议
网络·网络协议·tcp/ip
czliutz1 天前
R语言绘制股票K线图及布林线
开发语言·r语言
aitav02 天前
⚡ WSL2 搭建 s5p6818 Linux 嵌入式开发平台 (part 3):Wifi驱动移植、ssh移植、e2fsprogs移植
linux·wifi·ssh·嵌入式·e2fsprogs
牛奶咖啡132 天前
实现Linux的ssh免密登录实操保姆级教程
linux·ssh·生成ssh密钥对的三种方法·添加公钥到需ssh免登录服务器·测试ssh免登录的服务器·生产环境linux的优化策略